While loop is running only for the first iteration

Hello,
I've written a script to automate encoding of all the MP4 files in a directory (incl. subdirectories). But unfortunately it's running for the first MP4 file only.
My machine details:

root@Ubuntu16:~# uname -a
Linux Ubuntu16 4.10.0-28-generic #32~16.04.2-Ubuntu SMP Thu Jul 20 10:19:48 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
root@Ubuntu16:~# bash --version
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
root@Ubuntu16:~#

My Script:

root@Ubuntu16:~# cat auto1.sh
#!/bin/bash
set -vx
MAIN_DIR="/root/So1b3"
cd $MAIN_DIR
find . -name "*.[Mm][Pp]4" >/tmp/mp4_List
cat /tmp/mp4_List |while read eachF
do
#cp -rp "${eachF}" /tmp
SUB_DIR=`dirname "${eachF}"`
cd "${MAIN_DIR}"/"${SUB_DIR}"
FILE_NAME=`basename "${eachF}"`
FILE_NAME1=`echo "${FILE_NAME}"|awk -F"." '{print $1}'`
Audio_ID=`mediainfo "${FILE_NAME1}.mp4" |grep -A1 "^Audio" |grep ID |awk '{print $NF}'`
MP4Box -raw ${Audio_ID} "${FILE_NAME1}.mp4" -out "${FILE_NAME1}.aac"
ffmpeg -i "${FILE_NAME1}.aac" -f caf - | fdkaac -b16 -I -p29 - -o "${FILE_NAME1}.m4a"
MP4Box -rem 1 -rem 2 -add "${FILE_NAME1}.m4a" "${FILE_NAME1}.mp4"
echo "Processing of ${FILE_NAME1}.mp4 has been DONE ....."
done

If I Run, it exits after first iteration (Ofcourse, it's processing the first file successfuly - ignore the warnings)

root@Ubuntu16:~# ./auto1.sh
MAIN_DIR="/root/So1b3"
+ MAIN_DIR=/root/So1b3
cd $MAIN_DIR
+ cd /root/So1b3
find . -name "*.[Mm][Pp]4" >/tmp/mp4_List
+ find . -name '*.[Mm][Pp]4'
cat /tmp/mp4_List |while read eachF
do
#cp -rp "${eachF}" /tmp
SUB_DIR=`dirname "${eachF}"`
cd "${MAIN_DIR}"/"${SUB_DIR}"
FILE_NAME=`basename "${eachF}"`
FILE_NAME1=`echo "${FILE_NAME}"|awk -F"." '{print $1}'`
Audio_ID=`mediainfo "${FILE_NAME1}.mp4" |grep -A1 "^Audio" |grep ID |awk '{print $NF}'`
MP4Box -raw ${Audio_ID} "${FILE_NAME1}.mp4" -out "${FILE_NAME1}.aac"
ffmpeg -i "${FILE_NAME1}.aac" -f caf - | fdkaac -b16 -I -p29 - -o "${FILE_NAME1}.m4a"
MP4Box -rem 1 -rem 2 -add "${FILE_NAME1}.m4a" "${FILE_NAME1}.mp4"
echo "Processing of ${FILE_NAME1}.mp4 has been DONE ....."
done
+ cat /tmp/mp4_List
+ read eachF
dirname "${eachF}"
++ dirname './The FileSet Number 1234.html/Clip 5 ab-cd.mp4'
+ SUB_DIR='./The FileSet Number 1234.html'
+ cd '/root/So1b3/./The FileSet Number 1234.html'
basename "${eachF}"
++ basename './The FileSet Number 1234.html/Clip 5 ab-cd.mp4'
+ FILE_NAME='Clip 5 ab-cd.mp4'
echo "${FILE_NAME}"|awk -F"." '{print $1}'
++ echo 'Clip 5 ab-cd.mp4'
++ awk -F. '{print $1}'
+ FILE_NAME1='Clip 5 ab-cd'
mediainfo "${FILE_NAME1}.mp4" |grep -A1 "^Audio" |grep ID |awk '{print $NF}'
++ mediainfo 'Clip 5 ab-cd.mp4'
++ grep -A1 '^Audio'
++ grep ID
++ awk '{print $NF}'
+ Audio_ID=2
+ MP4Box -raw 2 'Clip 5 ab-cd.mp4' -out 'Clip 5 ab-cd.aac'
[iso file] Unknown box type wide
[iso file] Unknown box type alis
[iso file] Unknown box type alis
[iso file] Box ".enc" is invalid in container udta
[iso file] Unknown box type Xtra
Extracting MPEG-4 AAC
+ ffmpeg -i 'Clip 5 ab-cd.aac' -f caf -
+ fdkaac -b16 -I -p29 - -o 'Clip 5 ab-cd.m4a'
ffmpeg version 2.8.11-0ubuntu0.16.04.1 Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.4) 20160609
  configuration: --prefix=/usr --extra-version=0ubuntu0.16.04.1 --build-suffix=-ffmpeg --toolchain=hardenex-gnu --incdir=/usr/include/x86_64-linux-gnu --cc=cc --cxx=g++ --enable-gpl --enable-shared --disable-strijpeg --disable-decoder=libschroedinger --enable-avresample --enable-avisynth --enable-gnutls --enable-ladsbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enabldi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-lle-librtmp --enable-libschroedinger --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeeheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enableble-libzvbi --enable-openal --enable-opengl --enable-x11grab --enable-libdc1394 --enable-libiec61883 --enaable-libx264 --enable-libopencv
  libavutil      54. 31.100 / 54. 31.100
  libavcodec     56. 60.100 / 56. 60.100
  libavformat    56. 40.101 / 56. 40.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 40.101 /  5. 40.101
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
  libpostproc    53.  3.100 / 53.  3.100
