A time may come when you need to ‘code’ video conversions or maybe you’ve just fallen in love with Terminal.

Either way, this post should guide you through the installation, setup and usage of ffmpeg and libx264 in CentOS.

I was working on a project where I had to create videos from image sequences. I decided to use H.264/AVC as I wanted the app to be cross browser compliant.

Get FFmpeg, x264, WebM

I chose not to yum install since I wanted the latest versions. You can get ffmpeg  here.
tar.gz for ffmpeg 0.10 – download.
FFmpeg can encode/decode almost all formats with the built-in libraries. However, external libraries do need to be installed for encoding .mp4 or .mov in h.264. VideoLAN provides the library libx264 (x264 is the application).
x264 can also be used as a standalone command for encoding videos. Using h.264 does come under licensing; something that many people aren’t aware of. I will discuss this more later.
tar.bz2 for x264 – download.
If you want to use .webm or .vp8, you will need the libvpx library. Webm is open and supported on all browsers.
tar.bz2 for libvpx – download
Now that you have all the required files, we’ll take a look at configuring and installing them.

Configure and Install

Let’s talk about dependencies first. FFmpeg requires YASM for compiling. Configuring ffmpeg without yasm results in a ‘crippled install’.
Download yasm using
yum install yasm
The external libraries need to installed first.
Go to the x264 / vpx folder
cd /path/to/folder
./configure
make
sudo make install
If any dependency errors are shown, install those using yum install.
Go to the ffmpeg folder
cd /path/to/folder
./configure –enable-gpl –enable-libx264 –enable-libvpx
make
sudo make install
FFmpeg generally installs in /usr/local/lib. Please make sure that the libx264.so.120 (or similar) and libvpx.so.1 are in the same directory, or both should be in /usr/lib. This will resolve errors where ffmpeg throws ‘libx264 not found’ or similar errors.
It is important to use the latest version of libx264 library as ffmpeg will not configure for an old version.
To check if its installed, run the command ffmpeg. If everything is setup well, you should see the versions and other information displayed.
To check which formats are supported, run ffmpeg -formats
To check the codecs, run ffmpeg -codecs. This also displays whether encoding, decoding or both are enabled.
Also note, that H.264 and libx264 is not the same in FFmpeg codecs list. H.264 encodes the raw video.

Conversion Examples

Let’s look at examples for conversions. Depending on your requirement, certain encoding settings can be specified. I will try to give an overview of all types.

.mov to .mp4 (simple)
ffmpeg -i inputfile.mov outputfile.mp4
.mov to .mp4 where outfile can be replaced if it exists
ffmpeg -i inputfile.mov -y outputfile.mp4
.mov to h.264 .mp4 (simple)
ffmpeg -i inputfile.mov -vcodec libx264 outputfile.mp4
.mov to h.264 .mp4 (extended)
ffmpeg -i inputfile.mov -an -r 24 -vcodec libx264 -vpre baseline -crf 22 -s 1024×576 -y outputfile.mp4
Image sequences to video – ffmpeg requires images to be in a sequence like img01,img02 and so on. If your images are named differently or start from a different numbering, it would be good practice to create symbolic links. These can be deleted once the encoding is done. Most image formats such as jpg, png, tiff, tga, dpx are supported.
i=1; for f in *.jpg; do count=$(printf %03d $i); ln -s “$f” imglink”$count”.jpg; i=$(($i+1)); done
ffmpeg -f image2 -i imglink%03d.jpg -an -r 24 -vcodec libx264 -crf 22 -y outputfile.mp4
rm -f imglink*.jpg
OpenEXR to video – ffmpeg doesn’t read OpenEXR, so a solution is to convert the.exr to a raw yuv file and then use ffmpeg to convert it to .mp4 To avoid creating a temporary file, we use a named pipe so that the video encoding is done on the fly.
mkfifo tempoutput.yuv
convert inputfile.exr tempoutput.yuv &
ffmpeg  -i tempoutput.yuv -pix_fmt yuv420p -vcodec libx264 outputfile.mp4
Explanation of ffmpeg options
-i input file
-y create output file and replace if it already exists (no prompt)
-s resolution
-r frame rate
-acodec audio codec to encode in
-vcodec video codec to embed in
-an ignore audio
-crf used for x264. values should vary from 15 to 22. It keeps the quality constant and calculates bitrate based on complexity of the frame. 15 gives better quality = higher bitrate = more time. Remember, file size=bitrate * video duration.
-f format
-b:a audio bitrate
-b:v video bitrate
-vpre x264 levels and profiles. More information on these here.

Hope you found this useful. If you come across any problems and have suggestions, please comment below. I am by no means an expert but I will try to solve it.
Related Posts Plugin for WordPress, Blogger...
  • peterparamo

    I don’t have a vpx folder in my x264 folder?

    • http://www.xuvious.com/ Xuvious

      Are you attempting to install the x264 lib or ffmpeg?
      vpx is a separate codec, it wouldn’t be inside the x264 folder.

      Try updating libavutil with yum or apt-get

      • peterparamo

        I am attempting to install x264.  I am following the directions above which say “Go to the x264 / vpx folder”??

        • http://www.xuvious.com/ Xuvious

          Like I said, try updating libavutil. Once that’s done you can try configuring again.

          yum update libavutil