Hardware Encoding with the Raspberry Pi

Task

Solved by Mark Turner.

The Raspberry Pi has a hardware encoding (and decoding) chip to transform .ts to .mp4. Can you help?

If so, write to

and we will try to connect you with a mentor.

Related links

More information

Add support via ffmpeg. See https://john.ly/stream-rtmp/

$ sudo apt-get update

$ sudo apt-get upgrade

Compiling libfdk-aac: https://trac.ffmpeg.org/wiki/CompilationGuide/Quick/libfdk-aac

Installing ffmpeg: https://sebastian.korotkiewicz.eu/2016/09/30/ffmpeg-on-raspbian-raspberry-pi/ (but see below)

The short story is that this works, but the result is too poor to be used by Red Hen; we could provide feedback to the Raspberry Pi developers. An alternative may be to work with Beagleboard -- community-supported hardware development -- to implement a higher-quality hardware video compressor.

Video compression

We currently transfer the uncompressed files from the Raspberry Pi capture stations to the Hoffman2 high-performance computing cluster at UCLA, which processes the files and sends them to the NewsScape search engines and archival servers. However, in some cases it may be necessary or desirable to perform the compression locally, either with software codecs or with the hardware codec built into the Raspberry Pi.

Enabling hardware compression

The Raspberry Pi has a hardware compression chip. To support it, build a custom version of ffmpeg as follows.

First activate the source repositories in /etc/apt/sources.list by adding this:

deb-src http://www.deb-multimedia.org jessie main non-free

Install the build dependencies:

apt-get update
apt-get install build-essential dh-make fakeroot yasm pkg-config libfdk-aac-dev libx264-dev

Get ffmpeg, note the git version number, and configure:

cd ~/software/ffmpeg
git clone --depth=1 git://source.ffmpeg.org/ffmpeg.git
./configure --enable-gpl --enable-libx264 --enable-nonfree --enable-mmal --enable-omx --enable-omx-rpi --enable-libfdk-aac --prefix=/usr

make -j4

At this point, build a deb package or install locally.

Building a deb package

The advantage of creating a deb package (cf. details) is that you can install it on multiple computers. Red Hen followed this procedure and has a ready package that provides hardware encoding through ffmpeg:

mkdir /usr/share/ffmpeg
cat RELEASE ; ./version.sh
sudo checkinstall --nodoc --install=no

Set the version to the output of RELEASE and ./version.sh -- something like 20:3.2+git-2017-03-27-d7896e9, consisting of:

    • epoch -- if you don't want the package to be overwritten on an upgrade, use a high epoch number, followed by a colon
    • upstream version -- check the content of RELEASE, followed by a plus sign
    • Debian version -- use the git pull date and version -- see the output of the command ./version.sh

Note that the epoch will not be included in the deb file name, so the version number used above will create a package called ffmpeg_3.2+git-2017-03-27-d7896e9-1_armhf.deb. However, the epoch will show and have the desired effect when installing the package.

This package can now be copied to other RPis. Install it along with the libx264-148 package from Raspbian Stretch (the name of the next version release):

just install libx264-148/stretch
just install ffmpeg_3.2.4+git-2017-02-21-4295904-1_armhf.deb

In addition to being portable, an installed package is also easy to uninstall and upgrade.

Verify the hardware encoder is supported by ffmpeg:

ffmpeg -decoders | grep mmal
V..... h264_mmal h264 (mmal) (codec h264)
V..... mpeg2_mmal mpeg2 (mmal) (codec mpeg2video)
V..... mpeg4_mmal mpeg4 (mmal) (codec mpeg4)
V..... vc1_mmal vc1 (mmal) (codec vc1)
ffmpeg -encoders | grep omx
V..... h264_omx             OpenMAX IL H.264 video encoder (codec h264)

How to encode with the hardware encoder

First give the user access to the hardware encoder:

sudo usermod -a -G video myuser

Log out of that user (all screen windows, out of the RPi) and then log back in. Otherwise the hardware encoder cannot be accessed and we get the error "failed to open vchiq instance."

You can then use the ts2mp4-single-02.sh script and encode a one-hour video in about 20 minutes:

ffmpeg -i $FIL.$EXT -y $FFMAP -acodec libfdk_aac -b:a 96k -ac 2 -ar 44100 -c:v h264_omx -r 25 -b:v 900  $FIL.mp4

This is implemented in the script check-cc-single-02.sh. The quality is decent, but not as good as software-encoded files; the hardware encoder is poor at low-contrast motion, which affects faces.

Install ffmpeg directly

This should only be done on an experimental computer where we occasionally reinstall a fresh operating system. Installing ffmpeg in this way makes it very hard to uninstall it.

For an experimental build on a single computer, we first got the latest x264 and fdk-aac and built them:

git clone --depth 1 git://git.videolan.org/x264
apt-get install pkg-config autoconf automake libtool
sudo apt-get install pkg-config autoconf automake libtool
git clone https://github.com/mstorsjo/fdk-aac.git
cd fdk-aac
./autogen.sh
./configure --enable-shared --enable-static
make
make install

Then we built ffmpeg:

sudo apt-get purge --auto-remove ffmpeg
git clone --depth=1 git://source.ffmpeg.org/ffmpeg.git
./configure --enable-gpl --enable-libx264 --enable-nonfree --enable-mmal --enable-omx --enable-omx-rpi --enable-libfdk-aac
make -j4
sudo make install
ldconfig

This version cannot be copied to other computers.

HandBrake

See instructions and more for compiling on a Raspberry Pi.

deb-src http://mirror.ox.ac.uk/sites/archive.raspbian.org/archive/raspbian/ jessie main contrib non-free rpi

deb-src http://archive.raspbian.org/raspbian/ jessie main contrib non-free rpi