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.
2 Answers
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.

- 173,512
- 41
- 224
- 223
-
Yes but my videos will be getting added dynamically. So I don't want to hand convert them. When a new video is downloaded on my server, I want it to add the video to the queue – Travis Peacock Oct 03 '12 at 09:30
-
The POST is rewritten from scratch to fit your needs. – Gilles Quénot Oct 03 '12 at 11:58
-
Beautiful I will try this when I get home – Travis Peacock Oct 03 '12 at 16:45
-
I can't upvote it because I am too new. I did, however, accept it! Thank you. – Travis Peacock Oct 09 '12 at 13:44
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.
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
Just thought I should share this for any possible future use of this.