Bash 脚本在 while 循环中停止执行 ffmpeg - 为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12543628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:21:15  来源:igfitidea点击:

Bash script stops execution of ffmpeg in while loop - why?

bashloopswhile-loopffmpeg

提问by Alex Monthy

I have this bash script for converting .mp4video to .mp3audio. It runs, but does the loop only once, though there are more mp4 files in /home/myname/mp4dir.

我有这个用于将.mp4视频转换为.mp3音频的bash 脚本。它运行,但只循环一次,尽管/home/myname/mp4dir.

The script converts the first .mp4file it finds to .mp3, unless there is already an .mp3file. This should be done in a loop, but after the first call to ffmpeg the script stops.

该脚本将.mp4它找到的第一个文件转换为.mp3,除非已经有一个.mp3文件。这应该在循环中完成,但在第一次调用 ffmpeg 后脚本停止。

Why?

为什么?

#!/bin/bash
find /home/myname/mp4dir -name "*.mp4" | while read f
do
        fOut=`echo $f | sed s/mp4/mp3/`
        echo "convert $f => $fOut"
        if ! test -f $fOut
        then
                ffmpeg -i $f -vn -f mp3 $fOut
        fi
done

回答by kendosan

try this in terminal

在终端试试这个

create bash nohup.sh and paste in this

创建 bash nohup.sh 并粘贴于此

#!/bin/sh
while true
do
 /var/www/bash/Someother-bash-script-goes-here.sh
 sleep 60
done

in terminal type where nohup.sh is located

在 nohup.sh 所在的终端类型中

nohup sh nohup.sh >/dev/null 2>&1 &

nohup, will execute every 60 seconds, you can grep service check like this

nohup,每60秒执行一次,你可以像这样grep服务检查

 #!/bin/sh
SERVICE='ffmpeg'

if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
    echo "$SERVICE service running, will not bother ffmpeg yet"
else
    echo "$SERVICE is not running"
    echo "$SERVICE is not running!" | /var/www/bash/start_service.sh
fi

回答by Dr. Jan-Philip Gehrcke

From the comments we got that there actually are input files you are looping over (of what kind soever) and that ffmpegat least starts once. Then, you are stuck.

从我们得到的评论中,实际上有您正在循环的输入文件(无论哪种)并且ffmpeg至少开始一次。然后,你被卡住了。

Add

添加

&> ffmpeg.outerr < /dev/null 

to your ffmpegcommand and monitor the file ffmpeg.outerrwhile your "loop", i.e. ffmpeg, executes. It will tell you what the problem is. Either you can solve this problem yourself, or you come back to stackoverflow with this ffmpeg-specific problemusing the tag ffmpeg.

到您的ffmpeg命令并ffmpeg.outerr在您的“循环”(即 ffmpeg)执行时监视文件。它会告诉你问题是什么。您可以自己解决这个问题,也可以使用标签返回到 stackoverflow 解决这个ffmpeg 特定的问题ffmpeg

回答by Sandor Marton

The answer is to add '-nostdin' to ffmpeg (wouldn't answer to an old question like this, but still tops on related Google searches).

答案是在 ffmpeg 中添加 '-nostdin'(不会回答这样的老问题,但仍然在相关的 Google 搜索中名列前茅)。