Install FFmpeg on NVIDIA Jetson with Hardware Encoding (jetson-ffmpeg, h264_nvmpi)

Build FFmpeg with jetson-ffmpeg (NVMPI) on Jetson TX2 / JetPack 4.3 for hardware H.264 encoding and decoding, then verify the h264_nvmpi encoder and decoder.

Install FFmpeg on NVIDIA Jetson with Hardware Encoding (jetson-ffmpeg, h264_nvmpi)

Why build FFmpeg yourself on a Jetson

NVIDIA doesn’t ship an FFmpeg build that uses the Jetson’s hardware video encoder and decoder. The stock FFmpeg from apt runs everything on the CPU, which is slow on a Jetson. The open-source jetson-ffmpeg project adds an nvmpi codec that talks to the Jetson’s hardware. Once it’s built in, you get h264_nvmpi for fast H.264 encoding and decoding.

I tested these steps on a Jetson TX2 with JetPack 4.3.

Step 1: Install the build dependencies

$ sudo apt build-dep ffmpeg

If you get the error “You must put some ‘source’ URIs in your sources.list”, source repositories are turned off. Open the sources list:

$ sudo vi /etc/apt/sources.list

Uncomment every line that starts with deb-src, save (:wq), then try again:

$ sudo apt update
$ sudo apt build-dep ffmpeg

Step 2: Build the jetson-ffmpeg library

This builds the nvmpi library that FFmpeg will use to reach the hardware encoder and decoder:

$ git clone https://github.com/jocover/jetson-ffmpeg.git
$ cd jetson-ffmpeg
$ mkdir build
$ cd build
$ cmake ..
$ make
$ sudo make install
$ sudo ldconfig

Step 3: Patch and build FFmpeg

Download FFmpeg 4.2, apply the nvmpi patch, and build it with --enable-nvmpi:

$ git clone git://source.ffmpeg.org/ffmpeg.git -b release/4.2 --depth=1
$ cd ffmpeg
$ wget https://github.com/jocover/jetson-ffmpeg/raw/master/ffmpeg_nvmpi.patch
$ git apply ffmpeg_nvmpi.patch
$ ./configure --enable-nvmpi
$ make -j4 2>&1 | tee make.log
$ sudo make install

The build takes a while on the TX2. make.log keeps the output in case something fails.

Step 4: Check that h264_nvmpi is available

$ which ffmpeg
/usr/local/bin/ffmpeg
$ ffmpeg -codecs | grep 264

You should see h264_nvmpi listed as both a decoder and an encoder:

ffmpeg version 3eedf15 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 7 (Ubuntu/Linaro 7.5.0-3ubuntu1~18.04)
  configuration: --enable-nvmpi
  libavutil 56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
 DEV.LS h264                 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h264 h264_v4l2m2m h264_nvmpi ) (encoders: h264_nvmpi h264_v4l2m2m h264_vaapi )

If which ffmpeg points somewhere other than /usr/local/bin/ffmpeg, you’re still running the old apt version. Remove it or call /usr/local/bin/ffmpeg directly.

Using the hardware encoder

To encode with the hardware, pass h264_nvmpi as the video codec:

$ ffmpeg -i input.mp4 -c:v h264_nvmpi output.mp4

I first published this guide on Medium.