Aug 21, 2013

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

By popular demand (according to page view stats), I've decided to refresh the Dec'2008 post ("HOWTO: compiling ffmpeg + x264 + MP3 + Xvid + AMR on Ubuntu 8.x") for Ubuntu 12.04. Enjoy :)

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 major codecs. This procedure was successfully performed on a vanilla Ubuntu 12.04 system.


0) Prerequisites

First, we need to enable the 'multiverse" repository in /etc/apt/sources.list, because that's where some of our external libraries live. All you have to do is
uncomment the line similar to:
add deb http://eu-west-1.ec2.archive.ubuntu.com/ubuntu/ precise multiverse sudo apt-get update

Don't forget to refresh the repos:
$ sudo apt-get update

Then, we need to make sure that git and svn are installed. We'll need them to fetch source code.
$ sudo apt-get install git subversion

Let's also add some mandatory build tools:
$ sudo apt-get install build-essential nasm pkg-config 

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 about 150MB).

The x264 build requires a recent version of the yasm assembler. Let's take care of it:
$ sudo apt-get remove yasm 
$ wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz 
$ tar xvfz yasm-1.2.0.tar.gz 
$ cd yasm-1.2.0 
$ ./configure --prefix=/usr/local 
$ make 
$ sudo make install 
$ yasm --version
yasm 1.2.0 Compiled on Aug 21 2013. 

Done! One final note before we start building: 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.


1) Fetching the ffmpeg sources

First of all, let's get the latest ffmpeg source:
$ git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg

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

$ cd ffmpeg
$./configure 
--prefix=/usr/local
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 these additional encoders: AAC, MP3, Theora, Vorbis, x264, xvid, AMR, GSM. Let's also build shared libraries, which are useful if you're running multiple copies of ffmpeg:

./configure --prefix=/usr/local --enable-gpl --enable-version3 --enable-nonfree --enable-shared --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libfaac --enable-libgsm --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid 

It will inevitably lead to something like:
ERROR: libfaac not found

That's ok, we haven't installed the external libraries required by our ffmpeg build. Let's get to it!

3) Installing AAC, MP3, Theora, Vorbis, AMR, GSM

These are readily available in the Ubuntu repository, let's add them:
$ sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev libvorbis-dev libopencore-amrnb-dev libopencore-amrwb-dev libgsm1-dev zlib1g-dev libgpac1-dev

4) Installing libx264

x264 is a free library for encoding H264/AVC video streams. It can be installed with APT using 'apt-get install libx264-dev' but let's make sure we have both the latest ffmpeg and the latest x264.

So, let's fetch the x264 sources and build them:
git clone git://git.videolan.org/x264.git
$ cd x264

$ ./configure --prefix=/usr/local --enable-shared
make
$ sudo make install


5) Installing libxvid

Let's fetch the xvid sources and build them:
$ wget http://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz 
$ tar xvfz xvidcore-1.3.2.tar.gz 
$ cd xvidcore-1.3.2/build/generic 
$ ./configure --prefix=/usr/local 
$ make
$ sudo make install

6) Configuring ffmpeg... and succeeding!

We should have everything we need now. Let's try that configure command again:
./configure --prefix=/usr/local --enable-gpl --enable-version3 --enable-nonfree --enable-shared --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libfaac --enable-libgsm --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid
output removed
Creating config.mak, config.h, and doc/config.texi...

All right, let's build it. Time for coffee(s), this is a rather long build:
$ make
$ sudo make install
$ sudo ldconfig -v

Cool, huh? Now, let's check this new ffmpeg:
which ffmpeg
/usr/local/bin/ffmpeg

