How to compile FFmpeg with GPU(cuda) acceleration

A short tutorial on how to compile R for beginners
Bash
Tutorial
Gpu
Author

Gninahophin Jean-Pierre Axel

The setup

This is a short tutorial on how to compile FFmpeg from source with CUDA acceleration. It’s mostly a patch for this tutorial. It assumes you have a working Linux installation with Nvidia drivers installed. It’s slightly tailored toward Ubuntu/Debian users, but the process is about 90% identical on other GNU/Linux distros.

The process

1) Getting the source code

The latest stable version at the time of writing this can be found here or here.

A suggestion from the tutorial I referenced, which is quite good, is to create one folder for the source code and another for the build output.

mkdir ~/ffmpeg_sources ~/bin
cd ~/ffmpeg_sources
wget -O ffmpeg.tar.bz2 https://www.ffmpeg.org/releases/ffmpeg-8.0.tar.bz

The line above will download the FFmpeg 8.0 tarball.

2) Extracting the source code from the tarball

tar -xjvf ffmpeg.tar.bz2
cd ffmpeg-8.0

3) The configure script and the flags

The dependencies you need to compile on Debian/Ubuntu can be obtained like this:

sudo apt install -y git wget autoconf automake build-essential cmake git-core libass-dev libfreetype6-dev libgnutls28-dev libmp3lame-dev libsdl2-dev libtool libva-dev  libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev meson ninja-build pkg-config texinfo wget yasm zlib1g-dev libunistring-dev nasm libx264-dev libx265-dev libvpx-dev libfdk-aac-dev libopus-dev libaom-dev libdav1d-dev libnuma-dev libsvtav1-dev libsvtav1enc-dev libsvtav1dec-dev

Extra headers you might need depending on the version of FFmpeg you get:

git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers
make
sudo make install

To get the official list of flags:

./configure -help

TL;DR

PATH="$HOME/bin:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure \
  --prefix="$HOME/ffmpeg_build" \
  --pkg-config-flags="--static" \
  --extra-cflags="-I$HOME/ffmpeg_build/include" \
  --extra-ldflags="-L$HOME/ffmpeg_build/lib" \
  --extra-libs="-lpthread -lm" \
  --ld="g++" \
  --bindir="$HOME/bin" \
  --enable-gpl \
  --enable-gnutls \
  --enable-libaom \
  --enable-libass \
  --enable-libfdk-aac \
  --enable-libfreetype \
  --enable-libmp3lame \
  --enable-libopus \
  --enable-libsvtav1 \
  --enable-libdav1d \
  --enable-libvorbis \
  --enable-libvpx \
  --enable-libx264 \
  --enable-libx265 \
 --enable-cuda-nvcc --enable-libnpp --enable-cuvid --enable-nvenc --enable-nonfree

The last line of flags is the one relevant for Nvidia GPUs. You can tweak the rest or just copy-paste the whole thing if you don’t want to think about it.

PATH="$HOME/bin:$PATH" make
make install
source ~/.profile

You can check that FFmpeg is installed correctly by running:

source ~/.profile
ffmpeg -version