How to Compile ffmpeg on a Raspberry Pi

Encoding video files on Raspberry Pi

Encoding video files to different formats takes a lot of processing power from a computer. I realized this when I was compressing a lot of my "raw" AVI files from my camera down to a more acceptable/compressed format for archiving.

The Raspberry Pi is a perfectly capable computer to do such a task. Offloading the encoding process to the Raspberry Pi frees up your main machine for more important tasks such as gaming/surfing Reddit/watching movies - you know, important stuff!

Disclaimer

I think this is as good as time as any to say I've built this guide around a Raspberry Pi B+ v2. With the more capable quad-core processor, compiling and encoding on this device is more capable than it's younger brother Raspberry Pi B+ v1. In theory, the guide will work on the older device - but compile times can take 9 hours+ on the older device.

Download and install dependencies

ffmpeg has a long line of dependencies. Not all of these are required and these are just an opiniated subset of what I believe MOST use-cases should be. Certainly research your options if you need other encoding options. The research of these options was heavily enfluenced by this page: ffmpeg compilation guide

sudo apt-get update;
sudo apt-get -y --force-yes install libfaac-dev libmp3lame-dev libx264-dev yasm git autoconf automake build-essential libass-dev libfreetype6-dev libtheora-dev libtool libvorbis-dev pkg-config texi2html zlib1g-dev

Compile the libfaac library

I like the libfaac audio codec for ffmpeg for it's fast encoding and terrific audio compression results. It's a win/win. So it's worth compiling this source, as distributing it's binary is against it's licensing.

cd ~;
wget http://downloads.sourceforge.net/faac/faac-1.28.tar.gz;
tar -xvf faac-1.28.tar.gz;
cd faac-1.28/;

Now here comes the tricky part. We need to modify some of the source files to enable the build to compile correctly. Documented in this StackOverflow post Install FAAC on Linux We need to run the following command:

nano +126 common/mp4v2/mpeg4ip.h;

Which will open up the nano text editor to line 126. It should start with "strcasestr". And go ahead and delete that line.

Now we can run the rest of the compile process:

./configure
make;
make install;

Last step is to run the command "ldconfig" which will help ffmpeg load up the dynamic library of libfaac.

sudo ldconfig;

Download the ffmpeg source

We will now download the latest release (at the time of this writeup it was ffmpeg-2.7.2). Using the git command we will download the source from their repo into the "ffmpeg" folder in our user's home directory.

cd ~;
git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg;

Run the ./configure command

Now it's time to configure out make file before the build process. This is an in-flight checklist for the build process to make sure we have our dependencies in order.

cd ffmpeg;
./configure --enable-libfreetype --enable-gpl --enable-nonfree --enable-libx264 --enable-libass --enable-libfaac --enable-libmp3lame --bindir="/usr/local/bin"

Afterwards you should see the checklist of all the enabled/disabled options for the binary we're about to build. As long as you don't see any glaring errors you're free to run the make command:

make;

Now go grab a coffee. This will take a while to compile. Approximately one and a half hours to complete. Once this completes, go ahead and move the binaries to their final location:

sudo make install;

Now you should have global access to the ffmpeg command. Issue the following command to make sure you have everything configured:

ffmpeg -version;

You should see information about all the options that were used during the compile.

Basic ffmpeg usage

I currently have a .avi file that is 159 megabytes in file size. Which is okay, but I'd like to have an even smaller file size. I want to use the MP4 with decent compression.

ffmpeg -i input.avi -acodec libfaac -b:a 128k -vcodec mpeg4 -b:v 1200k -flags +aic+mv4 output.mp4;

After running that command the AVI file compressed down to 1/4's the file size without much degredation.

A quick run down of the options passed here:

Questions?

This is a pretty extensive topic. And I only barely scratched the surface. If you have any issues compiling your own ffmpeg binary- let me know in the comments below and I'll do my best to answer.