如何设置ffmpeg队列?

时间:2012-10-02 20:43:48

标签: bash cron ffmpeg queue batch-processing

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

2 个答案:

答案 0 :(得分:7)

我们将在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

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

echo "ffmpeg -version" > /tmp/ffmpeg

错误检查:

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

他们将自动排队。

答案 1 :(得分:1)

谢谢你。正是应用这种技术来创建一个ffmpeg队列。 我做了一个小改动。由于某种原因,此队列仅适用于2个项目。我只能在第一个项目结束时发送第三个项目。

我相应地修改了脚本:

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

我的基础是: https://stackoverflow.com/questions/15376562/cant-write-to-named-pipe

我想我应该分享这一点,以便将来可以使用它。