HOWTO: compiling ffmpeg + x264 + MP3 + Xvid + AMR on Ubuntu

ffmpeg is THE audio/video conversion tool. Unfortunately, the default build included in Ubuntu is usually quite outdated, as well as lacking support for many codecs.
The purpose of this article is to show you how you can build a fresh, up to date version of ffmpeg supporting (almost) all codecs. This procedure was successfully performed on Ubuntu 8.04 and 8.10.
0) Prerequisites
Before we start, let's check if subversion and git are installed. We'll need both to install some of the libraries required by ffmpeg:
ubuntu% svn
Type 'svn help' for usage.
ubuntu% git
usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate|--no-pager] [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE] [--help] COMMAND [ARGS]
OK, they're both present. If not, you need to install them with the following commands:
ubuntu% sudo apt-get install subversion
ubuntu% sudo apt-get install git git-core
Now would be a good time to decide where you're going to build all those sources. Just create a temporary directory anywhere you like (you'll need less than 150MB).
[Updated on 2008/01/02] If you have an existing installation of ffmpeg, you may run into linking issues caused by conflicting library versions. My advice is to remove all existing copies of libav* (libavcodec and so on) which may be present in /usr/lib, either by uninstalling them with APT or by deleting the .a and .so files. Please read the comments below for additional information.
1) Fetching the ffmpeg sources
First of all, let's get the latest ffmpeg source snapshot from the Subversion server:

ubuntu% svn checkout svn://svn.ffmpeg.org/ffmpeg/trunk ffmpeg
lots of output removed
Checked out external at revision 28172.
Checked out revision 16245.

Of course, you could just go ahead with configure, make, make install and be done with it. Unfortunately (?), it's not that simple. Go ahead, run configure:

ubuntu% cd ffmpeg
ubuntu% ./configure
--prefix=/usr/local
lots of output removed
Creating config.mak and config.h...

Take a closer look at the output, especially at the 'Enabled encoders' section. A number of major formats, like AAC, MP3, x.264 or XViD are missing. Can you live without these? Probably not...
Why, oh why are they missing? Take another look at the output of the configure command:
libfaac enabled no
libmp3lame enabled no
libx264 enabled no
libxvid enabled no

These encoders are missing because they're handled by external libraries which are not part of the ffmpeg source package. Chasing them all is a bit of a pain in the #$$ and hopefully this article will help!
2) Configuring ffmpeg... and failing
Let's go wild and enable almost everything, including shared libraries (nice if you're running multiple copies of ffmpeg) and POSIX threads (additional parallelism can't hurt):
ubuntu% ./configure --prefix=/usr/local --enable-gpl --enable-nonfree --enable-shared --enable-postproc --enable-avfilter --enable-avfilter-lavf --enable-pthreads --enable-x11grab --enable-bzlib --enable-libamr-nb --enable-libamr-wb --enable-libdc1394 --enable-libfaac --enable-libfaad --enable-libfaadbin --enable-libgsm --enable-libmp3lame --enable-libnut --enable-libschroedinger --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-zlib

It will inevitably lead to something like this:
FAAD test failed.
If you think configure made a mistake, make sure you are using the latest
version from SVN. If the latest version fails, report the problem to the
ffmpeg-user@mplayerhq.hu mailing list or IRC #ffmpeg on irc.freenode.net.
Include the log file "config.err" produced by configure as this will help
solving the problem.

It's normal, we haven't installed the external libraries required for our ffmpeg build. Let's get to it!
3) Installing libamr
This library is needed for 3GPP speech codecs. For legal reasons, it is not part of the standard Ubuntu repository. You can find it in the Medibuntu repository. Of course, you need to let APT know about this new repository. These are the commands for Ubuntu 8.04 (more information on other versions here):

ubuntu% sudo wget http://www.medibuntu.org/sources.list.d/hardy.list --output-document=/etc/apt/sources.list.d/medibuntu.list
ubuntu% sudo apt-get update && sudo apt-get install medibuntu-keyring && sudo apt-get update
Now you can install libamr:
ubuntu% sudo apt-get install libamrnb-dev libamrwb-dev
4) Installing libnut

NUT is a container format under construction by MPlayer and FFmpeg developers. Libnut needs to be built from source:
ubuntu% svn co svn://svn.mplayerhq.hu/nut/src/trunk/ nut
ubuntu% cd nut
ubuntu% make
ubuntu% sudo make install

5) Installing libx264
x264 is a free library for encoding H264/AVC video streams.
It can be fetched with APT using 'apt-get install libx264-dev' but let's make sure we have both the latest ffmpeg and the latest x264.
Before we build the x264 source, we need to install

  • libgpac, required to support the mp4 container with the x264 codec,
  • the yasm assembler, required to compile several assembly language routines present in the x264 code.
