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

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

问题描述

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

  while -r line; do 
ffmpeg -i$ line[MORE OPTIONS ...]
done< <(find / home / user / -iname* .MP4)

原因,只有第一行正在成功处理。



任何想法为什么我的代码忽略所有其他行?

解决方案

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



引自 Bash常见问题89 ,它处理您的案例几乎完全


什么是在这里发生我们来举个例子。读取从标准输入(FD 0)读取一行,将其放在文件参数中,然后执行ffmpeg。 像从BASH执行的任何程序一样,ffmpeg继承标准输入 ,它由于某种原因读取。我不知道为什么但是无论如何,当ffmpeg读取stdin时, 会吸收find命令的所有输入,使循环 变坏。




TL; DR:



有两个主要选项: p>


  • 在$ $的末尾添加< / dev / null c $ c> ffmpeg 的行(即 ffmpeg -i$ line[更多选项] ...< / dev / null )将解决问题,并使 ffmpeg 按预期行为。


  • 文件描述符中读取,这不太可能被随机使用程序:

     而IFS =读-r行<& 3; do 
    #这里读取的是从FD 3读取,文件被重定向到。
    完成3<文件



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 ?

解决方案

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

Quoted from Bash FAQ 89 which handles your case almost exactly:

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 :

There are two main options:

  • 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.

  • 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天全站免登陆