[aac @ 0x869580] Estimating duration from bitrate, this may be inaccurate
Input #0, aac, from 'Clip 5 ab-cd.aac':
  Duration: 00:19:20.80, bitrate: 201 kb/s
    Stream #0:0: Audio: aac (LC), 44100 Hz, stereo, fltp, 201 kb/s
Output #0, caf, to 'pipe:':
  Metadata:
    encoder         : Lavf56.40.101
    Stream #0:0: Audio: pcm_s16be, 44100 Hz, stereo, s16, 1411 kb/s
    Metadata:
      encoder         : Lavc56.60.100 pcm_s16be
Stream mapping:
  Stream #0:0 -> #0:0 (aac (native) -> pcm_s16be (native))
Press [q] to stop, [?] for help
01:09.474 (102x)   size=    8840kB time=00:00:51.31 bitrate=1411.2kbits/s
Enter command: <target>|all <time>|-1 <command>[ <argument>]
Parse error, at least 3 arguments were expected, only 1 given in string 'ret weapon 1 b3.html/Clip 1 Fisch
02:31.208 (102x)   size=   26320kB time=00:02:32.78 bitrate=1411.2kbits/s
Enter command: <target>|all <time>|-1 <command>[ <argument>]
Parse error, at least 3 arguments were expected, only 1 given in string 'ret weapon 1 b3.html/Clip 9 1.b3
03:52.943 (102x)   size=   35088kB time=00:03:23.68 bitrate=1411.2kbits/s
Enter command: <target>|all <time>|-1 <command>[ <argument>]
Parse error, at least 3 arguments were expected, only 1 given in string 'ret weapon 1 b3.html/Clip 6 Gelfa
05:14.677 (102x)   size=   52696kB time=00:05:05.89 bitrate=1411.2kbits/s
Enter command: <target>|all <time>|-1 <command>[ <argument>]
Parse error, at least 3 arguments were expected, only 1 given in string 'ret weapon 1 b3.html/Clip 2 Rappo
06:36.411 (102x)   size=   61448kB time=00:05:56.70 bitrate=1411.2kbits/s
Enter command: <target>|all <time>|-1 <command>[ <argument>]
Parse error, at least 3 arguments were expected, only 1 given in string 'ret weapon 1 b3.html/Clip 8 Nakam
07:58.145 (102x)   size=   78920kB time=00:07:38.12 bitrate=1411.2kbits/s
Enter command: <target>|all <time>|-1 <command>[ <argument>]
Parse error, at least 3 arguments were expected, only 1 given in string 'ret weapon 1 b3.html/Clip 3 Jobav
09:19.880 (102x)   size=   96400kB time=00:09:19.60 bitrate=1411.2kbits/s
Enter command: <target>|all <time>|-1 <command>[ <argument>]
Parse error, at least 3 arguments were expected, only 1 given in string 'ret weapon 1 b3.html/Clip 4 Jobav
10:41.614 (102x)   size=  105144kB time=00:10:10.35 bitrate=1411.2kbits/s
Enter command: <target>|all <time>|-1 <command>[ <argument>]
Parse error, at least 3 arguments were expected, only 1 given in string 'ret weapon 1 b3.html/Clip 7 Kramn
20:01.493 (102x)   size=  207256kB time=00:20:03.11 bitrate=1411.2kbits/s
video:0kB audio:207256kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000061%
20:03.119 (102x)
53057536 samples processed in 00:11.841
+ MP4Box -rem 1 -rem 2 -add 'Clip 5 ab-cd.m4a' 'Clip 5 ab-cd.mp4'
[iso file] Unknown box type wide
[iso file] Unknown box type alis
[iso file] Unknown box type alis
[iso file] Box ".enc" is invalid in container udta
[iso file] Unknown box type Xtra
IsoMedia import Clip 5 ab-cd.m4a - track ID 1 - HE-AACv2 (SR 22050 - SBR-SR 44100 - 1 channels)
Removing track ID 1
Removing track ID 2
Saving Clip 5 ab-cd.mp4: 0.500 secs Interleaving
+ echo 'Processing of Clip 5 ab-cd.mp4 has been DONE .....'
Processing of Clip 5 ab-cd.mp4 has been DONE .....
+ read eachF