Installing libgpac is straightforward:
ubuntu% sudo apt-get install libgpac-dev
Now, let's take a look at yasm:
ubuntu% yasm --version
zsh: command not found: yasm
It's not there. Let's get it using APT:
ubuntu% sudo apt-get install yasm
ubuntu% yasm --version
yasm 0.5.0.1591

OK, now let fetch the x264 source and configure the build:

ubuntu% git clone git://git.videolan.org/x264.git
ubuntu% cd x264
ubuntu% ./configure --prefix=/usr/local --enable-shared
Found yasm 0.5.0.1591
Minimum version is yasm-0.6.1
If you really want to compile without asm, configure with --disable-asm.

Hmmm.. Do we want to use generic C routines instead of optimized assembly? No. Let's build the latest yasm (0.7.2 at the time of this writing):
ubuntu% sudo apt-get remove yasm
ubuntu% wget http://www.tortall.net/projects/yasm/releases/yasm-0.7.2.tar.gz
ubuntu% tar xvfz yasm-0.7.2.tar.gz
ubuntu% cd yasm-0.7.2
ubuntu% ./configure --prefix=/usr/local
ubuntu% make
ubuntu% sudo make install
ubuntu% yasm --version
yasm 0.7.2.2153

OK, now we can build x264:

ubuntu% cd x264
ubuntu% ./configure --prefix=/usr/local --enable-shared
output removed
Platform: X86
System: LINUX
asm: yes
avis input: no
mp4 output: yes
pthread: yes
debug: no
gprof: no
PIC: no
shared: yes
visualize: no
ubuntu% make
ubuntu% sudo make install

6) Installing libxvid
Before we install libxvid, we need to check that the nasm assembler is OK. It's required to build assembly code inlibxvid and you do NOT want this code to be replaced with generic C code in case nasm is missing : I ran some Xvidencoding tests with and without assembly code and there's a 2.5x factor... So read on :)
You need at least nasm 2.0, so let's check the default version on Ubuntu 8.04 and Ubuntu 8.10:

ubuntu8.04% sudo apt-get install nasm
ubuntu8.04% nasm -v
NASM version 0.99.06-20071101 compiled on Sep 16 2008
ubuntu8.10% sudo apt-get install nasm
ubuntu8.10% nasm -v
NASM version 2.03.01 compiled on Jun 19 2008

So, if you have 8.10, you're good to go and you can skip the rest of this section. If you have 8.04, follow me:
ubuntu% sudo apt-get remove nasm
ubuntu% wget http://www.nasm.us/pub/nasm/releasebuilds/2.05.01/nasm-2.05.01.tar.gz
ubuntu% tar xvfz nasm-2.05.01.tar.gz
ubuntu% cd nasm-2.05.01
ubuntu% ./configure --prefix=/usr/local
ubuntu% make
ubuntu% sudo make install
ubuntu% nasm -v
NASM version 2.05.01 compiled on Dec 23 2008

Now, let's fetch the xvid sources and build them:

ubuntu% wget http://downloads.xvid.org/downloads/xvidcore-1.2.1.tar.gz
ubuntu% tar xvfz xvidcore-1.2.1.tar.gz
ubuntu% cd xvidcore/build/generic
ubuntu% ./configure --prefix=/usr/local
ubuntu% make
lots of output removed
---------------------------------------------------------------
Xvid has been successfully built.
* Binaries are currently located in the '=build' directory
* To install them on your system, you can run '# make install'
as root.
---------------------------------------------------------------

ubuntu% sudo make install
7) Installing everything else
All the other libraries we need are part of the standard Ubuntu repository. Let's install them all with a single command:

ubuntu% sudo apt-get install
libfaac-dev libfaad-dev libschroedinger-dev libtheora-dev libvorbis-dev libxv-dev libxvmc-dev

We also need to install the LAME MP3 encoder. The name of the library differs on Ubuntu 8.04 and Ubunto 8.10, so choose wisely :)

ubuntu8.04% sudo apt-get
install liblame-dev
ubuntu8.10% sudo apt-get install libmp3lame-dev
8) Configuring ffmpeg... and succeeding!

