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-aachttps://trac.ffmpeg.org/wiki/CompilationGuide/Quick/libfdk-aac

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://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
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
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. We followed this simple procedure:

sudo checkinstall --nodoc --install=no

Set the version to something like 20:3.2.4+git-2017-02-21-4295904, 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 ffmpeg for their latest release, followed by a plus sign
  • Debian version -- use the git pull date and version
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.4+git-2017-02-21-4295904-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.

How to encode with the hardware encoder

Remember to give the user access to the hardware encoder:

sudo usermod -a -G video myuser

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

 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.