The contents of the /tmp/mp4_List generated: (only first file got processed)

root@Ubuntu16:~# cat /tmp/mp4_List
./The FileSet Number 1234.html/Clip 5 ab-cd.mp4
./The FileSet Number 1234.html/Clip 1 ef-gh.mp4
./The FileSet Number 1234.html/Clip 9 1.b3 d5-2.mp4
./The FileSet Number 1234.html/Clip 6 GF-VW.mp4
./The FileSet Number 1234.html/Clip 2 RP-VW.mp4
./The FileSet Number 1234.html/Clip 8 Nak - And.mp4
./The FileSet Number 1234.html/Clip 3 Job- So.mp4
./The FileSet Number 1234.html/Clip 4 Job- Yu.mp4
./The FileSet Number 1234.html/Clip 7 Kr - Denk.mp4
root@Ubuntu16:~# 

Please advise on what mistake I'm doing here and how to fix it. Many thanks!!

---------- Post updated at 09:23 PM ---------- Previous update was at 08:00 PM ----------

Suspecting the behavior of ffmpeg and MP4Box (they might be interactive programs), I tried adding < /dev/null in the end of each line with above commands. but still no luck :frowning:

I don't have ffmpeg installed on my system. What does the final hyphen in the command:

ffmpeg -i "${FILE_NAME1}.aac" -f caf -

tell ffmpeg to do? With many utilities, that would be a request to have the utility read data from the standard input of that utility (which in this case would be the remaining lines from /tmp/mp4_List ) which would cause the while read to hit EOF on the 2nd iteration through the loop.

Your script would also run faster and use fewer system resources if you would change:

cat /tmp/mp4_List |while read eachF
do
... ... ...
done

to:

while read eachF
do
... ... ...
done < /tmp/mp4_List
1 Like

Hi Don,

Thank you for your reply and pointing about hyphen which is the actual culprit but I had to use it to get both commands to work in combination.

Luckily ffmeg offers a qualifier -nostdin which solves this problem. I used below line and everything works perfectly fine :b:

ffmpeg -nostdin -i "${FILE_NAME1}.aac" -f caf - | fdkaac -b16 -I -p29 - -o "${FILE_NAME1}.m4a"

Best regards,