This guide provides a comprehensive walkthrough for compiling FFmpeg from source on an Ubuntu system equipped with an NVIDIA GPU. The resulting build will support NVIDIA's hardware encoding/decoding (NVENC/DEC), NPP filters (NVIDIA Performance Primitives), and CUDA-based VMAF (Video Multi-Method Assessment Fusion) for video quality assessment.
Before you begin, ensure your system environment is similar to the configuration below. Version matching is crucial for a successful compilation. The GPU needs to support HEVC; refer to the NVIDIA NVDEC Support Matrix.
- GPU: NVIDIA GeForce RTX 4090 or other compatible models
- OS: Ubuntu 22.04
- NVIDIA Driver Version: A version compatible with CUDA 12.6
- CUDA Version (from
nvidia-smi):12.x - CUDA Toolkit Version:
12.6(This is the version used for compilation) - Target FFmpeg Version:
6.1
Key Tip: The version of the NVIDIA Codec Headers (ffnvcodec) must be compatible with the CUDA Toolkit version installed on your system and the version of FFmpeg you intend to compile.
Please follow these steps in order.
Update system packages and install required development tools and libraries:
sudo apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y \
libopenjp2-7-dev \
ninja-build \
cmake \
git \
python3 \
python3-pip \
nasm \
xxd \
pkg-config \
curl \
unzip \
ca-certificates \
libnuma-dev \
libsm6 \
libxext6 \
libxrender1 \
libgl1 \
vim \
nvidia-cuda-toolkit# Create a working directory (custom path allowed)
mkdir -p ~/ffmpeg-build && cd ~/ffmpeg-build
# Clone nv-codec-headers (NVIDIA codec headers)
git clone https://github.com/FFmpeg/nv-codec-headers.git
# Clone libvmaf (video quality assessment library)
git clone https://github.com/Netflix/vmaf.git
cd vmaf && git checkout master # Switch to master branch (modify version if needed)
cd ..
# Clone FFmpeg source code
git clone https://github.com/FFmpeg/FFmpeg.git
cd FFmpeg && git checkout master # Switch to master branch (modify version if needed)
cd ..cd nv-codec-headers
make
sudo make install
cd ..-
Install the meson build tool:
python3 -m pip install meson
-
Compile and install libvmaf:
cd vmaf meson libvmaf/build libvmaf \ -Denable_cuda=true \ -Denable_avx512=true \ --buildtype release ninja -vC libvmaf/build sudo ninja -vC libvmaf/build install cd ..
-
Update system library cache:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/x86_64-linux-gnu/ sudo ldconfig
cd FFmpeg
# Configure compilation options (enable CUDA, NVENC, NVDEC, and libvmaf)
./configure \
--enable-libnpp \
--enable-nonfree \
--enable-nvdec \
--enable-nvenc \
--enable-cuvid \
--enable-cuda \
--enable-cuda-nvcc \
--enable-libvmaf \
--enable-ffnvcodec \
--disable-stripping \
--extra-cflags="-I/usr/local/cuda/include" \
--extra-ldflags="-L/usr/local/cuda/lib64 -L/usr/local/cuda/lib64/stubs/"
# Compile (adjust the number after -j based on CPU cores for faster compilation)
make -j$(nproc)
# Install
sudo make install
cd ..-
Upgrade pip and set up links:
sudo ln -sf /usr/bin/python3 /usr/bin/python python3 -m pip install --no-cache-dir --upgrade pip setuptools wheel
-
Install Python dependencies (assuming project code is cloned locally; replace with actual path):
# Navigate to the project root directory cd /path/to/your/project # Install dependencies python3 -m pip --no-cache-dir install -r requirements/requirements.txt python3 -m pip --no-cache-dir install -r requirements/requirements_scoring.txt || true python3 -m pip --no-cache-dir install -r requirements/requirements_annotation.txt || true
-
Check FFmpeg version and configuration:
ffmpeg -version ffmpeg -encoders | grep nvenc # Verify NVENC support ffmpeg -decoders | grep nvdec # Verify NVDEC support ffmpeg -filters | grep vmaf # Verify libvmaf support
-
If all the above commands output corresponding content correctly, the installation is successful.
- Cause: This error typically occurs if you downloaded the VMAF source code as a ZIP archive instead of using
git clone. The build script relies on the.gitdirectory to generate version header files. - Solution: Always use
git cloneto get the source code.git clone https://github.com/Netflix/vmaf.git
- Error Message: Something like
ERROR: nvenc requested, but NVIDIA Video Codec SDK 12.1 or later is required.(The version number may vary). - Cause: This means the version of
nv-codec-headersyou checked out is not compatible with your NVIDIA driver, CUDA Toolkit, or the version of FFmpeg you are building. - Solution:
- Carefully re-check your NVIDIA Driver and CUDA Toolkit versions.
- Go back to Step 3: Install NVIDIA Codec Headers and ensure you
git checkoutthe branch that best matches your environment (e.g.,sdk/12.6). - Consult the Official NVIDIA FFmpeg Guide or the
nv-codec-headersrepository to confirm version compatibility.