我正在编写一个
Bash脚本,我在其中逐行读取文件(嗯,<(查找/ home / user / -iname“* .MP4”)),对于每一行,我执行ffmpeg,所以我正在做这样的事情:
while read -r line; do ffmpeg -i "$line" [ MORE OPTIONS ... ] done < <(find /home/user/ -iname "*.MP4")
虽然,由于某种原因,只有第一行正在成功处理.
知道为什么我的代码忽略了所有其他行吗?
解决方法
这是由ffmpeg的特殊行为引起的常见问题(也发生在ssh中).
引自Bash FAQ 89,几乎完全处理你的情况:
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:
有两个主要选择:
>在ffmpeg行的末尾添加< / dev / null(即ffmpeg -i“$line”[更多选项] ...< / dev / null)将解决问题,并使ffmpeg按预期运行.
>让我们从File Descriptor读取一个不太可能被随机程序使用的内容:
while IFS= read -r line <&3; do # Here read is reading from FD 3,to which 'file' is redirected. done 3<file