我正在尝试在我的服务器上编码许多视频,但 FFMPEG 是资源密集型的,所以我想设置某种形式的队列。我网站的其余部分使用 PHP,但我不知道是否应该使用 PHP、Python、BASH 等。我想我可能需要使用 CRON,但我不确定如何告诉 ffmpeg 启动一个新任务(从列表中)完成之前的任务后。
问问题
7146 次
2 回答
8
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 ffmpeg
commands to this script :
#!/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 ffmpeg
commands to the named pipe by using the syntax :
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.
于 2012-10-02T21:05:26.343 回答
1
谢谢你。正是应用这种技术来创建一个 ffmpeg 队列。不过我做了1个小改动。由于某种原因,此队列仅适用于 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
只是想我应该分享这个以备将来使用。
于 2015-05-01T12:57:36.980 回答