Compiling FFmpeg with hardware acceleration support for Raspberry Pi

This works with Raspbian. The resulting FFmpeg binaries will support the following codecs: MP3, Vorbis, VP6, VP9, x264, Opus and all sorts of related container formats – AVI, MP4, WebM.

The whole process will take about 3 hours.

Because you are about to install a lot of things system-wide, then switch to root user, so you don’t have to prefix everything with “sudo” all the time:

sudo -i

Next you will need to install couple of software packages from the official repository:

apt install git libssl-dev libasound2-dev libomxil-bellagio-dev

Some libraries benefit from having some parts of their code compiled using NASM, so we start with compiling NASM. It is one of the few bits you can install from an official repository if you prefer, but compiling it from the latest source takes only couple of minutes and compared to the v2.12.01, it has fixes for some nasty bugs.

cd /usr/src/
wget http://www.nasm.us/pub/nasm/releasebuilds/2.14.02/nasm-2.14.02.tar.gz
tar -xvf nasm-2.14.tar.gz
cd nasm-2.14
./configure
make
make install

Next comes the MP3 support

cd /usr/src/
wget http://downloads.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz
tar xzvf lame-3.100.tar.gz
cd lame-3.100
./configure --enable-nasm --disable-frontend
make
make install

Then we build the Ogg libraries

cd /usr/src/
wget http://downloads.xiph.org/releases/ogg/libogg-1.3.3.tar.gz
tar xzvf libogg-1.3.3.tar.gz
cd libogg-1.3.3
./configure
make
make install

…and now Vorbis

cd /usr/src/
wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.6.tar.gz
tar xvzf libvorbis-1.3.6.tar.gz
cd libvorbis-1.3.6
./configure
make
make install

…and Opus

cd /usr/src/
wget https://archive.mozilla.org/pub/opus/opus-1.3.tar.gz
tar xzvf opus-1.3.tar.gz
cd opus-1.3
./configure --disable-doc --disable-extra-programs
make
make install

…and x264

cd /usr/src/
git clone --depth 1 http://git.videolan.org/git/x264
cd x264
./configure --disable-cli --enable-shared
make
make install

…and VP6 / VP9 are used somewhere

cd /usr/src/
git clone --depth 1 https://chromium.googlesource.com/webm/libvpx.git
cd libvpx
./configure --disable-mmx --disable-examples --disable-tools --disable-docs --disable-unit-tests --enable-vp9-highbitdepth --as=nasm
make
make install

and FINALLY the last act – the actual FFmpeg (this will take around 2 hours)

cd /usr/src/
git clone --depth 1 git://source.ffmpeg.org/ffmpeg
cd ffmpeg
./configure --enable-gpl --enable-nonfree --disable-doc --enable-openssl --enable-libmp3lame --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libopus --extra-ldflags=-ldl --enable-omx --enable-omx-rpi --enable-mmal
make
make install

Now using the FFmpeg with the hardware acceleration…
The following example uses Raspberry Pi’s camera and an external USB audio interface to stream a 720p video to Twitch (the key is not a real one – you have to set up one for yourself):

/usr/local/bin/ffmpeg -hide_banner -loglevel error \
-thread_queue_size 512 -f alsa -ar 44100 -channel_layout stereo -ac 2 -i default:CARD=CODEC \
-thread_queue_size 512 -f v4l2 -framerate 25 -input_format yuv420p -video_size 1280x720 -i /dev/video0 -vf "hflip" \
-vsync vfr -vcodec h264_omx -pix_fmt yuv420p -b:v 2100k -acodec aac -b:a 128k \
-f flv "rtmp://live.twitch.tv/app/live_999999999_abcdefghijklmnopqrstuvwxyzabcd"