Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash while loop wait until task has completed

I have a bash script that I created to process videos from within a folder and it's subfolders:

find . -type f -name '*.mkv' | while read file;
do
  ffmpeg -i $file ...
done

The problem: Instead of the while loop waiting ffmpeg to complete, it continues iterate through the loop. The end result is, files not getting processed. I need a way to have the current while loop iteration to wait until ffmpeg is complete before continuing to the next. Or alternatively a way to queue these items.

Edit: So The solution when iterating over a set of files is to pass the -nostdin param to ffmpeg. Hope this helps anyone else who might have a similar issue.

Also file --> $file was a copy/paste typo.

like image 872
mac2017 Avatar asked Dec 21 '12 19:12

mac2017


4 Answers

I realize I posted this a while ago but I found the solution. Thanks for all of the responses. Providing the -nostdin param to ffmpeg will do the trick. It will process only the current file before moving onto the next file for processing.

ffmpeg's -nostdin option avoids attempting to read user input from stdin otherwise the video file itself is interpreted.

ffmpeg -i <filename> ... -nostdin

The best part about using the above is that you can continue to use verbosity in case an error is thrown in the output:

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

OR if you would rather report the output to a file do so this way:

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

You can also use a combination of the two as well. Also thanks @Barmar for the solution!

like image 162
mac2017 Avatar answered Oct 02 '22 20:10

mac2017


I think that this is as simple as you missing the $ before file.

find . -type f -name '*.mkv' | while read file;
do
    ffmpeg -i $file ...
done
like image 31
Buggabill Avatar answered Oct 02 '22 19:10

Buggabill


This is good for you?

find . -type f -name '*.mkv' -exec ffmpeg -i {} \;
like image 26
alinsoar Avatar answered Oct 02 '22 20:10

alinsoar


I'm a vicious answer snatcher. I found one:

ffmpeg -i $file &
wait $!

Thanks to puchu, here: apply ffmpeg to many files

like image 33
Koyot Avatar answered Oct 02 '22 19:10

Koyot