$ ffmpeg -encoders|grep -E "mp3|xvid|aac|gsm|amr|x264|theora|vorbis"
ffmpeg version N-55691-gca7f637 Copyright (c) 2000-2013 the FFmpeg developers
  built on Aug 21 2013 15:44:17 with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)
  configuration: --prefix=/usr/local --enable-gpl --enable-version3 --enable-nonfree --enable-shared --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libfaac --enable-libgsm --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid
  libavutil      52. 42.100 / 52. 42.100
  libavcodec     55. 29.100 / 55. 29.100
  libavformat    55. 14.100 / 55. 14.100
  libavdevice    55.  3.100 / 55.  3.100
  libavfilter     3. 82.100 /  3. 82.100
  libswscale      2.  5.100 /  2.  5.100
  libswresample   0. 17.103 /  0. 17.103
  libpostproc    52.  3.100 / 52.  3.100
 V..... libx264              libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (codec h264)
 V..... libx264rgb           libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 RGB (codec h264)
 V..... libxvid              libxvidcore MPEG-4 part 2 (codec mpeg4)
 V..... libtheora            libtheora Theora (codec theora)
 A..X.. aac                  AAC (Advanced Audio Coding)
 A..... libfaac              libfaac AAC (Advanced Audio Coding) (codec aac)
 A..... libopencore_amrnb    OpenCORE AMR-NB (Adaptive Multi-Rate Narrow-Band) (codec amr_nb)
 A..... libgsm               libgsm GSM (codec gsm)
 A..... libgsm_ms            libgsm GSM Microsoft variant (codec gsm_ms)
 A..... libmp3lame           libmp3lame MP3 (MPEG audio layer 3) (codec mp3)
 A..X.. vorbis               Vorbis
 A..... libvorbis            libvorbis (codec vorbis)

Congratulations, your brand new ffmpeg can now encode AAC, MP3, Theora, Vorbis, x264, xvid, AMR and GSM :)

7) Cleaning up (optional)
If like me you keep building the latest version, you will eventually end up with a lot of unnecessary libraries in /usr/local/lib. There's nothing really wrong with old versions, but for the sake of clarity, you may want to remove the old shared libraries, i.e. the ones NOT linked as lib*.so.

That's it for today :)

12 comments:

  1. Hey! Thank you so much for this tutorial. It is the ONLY one that worked for me with x264, and I spent days trying to work this out! Anyway, a couple notes, if I may:

    1) there was no line to uncomment in /etc/apt/sources.list on a fresh install, I don't think. I just added "deb http://us-west-1.ec2.archive.ubuntu.com.s3.amazonaws.com/ubuntu/ precise main restricted universe multiverse" to the bottom of the file.

    2) You left out "install" in "sudo apt-get build-essential nasm pkg-config "

    3) Most importantly, please recommend that people REBOOT after installing ffmpeg. I completely wiped my system twice before I figured out that I needed to reboot.

    Thanks again!

    ReplyDelete
  2. Hey Jake, thanks so much for the kind words and for pointing out a typo :) Glad I could help!

    ReplyDelete
  3. Thank you very much, it really helped me!
    I couldn't do it as described here http://trac.ffmpeg.org/wiki/UbuntuCompilationGuide
    And ffmpeg worked without reboot

    ReplyDelete
  4. I had a problem on step 3. It could not find libgpac1-dev. I ended up building FFmpeg without it. Seemed to build fine. All codecs are enabled. I'm running ubuntu 12.04 on a fresh install.

    ReplyDelete
    Replies
    1. I had the same issue. sudo apt-get install libgpac-dev worked (drop the '1').

      Delete
  5. On Ubuntu 12.04.3 LTS libgpac1-dev is libgpac-dev, xvidcore-1.3.2/build/generic is xvidcore/build/generic.

    Thanks

    ReplyDelete
  6. Thank you for this. I was able to successfully build a fresh ffmpeg on my Ubuntu 12.10 system. I'm still tearing my hair out, though, because is still plays no audio with my mp4/AAC videos. I swear I've looked at everything, and I seem to have the correct codecs present and enabled. Without aac audio working with ffmpeg, I can't in turn use Openshot or other video editing software that relies on ffmpeg. The audio in these vids plays fine in VLC and totem player. Any ideas?

    ReplyDelete
  7. Thanks your solution works like a charm. If you're going to process mkv files you're going to need libvpx library:

    sudo apt-get install libvpx-dev

    and add in the configure part of ffmpeg:

    --enable-libvpx

    ReplyDelete
  8. Hi Julian,
    Thank you very much for the post, this is the only one worked for me. I had been trying for days to get this resolved. now my Drupal 7.26 detects ffmpeg correctly.

    Prashant+

    ReplyDelete