加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 21:24:34 所属栏目:安全 来源:网络整理
导读:我正在编写一个 Bash脚本,我在其中逐行读取文件(嗯,(查找/ home / user / -iname“* .MP4”)),对于每一行,我执行ffmpeg,所以我正在做这样的事情: while read -r line; do ffmpeg -i "$line" [ MORE OPTIONS ... ]done (find /home/user/ -iname "*.MP4") 虽
我正在编写一个 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读