Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
584 views
in Technique[技术] by (71.8m points)

bash - When reading a file line by line, I only get to execute ffmpeg on the first line

I'm writing a Bash script in which I read a file line by line ( well, <(find /home/user/ -iname "*.MP4") to be exact ) , and for each line I execute ffmpeg, so I'm doing something like this:

while read -r line; do
    ffmpeg -i "$line" [ MORE OPTIONS ... ]
done < <(find /home/user/ -iname "*.MP4")

Though, for some reason, only the first line is being processed successfuly.

Any idea why my code ignores all other lines ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That's a frequent problem which is caused by the special behavior of ffmpeg (also happens with ssh ).

Quoted from Bash FAQ 89 which handles your case almost exactly:

What's happening here? Let's take the first example. read reads a line from standard input (FD 0), puts it in the file parameter, and then ffmpeg is executed. Like any program you execute from BASH, ffmpeg inherits standard input, which for some reason it reads. I don't know why. But in any case, when ffmpeg reads stdin, it sucks up all the input from the find command, starving the loop.

TL;DR :

There are two main options:

  • Adding a </dev/null at the end of ffmpeg's line ( i.e. ffmpeg -i "$line" [ MORE OPTIONS ] ... </dev/null ) will fix the issue and will make ffmpeg behave as expected.

  • Let read read from a File Descriptor which is unlikely to be used by a random program:

       while IFS= read -r line <&3; do
           # Here read is reading from FD 3, to which 'file' is redirected.
       done 3<file
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...