How to compile ffmpeg from source

ffmpeg is a free and very powerful video encoding tool that is the basis of many other popular video encoding tools, with a command line interface.

Because of the frequent updates to ffmpeg and the codecs it uses, it is often best to download the latest source and compile it yourself. These updates usually include new features, bug fixes, support for new codecs and noticeable speed improvements. Most Linux distributions have outdated packages in their repositories so being able to compile the latest code from source is very useful

I use ffmpeg to output H.264 video with AAC audio in a MP4 container. I also occasionally convert audio to mp3. This means I will be compiling the following:

Preparing to compile on Linux

(If you already compile things on Linux, skip to the next section.)
First make sure that you have all of the tools and headers required to compile on Linux. On Debian based systems (including Ubuntu) you can usually run the following as root to get everything you need. First update your system. It is important that you get the latest kernel and the correct sources for it.

apt-get update
apt-get dist-upgrade

A reboot may be required here if your kernel updated.

Now get the compilers, tools and headers needed to compile software on Linux:

apt-get install build-essential linux-headers-`uname -r` git subversion yasm

NB: your “git” package may be called something like “git-core”

Compile ffmpeg (empty)

Because some libraries require ffmpeg to be installed, we will first install a blank copy with no additional codecs specified.

When I update, I often find that I need to recompile a blank version of ffmpeg before I can compile x264.

Download the latest ffmpeg source with subversion, configure, compile:

svn checkout svn://svn.ffmpeg.org/ffmpeg/trunk ffmpeg
cd ffmpeg
./configure
make

If this all worked with no errors (you can check with echo $?), run

make install

as root. Use su or sudo if you need to. I use su.

Compile faac and faad

Download and extract faac and faad from audiocoding.com.

As above, run

./configure
make
su -c 'make install'

on both libraries.

You can optionally do the same thing with LAME if you want to be able to encode mp3 audio.

NB: if you get a strcasestr error compiling faac, just comment it out in faac_1.28/common/mp4v2/mpeg4ip.h

Compile x264

Download and compile the latest x264 source with git:

git clone git://git.videolan.org/x264.git
cd x264
./configure --enable-static --enable-shared
make
su -c 'make install'

Compile ffmpeg with the above codecs

Now that we have all of the codecs (x264, aac, mp3) successfully installed as libraries on the system, we can reinstall ffmpeg to incorporate these libraries. First go back to the ffmpeg source directory and clean off the previous compile.

cd ffmpeg
make clean

Now configure to enable all of the installed libraries and recompile:

./configure --enable-gpl --enable-nonfree --enable-pthreads --enable-libx264 --enable-libfaac --enable-libfaad --enable-libmp3lame
make

(if it tells you that –enable-libfaad is not an option, just remove it. It’s probably either been rolled into faac or ffmpeg has it built in)

If it all worked, run make install as root.

su -c 'make install'

Testing

As long as /usr/local/bin/ is on your $PATH environment variable, you should be able to just type ffmpeg to check that it is all installed. It should look something like this:

$ ffmpeg
FFmpeg version SVN-r23548, Copyright (c) 2000-2010 the FFmpeg developers
  built on Jun  9 2010 13:35:17 with gcc 4.4.4
  configuration: --enable-gpl --enable-nonfree --enable-pthreads --enable-libx264 --enable-libfaac --enable-libfaad --enable-libmp3lame
  libavutil     50.18. 0 / 50.18. 0
  libavcodec    52.75. 1 / 52.75. 1
  libavformat   52.68. 0 / 52.68. 0
  libavdevice   52. 2. 0 / 52. 2. 0
  libavfilter    1.20. 0 /  1.20. 0
  libswscale     0.11. 0 /  0.11. 0
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
 
Use -h to get full help or, even better, run 'man ffmpeg'

Using ffmpeg

I have written a small Python video encoding script to make encoding as easy as

enc -t cd file.vob

If anyone has any suggestions to improve this, please feel free to post them in the comments.

Tags: , ,

3 Responses to “How to compile ffmpeg from source”

  1. Alan says:

    What you have written here is very straightforward and seems simple, but I get an error stating that libx264 is not found. I have done some searching on Google, but nowhere have I yet been able to find a straight answer to where ffmpeg is looking for these libraries. I have compiled libx264 as shared, as static, and either way I always get the error from ffmpeg that libx264 is not found.

    Can anyone tell me where the ffmpeg configure script expects to find the library, and whether it needs to be static or shared?

  2. George Helyar says:

    For static vs shared, you set the option in both ffmpeg and x264 with a configure argument.

    For finding libx264, the location depends on your operating system. It’s likely that your system is looking in /usr/lib but not /usr/local/lib (locally compiled libraries).

    From the Ubuntu forums (as an example) “Add /usr/local/lib to /etc/ld.so.conf then run ldconfig”

    Also make sure you’re running “make install” as super user and that it’s running correctly. You can check its exit status by running

    echo $?
  3. Benjamin says:

    Thank you very much for this well-written tutorial. I have been looking for something like this for a long time.

Leave a Reply