Apply ffmpeg to many files

I wrote a simple script:

#!/bin/bash find . -name "*.m4a" | while read filename; do new_filename=$(echo "$filename" | sed "s/^\(.*\)m4a$/\1flac/g"); if [ ! -f "$new_filename" ] then #ffmpeg -i "$filename" -acodec flac "$new_filename" > /dev/null 2>&1; #wait $!; echo "$filename"; echo "$new_filename"; fi done 

outputs the correct result:

 ./Equilibrium, ALAC [GER] viking.folk/2003 - Demo 2003, ALAC/04 - Met.m4a ./Equilibrium, ALAC [GER] viking.folk/2003 - Demo 2003, ALAC/04 - Met.flac ./Equilibrium, ALAC [GER] viking.folk/2003 - Demo 2003, ALAC/02 - Nach Dem Winter.m4a ./Equilibrium, ALAC [GER] viking.folk/2003 - Demo 2003, ALAC/02 - Nach Dem Winter.flac 

if uncomment ffmpeg and wait:

 ./Equilibrium, ALAC [GER] viking.folk/2003 - Demo 2003, ALAC/04 - Met.m4a ./Equilibrium, ALAC [GER] viking.folk/2003 - Demo 2003, ALAC/04 - Met.flac uilibrium, ALAC [GER] viking.folk/2003 - Demo 2003, ALAC/02 - Nach Dem Winter.m4a uilibrium, ALAC [GER] viking.folk/2003 - Demo 2003, ALAC/02 - Nach Dem Winter.flac 

And no flasks were made!

PS

 #!/bin/bash find . -name "*.m4a" | while read filename; do new_filename=$(echo "$filename" | sed "s/^\(.*\)m4a$/\1flac/g"); if [ ! -f "$new_filename" ] then ffmpeg -i "$filename" -acodec flac "$new_filename"; echo "$filename"; echo "$new_filename"; fi sleep 5; done 

1) starts the encoding, but suddenly stops without error messages

2) coding could not start due to "equilibrium" instead of "./Equilibrium"

3) = 1)

4) = 2)

...

last) right

+3
bash ffmpeg
source share
4 answers

unexpectedly found a solution:

 #!/bin/bash find . -name "*.m4a" | while read filename; do new_filename=$(echo "$filename" | sed "s/^\(.*\)m4a$/\1flac/g"); if [ ! -f "$new_filename" ] then ffmpeg -i "$filename" -acodec flac "$new_filename" & wait $!; fi done 

I don’t know why, but if you start the process in the background (&) and wait for it (wait $!), Everything works correctly!

+3
source share

Two problems that I see:

 ffmpeg -i "$filename" -acodec flac "$new_filename" > /dev/null 2>&1; 

An error message may be printed that you do not see because you are doing > /dev/null 2>&1 , which will overshadow any errors.

and

 wait $! 

nothing useful will do, because $! only defined if you run the command in the background (i.e. something with & ).

0
source share

Oh, uh, just please simplify your sed, it will not fix your error, but will be more transparent :-P

 $ echo m4a.m4a | sed 's/m4a$/flac/g' m4a.flac 
0
source share

May I suggest that you can provide the following ffmpeg parameter. It will only process the current file before moving on to the next file.

Option

ffmpeg -nostdin avoids trying to read user input from stdin, otherwise the video file itself will be interpreted.

 ffmpeg -i <filename> ... -nostdin 

The best part of using the above is that you can continue to use verbosity:

 ffmpeg -i <filename> ... -nostdin -loglevel panic 

OR, if you prefer to report output to a file, do this:

 # Custom log name (optional) # FFREPORT=./${filename}-$(date +%h.%m.%s).log ffmpeg -i <filename> ... -nostdin -report 

You can also use a combination of the two.

0
source share

All Articles