3

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.

Will Palmer
  • 5,742
  • 1
  • 26
  • 33
Travis Peacock
  • 143
  • 1
  • 5

2 Answers2

9

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.

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
1

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.

Community
  • 1
  • 1
Joost
  • 39
  • 1
  • 5