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

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...