微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

bash – 当逐行读取文件时,我只能在第一行执行ffmpeg

我正在编写一个 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