逐行读取文件时,我只能在第一行执行 ffmpeg [英] When reading a file line by line, I only get to execute ffmpeg on the first line

查看:20
本文介绍了逐行读取文件时,我只能在第一行执行 ffmpeg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Bash 脚本,在其中我逐行读取文件(好吧,<(find/home/user/-iname "*.MP4") 准确地说),并且对于每一行我执行 ffmpeg,所以我正在做这样的事情:

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 ?

推荐答案

这是一个常见问题,由 ffmpeg 的特殊行为引起(ssh 也会发生).

That's a frequent problem which is caused by the special behavior of ffmpeg (also happens with ssh ).

引用自 Bash 常见问题 89,它几乎完全地处理了您的情况:

这里发生了什么?我们以第一个例子为例.read 从标准输入(FD 0)中读取一行,放入file参数中,然后执行ffmpeg.与您从 BASH 执行的任何程序一样,ffmpeg 继承标准输入,出于某种原因它会读取.我不知道为什么.但无论如何,当 ffmpeg 读取 stdin 时,它会吸收来自 find 命令的所有输入,导致循环饥饿.

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;博士:

有两个主要选项:

TL;DR :

There are two main options:

  • ffmpeg 行的末尾添加一个 </dev/null(即 ffmpeg -i "$line" [ MOREOPTIONS ] ... </dev/null ) 将解决该问题并使 ffmpeg 按预期运行.

  • Adding a </dev/null at the end of ffmpeg's line ( i.e. ffmpeg -i "$line" [ MORE OPTIONS ] ... </dev/null ) will fix the issue and will make ffmpeg behave as expected.

read 从不太可能的文件描述符中读取由随机程序使用:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