bash 如何设置 ffmpeg 队列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12698006/
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:26:15  来源:igfitidea点击:

How do I set up an ffmpeg queue?

bashcronffmpegqueuebatch-processing

提问by Travis McCrea

I am trying to encode many videos on my server, but FFMPEG is resource intensive so I would like to setup some form of queueing. The rest of my site is using PHP, but I don't know if I should use PHP, Python, BASH, etc. I was thinking I might need to use CRON but I am not really sure exactly how to tell ffmpeg to start a new task (from the list) after it finishes the one before it.

我试图在我的服务器上编码许多视频,但 FFMPEG 是资源密集型的,所以我想设置某种形式的排队。我网站的其余部分正在使用 PHP,但我不知道我是否应该使用 PHP、Python、BASH 等。我想我可能需要使用 CRON,但我不确定如何告诉 ffmpeg 启动一个完成前一个任务后的新任务(来自列表)。

回答by Gilles Quenot

We will use FIFO(First In First Out) in a bash script. The script needs to run before cron(or any script, any terminal that call the FIFO) to send ffmpegcommands to this script :

我们将在 bash 脚本中使用FIFO(先进先出)。该脚本需要在cron(或任何脚本,调用 的任何终端FIFO)之前运行ffmpeg以向该脚本发送命令:

#!/bin/bash

pipe=/tmp/ffmpeg

trap "rm -f $pipe" EXIT

# creating the FIFO    
[[ -p $pipe ]] || mkfifo $pipe

while true; do
    # can't just use "while read line" if we 
    # want this script to continue running.
    read line < $pipe

    # now implementing a bit of security,
    # feel free to improve it.
    # we ensure that the command is a ffmpeg one.
    [[ $line =~ ^ffmpeg ]] && bash <<< "$line"
done

Now (when the script is running), we can send any ffmpegcommands to the named pipe by using the syntax :

现在(当脚本运行时),我们可以ffmpeg使用以下语法向命名管道发送任何命令:

echo "ffmpeg -version" > /tmp/ffmpeg

And with error checking:

并带有错误检查:

if [[ -p /tmp/ffmpeg ]]; then
    echo "ffmpeg -version" > /tmp/ffmpeg
else
    echo >&2 "ffmpeg FIFO isn't open :/"
fi

They will be queuing automatically.

他们会自动排队。

回答by Joost

Thanks for this. Applied exactly this technique to create a ffmpeg queue. I made 1 small change though. For some reason this queue only worked for 2 items. I could only send a third item when the first item was finished.

谢谢你。正是应用这种技术来创建一个 ffmpeg 队列。不过我做了 1 个小改动。出于某种原因,此队列仅适用于 2 个项目。当第一件物品完成时,我只能发送第三件物品。

I modified the script accordingly:

我相应地修改了脚本:

while true; do

# added tweak to fix hang
exec 3<> $pipe

# can't just use "while read line" if we 
# want this script to continue running.
read line < $pipe

I based this on: https://stackoverflow.com/questions/15376562/cant-write-to-named-pipe

我基于此:https: //stackoverflow.com/questions/15376562/cant-write-to-named-pipe

Just thought I should share this for any possible future use of this.

只是想我应该分享这个以供将来可能使用它。