Bash while loop wait until task has completed

懵懂的女人 提交于 2021-01-02 05:31:29

问题


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.


回答1:


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!




回答2:


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



回答3:


This is good for you?

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



回答4:


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

ffmpeg -i $file &
wait $!

Thanks to puchu, here: apply ffmpeg to many files



来源:https://stackoverflow.com/questions/13995715/bash-while-loop-wait-until-task-has-completed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!