While loop is running only for the first iteration


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While loop is running only for the first iteration
# 1  
Old 02-21-2018
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:
Code:
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:
Code:
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)
Code:
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)
Code:
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 Smilie
# 2  
Old 02-21-2018
I don't have ffmpeg installed on my system. What does the final hyphen in the command:
Code:
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:
Code:
cat /tmp/mp4_List |while read eachF
do
... ... ...
done

to:
Code:
while read eachF
do
... ... ...
done < /tmp/mp4_List

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 02-22-2018
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 Smilie

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

Best regards,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Loop iteration with two variables

Hello, I have been stuck on this for some time and invested many hours trying to find a solution. I am trying to either loop through two variables or or two arrays and not sure how to do it. I am limited to ksh only, and don't have the ability to do a foreach, or for i AND for j etc...I... (19 Replies)
Discussion started by: Decoy Octopus88
19 Replies

2. Shell Programming and Scripting

While loop is causing ssh command to exit from script after first iteration.

I am trying to check multiple server's "uptime" in a loop over "ssh". When I execute multiple ssh commands with hard coded servernames script is executing fine. But when I pass server names using while loop, script is exiting after checking first server's status, why? # serverList... (8 Replies)
Discussion started by: kchinnam
8 Replies

3. Shell Programming and Scripting

Getting the iteration count in WHILE LOOP

bash in RHEL 6.4 I have a requirement in which I want to get the iteration count from a WHILE LOOP. The below mentioned simple script test.sh works fine. In the below script, the WHILE loop will iterate every 5 seconds infinitely until it greps the string BASKETBALL from /tmp/somestring.txt... (6 Replies)
Discussion started by: John K
6 Replies

4. Shell Programming and Scripting

Do something only that last iteration of loop

I have a script with logic like: my_function() { if mkdir $1 mkdir mydir_${2} else do something else fi } read in list of items while read list do my_function $list `date` done so basically it will make a directory for every name in the list and create a directory with the... (6 Replies)
Discussion started by: glev2005
6 Replies

5. Shell Programming and Scripting

For Loop in shellscript - Printing Output for every iteration

for VGLIST in `lsvg -o` do CLOSED_OUT=`echo $VGLIST | lsvg -l $VGLIST | awk '{print $6 " " $7}' | grep closed` if ]; then echo "Filesystems $CLOSED_OUT in VG that are in Closed status" else echo "\n Some message" fi Above Code is working fine, but echo "Filesystems $CLOSED_OUT... (8 Replies)
Discussion started by: chandu123
8 Replies

6. Shell Programming and Scripting

for loop iteration and shell programming startup

question :how can i iterate to next item in for loop with the loop e.g for i in `cat abc.txt` do echo $i // this will display first line i=$i+1; // this doesnt work for me. echo $i; //this will display secound line done question: is my approach to manipulate text good? I have... (3 Replies)
Discussion started by: kashif_islam
3 Replies

7. Shell Programming and Scripting

Script running for one iteration and failing the second one

Hi, I am using Tclexpect to automation testing on switches and using regexp and the buffer outputs the program is running well for one iteration and failing the second one... can anyone please guide me what is the mistake i am making? Thanks Here is the small version of the program, while... (2 Replies)
Discussion started by: roh_20022002
2 Replies

8. Shell Programming and Scripting

howto stop loop iteration

I wonder how to stop further loop iterations when conditions gets false e.g. This file.txt contains the following structure : 1 2 3 4 5 6 7 8 9 10 How to stop iteration when if statement gets false ? for n in `cat file.txt` do if (( n<=5 )) (1 Reply)
Discussion started by: presul
1 Replies

9. Shell Programming and Scripting

Pick up the return code for every iteration and display the result only once in loop.

Hi All, I amlearning UNIX scripting. I have a small query. I would be thankful if any one helps me out. I have a below piece of code which delets the files. If file dosent have the permissions to delete a particular file I have used 2>>operator to track the error code. But my objective is... (1 Reply)
Discussion started by: manas6
1 Replies

10. Shell Programming and Scripting

New iteration of for-loop without incrementing?

Another question, is it possible to, in a for-loop incrementing until it reaches a certain number, to have it loop again without incrementing? Just have it drop what it is doing when it reaches this command and start again at the same number it was at? I know I could make a while loop and just... (0 Replies)
Discussion started by: jeriryan87
0 Replies
Login or Register to Ask a Question