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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With