We should have everything we need now. Let's try that configure command again:
[Updated on 2009/02/18 : the '--enable-xvmc' flag has been removed. XVMC support now seems to be integrated by default. If you're using an old build, please add the flag to the command line below]
[Updated on 2009/03/09 : the '--enable-swscale' flag has been removed. This library is now built by default. If you're using an old build, please add the flag to the command line below]
ubuntu% cd ffmpeg
ubuntu% ./configure --prefix=/usr/local --enable-gpl --enable-nonfree --enable-shared --enable-postproc --enable-avfilter --enable-avfilter-lavf --enable-pthreads --enable-x11grab --enable-bzlib --enable-libamr-nb --enable-libamr-wb --enable-libdc1394 --enable-libfaac --enable-libfaad --enable-libfaadbin --enable-libgsm --enable-libmp3lame --enable-libnut --enable-libschroedinger --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-zlib
lots of output removed
License: unredistributable
Creating config.mak and config.h...
All right. Let's build it.

9) Building ffmpeg
That's the easiest bit!
ubuntu% make
LOTS of output removed
ranlib libavutil/libavutil.a
rm doc/ffserver.pod doc/ffmpeg.pod doc/ffplay.pod
ubuntu% sudo make install
That's it. Cool, huh? Before you can start playing with your ffmpeg binary, you need to register those new dynamic libraries in /usr/local/lib (I'm using zsh. the syntax may be different if you're using another shell):
ubuntu% LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
ubuntu% export LD_LIBRARY_PATH
ubuntu% sudo ldconfig
Now, let's check this new ffmpeg:
ubuntu% which ffmpeg
/usr/local/bin/ffmpeg
ubuntu% ffmpeg -formats
lots of output removed
Pretty good list of codecs, isn't it?
10) Cleaning up

If like me you keep building the latest version, you will eventually end up with a lot of unecessary libraries in/usr/local/lib, e.g.:
ubuntu% cd /usr/local/lib
ubuntu% ls -l libav*
-rw-r--r-- 1 root root 24108334 2009-02-20 11:15 libavcodec.a
lrwxrwxrwx 1 root root 21 2009-02-20 11:15 libavcodec.so -> libavcodec.so.52.15.0
lrwxrwxrwx 1 root root 21 2009-02-20 11:15 libavcodec.so.52 -> libavcodec.so.52.15.0
-rwxr-xr-x 1 root root 5791060 2009-02-04 11:34 libavcodec.so.52.11.0
-rwxr-xr-x 1 root root 5791192 2009-02-05 15:29 libavcodec.so.52.12.0
-rwxr-xr-x 1 root root 5791228 2009-02-10 17:46 libavcodec.so.52.14.0
-rwxr-xr-x 1 root root 5774920 2009-02-20 11:15 libavcodec.so.52.15.0
-rw-r--r-- 1 root root 467478 2009-02-20 11:15 libavdevice.a
lrwxrwxrwx 1 root root 21 2009-02-20 11:15 libavdevice.so -> libavdevice.so.52.1.0
lrwxrwxrwx 1 root root 21 2009-02-20 11:15 libavdevice.so.52 -> libavdevice.so.52.1.0
-rwxr-xr-x 1 root root 39492 2009-02-20 11:15 libavdevice.so.52.1.0
-rw-r--r-- 1 root root 63220 2009-02-20 11:15 libavfilter.a
lrwxrwxrwx 1 root root 20 2009-02-20 11:15 libavfilter.so -> libavfilter.so.0.3.0
lrwxrwxrwx 1 root root 20 2009-02-20 11:15 libavfilter.so.0 -> libavfilter.so.0.3.0
-rwxr-xr-x 1 root root 17876 2009-02-20 11:15 libavfilter.so.0.3.0
-rw-r--r-- 1 root root 6259158 2009-02-20 11:15 libavformat.a
lrwxrwxrwx 1 root root 22 2009-02-20 11:15 libavformat.so -> libavformat.so.52.29.0
lrwxrwxrwx 1 root root 22 2009-02-20 11:15 libavformat.so.52 -> libavformat.so.52.29.0
-rwxr-xr-x 1 root root 766736 2009-02-05 15:29 libavformat.so.52.25.0
-rwxr-xr-x 1 root root 770972 2009-02-10 17:46 libavformat.so.52.26.0
-rwxr-xr-x 1 root root 771072 2009-02-12 16:57 libavformat.so.52.27.0
-rwxr-xr-x 1 root root 775232 2009-02-13 11:22 libavformat.so.52.28.0
-rwxr-xr-x 1 root root 767108 2009-02-20 11:15 libavformat.so.52.29.0
-rw-r--r-- 1 root root 225672 2009-02-20 11:15 libavutil.a
lrwxrwxrwx 1 root root 20 2009-02-20 11:15 libavutil.so -> libavutil.so.49.14.0
lrwxrwxrwx 1 root root 20 2009-02-20 11:15 libavutil.so.49 -> libavutil.so.49.14.0
-rwxr-xr-x 1 root root 55508 2009-02-20 11:15 libavutil.so.49.14.0
There's nothing really wrong here, but for the sake of clarity, you may want to remove the old libraries, i.e. the ones NOT linked as lib*.so.
That's it for today :)
Related Posts by Categories

ffmpeg

open source

howto

x264

Written by: Julien Simon at 3:45 PM

Tags: ffmpeg, howto, open source, x264