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;
Now we want to get and install yasm;
tar xzvf yasm-1.2.0.tar.gz
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;
git clone git://git.videolan.org/x264
And now the VisualOn AAC audio encoder;
tar xzvf vo-aacenc-0.1.2.tar.gz
We will be using ffmpeg to perform the conversions, let's get that set up;
tar xzvf ffmpeg-0.9.2.tar.gz
./configure --enable-gpl --enable-libvo-aacenc --enable-libx264 --enable-version3
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;
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;
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;
rpm -ivh libzen0-0.4.26-1.i386.CentOS_5.rpm
rpm -ivh libmediainfo0-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.