- Qt 6.8
- Qt Multimedia
- Building FFmpeg from source on Linux
- Qt 6.8.3 Reference Documentation

# Building FFmpeg from source on Linux

This page explains how to configure and build FFmpeg on Linux. This involves:

- Get the FFmpeg source code.
- Install the required dependencies.
- Configure FFmpeg from the command line.
- Build the development libraries.

## Get the FFmpeg source code

You can get the FFmpeg source code in these ways:

- Download from the FFmpeg download page.
- Clone from git. For example, this command clones the version 7.1 of the FFmpeg sources to `~/ffmpeg`.

       $ git clone --branch n7.1 https://git.ffmpeg.org/ffmpeg.git ffmpeg

It is recommended to use the same FFmpeg version as documented in the Qt Multimedia main page.

The following paragraphs assume that you store the FFmpeg source code under `~/ffmpeg`.

## Prerequisites

To build FFmpeg, these tools and packages are required:

- Yasm
- Developer packages for your chosen security backend (see below).

### Installing packages

To install the required packages (including Yasm for FFmpeg), run:

     $ apt-get install yasm

Additionally, if you're enabling a security backend, you need the corresponding developer package, for example:

     $ apt-get install libssl-dev

For other backends, install the relevant packages:

- For OpenSSL: `libssl-dev`.
- For GnuTLS: `libgnutls-dev`.
- For LibreSSL (libtls): `libtls-dev`.
- For MbedTLS: `libmbedtls-dev`.

## Configuring and building FFmpeg

Create a `build` directory inside the `~/ffmpeg` directory and navigate into it:

     $ mkdir ~/ffmpeg/build
     $ cd ~/ffmpeg/build

To configure FFmpeg, run:

     $ ../configure --prefix=/usr/local/ffmpeg --disable-doc --enable-network --enable-shared

The `--prefix` argument specifies a path where the FFmpeg development libraries are installed after building. The documentation is not needed, but network features should be enabled. To build FFmpeg as static libraries, omit the `--enable-shared` option.

If you're building FFmpeg with a security backend, choose the appropriate option:

     $ ../configure --enable-openssl # For OpenSSL
     $ ../configure --enable-gnutls # For GnuTLS
     $ ../configure --enable-libtls # For LibreSSL (libtls)
     $ ../configure --enable-mbedtls # For MbedTLS

Make sure you have the corresponding developer package installed as mentioned earlier.

### Hardware Acceleration

By default, FFmpeg is built without hardware acceleration unless the appropriate packages are installed. For better performance, especially on systems with GPU support, it’s recommended to install VAAPI development packages:

     $ apt-get install libva-dev

This will enable FFmpeg to detect VAAPI support during configuration.

If FFmpeg will be used with NVIDIA graphics cards, you should install the NVIDIA codec headers. These can be downloaded from the following repository: NVIDIA Codec Headers on GitHub.

To build and install the headers, run the following commands:

     $ mkdir ~/nv-codec-headers
     $ cd ~/nv-codec-headers
     $ git clone https://github.com/FFmpeg/nv-codec-headers.git
     $ mkdir build
     $ cd build
     $ make -j install

Make sure to check the compatibility of the `nv-codec-headers` with your NVIDIA driver version, as newer `nv-codec-headers` may not be compatible with older driver versions. Refer to the official documentation for more details.

## Disabling Autodetect

By default, FFmpeg attempts to automatically detect available dependencies on the build system. This includes hardware acceleration and security backends. If you want to disable autodetection (for example, to avoid compiling unnecessary dependencies), you can use:

     $ ../configure --disable-autodetect

If you are building FFmpeg with any additional dependencies (like a security backend or hardware acceleration), ensure that the required libraries are installed on the target system. There are several ways to handle this:

- Deliver binaries of the dependencies together with FFmpeg.
- Ensure that the required binaries are installed on the target system.
- Implement library stubs that can look up the real libraries and return an error code if the real libraries aren't found.

## RPATH and RUNPATH on Linux

On Linux, FFmpeg uses both RPATH and RUNPATH metadata sections to manage library paths. We recommend using the `patchelf` utility to set the correct runpath:

     $ patchelf --set-rpath "new/runpath" lib.so

By default, `patchelf` modifies the RUNPATH, as RPATH is considered obsolete on Linux. To set a relative runpath, you can use the `$ORIGIN` variable, which makes the path relative to the location of the executable:

     $ patchelf --set-rpath "$ORIGIN/../lib" lib.so

This approach ensures portability by linking libraries based on their relative locations.

## Building FFmpeg

Once you've configured FFmpeg with your desired options, you can build and install it:

     $ make -j install

If the build completes without errors, FFmpeg development libraries will be installed in the `/usr/local/ffmpeg` directory. You can then reference this path when configuring Qt Multimedia by setting the `FFMPEG_DIR` variable.