10

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.

4
  • Maybe stick the process in the background using the & at the end of the ffmpeg command? Commented Dec 21, 2012 at 19:03
  • What you have should wait for ffmpeg to finish. Are you sure you're not getting an error from ffmpeg (like invalid parameters)? Commented Dec 21, 2012 at 19:05
  • 3
    The loop as you have it should wait until each of the inner commands runs, unless you have put an & at the end of the line. Do you get any error messages? Try prefacing the inner command with "echo" and adding a sleep 1 after it, to verify that the loop itself doesn't have issues. For example, in the command line you give, you're using file instead of $file Commented Dec 21, 2012 at 19:09
  • 4
    Does adding -nostdin option to ffmpeg help? It might be reading all the rest of the input to the while-loop. Commented Dec 21, 2012 at 19:14

4 Answers 4

12

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!

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

3

This is good for you?

find . -type f -name '*.mkv' -exec ffmpeg -i {} \;

Comments

3

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

ffmpeg -i $file &
wait $!

Thanks to puchu, here: apply ffmpeg to many files

1 Comment

This is an interesting approach I'll have to try this. Thanks! I found that providing the ffmpeg param -nostdin did the trick.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.