Setting up ffmpeg on your server for php video conversion

I needed to setup a site similar to youtube, that would convert uploaded videos to web-friendly versions for viewing. Installing the software turned out to be quite straight forward, once I knew what was needed. But there were a few gotcha's along the way. Hopefully this post will help anybody else trying to setup an online video converter.

The main objective is to setup ffmpeg on the server so that you can send commands from your language of choice. This is what I did to setup ffmpeg and related software on a CentOS 5 server. If you're on a newer version of CentOS, some of the commands below will no longer apply and some of the software is now included in the standard repos.

First we need to install/update what we need to use to grab and install the software;

Note: I did all the following commands on an old CentOS 5 system, which is about to hit its end of life. It is always best to find the latest versions of all of the software you install from the relevant supplier.


yum install gcc git make nasm pkgconfig wget

Note: If you have issues installing git, you will need to setup the epel repo on your system. As this differs from OS versions, I will not go into detail here.

Let's make a directory for all of our source files and CD into it;


mkdir ~/ffmpeg-source
cd ~/ffmpeg-source

Now we want to get and install yasm;


wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
tar xzvf yasm-1.2.0.tar.gz
cd yasm-1.2.0
./configure
make
make install

I wanted the system to convert what I upload into mp4's using the H.264 video codec and the AAC audio codec. These are the most popular codecs to use for web videos, but there are other options.

Let's install the H.264 encoder from VLC;


cd ~/ffmpeg-source
git clone git://git.videolan.org/x264
cd x264
./configure --enable-static
make
make install

And now the VisualOn AAC audio encoder;


cd ~/ffmpeg-source
wget http://downloads.sourceforge.net/opencore-amr/vo-aacenc-0.1.2.tar.gz
tar xzvf vo-aacenc-0.1.2.tar.gz
cd vo-aacenc-0.1.2
./configure --disable-shared
make
make install

We will be using ffmpeg to perform the conversions, let's get that set up;


cd ~/ffmpeg-source
wget http://ffmpeg.org/releases/ffmpeg-0.9.2.tar.gz
tar xzvf ffmpeg-0.9.2.tar.gz
cd ffmpeg-0.9.2
./configure --enable-gpl --enable-libvo-aacenc --enable-libx264 --enable-version3
make
make install

Note: Running make will most likely take a very long time to complete.

If you get a problem regarding executing files in tmp try editing the configure file, searching for "TMPDIR" and commenting out the following lines;


: ${TMPDIR:=$TEMPDIR}
: ${TMPDIR:=$TMP}
: ${TMPDIR:=/tmp}

While we're in the ffmpeg folder we also want to add qt-faststart so that our encoded mp4 files have their meta data at the beginning of the file and can be streamed;


make tools/qt-faststart
cp -a tools/qt-faststart /usr/local/bin/

Finally, you will need to install the mediainfo CLI for your version of CentOS. This will enable us to detect iPhone portrait videos and other useful information;


cd ~/ffmpeg-source
wget http://downloads.sourceforge.net/zenlib/libzen0-0.4.26-1.i386.CentOS_5.rpm
rpm -ivh libzen0-0.4.26-1.i386.CentOS_5.rpm
wget http://downloads.sourceforge.net/mediainfo/libmediainfo0-0.7.58-1.i386.CentOS_5.rpm
rpm -ivh libmediainfo0-0.7.58-1.i386.CentOS_5.rpm
wget http://downloads.sourceforge.net/mediainfo/mediainfo-0.7.58-1.i386.CentOS_5.rpm
rpm -ivh mediainfo-0.7.58-1.i386.CentOS_5.rpm

All done, you should now be able to execute ffmpeg commands from the CLI. Like so;


/usr/local/bin/ffmpeg -i FILE_TO_CONVERT_HERE -vcodec libx264 -vprofile high -preset slow -b:v 500k -maxrate 500k -bufsize 1000k -s 640x360 -threads 0 -acodec libvo_aacenc -b:a 128k OUTPUT_FILENAME_HERE

In my next post I will run through the process of using ffmpeg to create a video upload and conversion tool in PHP.