当逐行读取文件时,我只能在第一行执行ffmpeg [英] When reading a file line by line, I only get to execute ffmpeg on the first line
问题描述
<(find / home / user / -iname* .MP4)
确切地说),对于每行,我执行 ffmpeg
,所以我做这样的事情: while -r line; do
ffmpeg -i$ line[MORE OPTIONS ...]
done< <(find / home / user / -iname* .MP4)
原因,只有第一行正在成功处理。
任何想法为什么我的代码忽略所有其他行?
这是一个常见的问题,这是由 ffmpeg
的特殊行为引起的(也发生在 ssh
)。
引自 Bash常见问题89 ,它处理您的案例几乎完全:
什么是在这里发生我们来举个例子。读取从标准输入(FD 0)读取一行,将其放在文件参数中,然后执行ffmpeg。 像从BASH执行的任何程序一样,ffmpeg继承标准输入 ,它由于某种原因读取。我不知道为什么但是无论如何,当ffmpeg读取stdin时, 会吸收find命令的所有输入,使循环 变坏。
TL; DR:
有两个主要选项: p>
-
在$ $的末尾添加
< / dev / null
c $ c> ffmpeg 的行(即ffmpeg -i$ line[更多选项] ...< / dev / null
)将解决问题,并使ffmpeg
按预期行为。 -
让
从文件描述符中读取,这不太可能被随机使用程序:
而IFS =读-r行<& 3; do
#这里读取的是从FD 3读取,文件被重定向到。
完成3<文件
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 ?
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 offfmpeg
's line ( i.e.ffmpeg -i "$line" [ MORE OPTIONS ] ... </dev/null
) will fix the issue and will makeffmpeg
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
这篇关于当逐行读取文件时,我只能在第一行执行ffmpeg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!