How to Compile ffmpeg on a Raspberry Pi
19 Aug 2015Encoding 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
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.
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:
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:
Last step is to run the command “ldconfig” which will help ffmpeg load up the dynamic library of libfaac.
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.
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.
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:
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:
Now you should have global access to the ffmpeg command. Issue the following command to make sure you have everything configured:
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.
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:
- acodec specifies the type of audio codec to use. We used our libfaac codec to encode the audio into aac format.
- b:a is the bitrate at which the audio was encoded at. The higher the bitrate the lower the compression and higher filesize. So find a happy median.
- vcodec is the awesome mpeg4 which is a lossless video compression. It’s the magic to make our videos smaller without much degredation to video quality.
- b:v is the bitrate at which the video was encoded at. Again - higher the bitrate lowers the compression but yeilds higher filesizes. Find one that suits your input video file.
- +aic flag allows advanced mpeg4 encoding algorithms which will decrease our encoding times.
- +mv4 flag also affects our mpeg4 encoding by using advanced mpeg4 encoding algorithms. But this flag will increase our file quality.
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.