Compile FFmpeg from Source with VP9 libvpx

This guide provides a straightforward, step-by-step walkthrough for compiling FFmpeg from source code with libvpx enabled, allowing you to encode high-quality VP9 video. You will learn how to install the necessary build prerequisites, compile the latest version of libvpx, and configure and install FFmpeg with VP9 support on a Linux-based system.

Step 1: Install Dependencies and Build Tools

Before compiling, you need to install the essential compilation tools and libraries. Update your package manager and install the required packages.

For Ubuntu/Debian-based systems, run:

sudo apt update
sudo apt install -y build-essential git pkg-config yasm nasm clean-up cmake

Note: yasm or nasm are highly recommended for optimizing the assembly code performance of both libvpx and FFmpeg.

Step 2: Create a Workspace Directory

Create a directory structure to keep your source code and build files organized:

mkdir -p ~/ffmpeg_sources ~/ffmpeg_build ~/bin

Step 3: Download and Compile libvpx

libvpx is the official VP8/VP9 video encoder library from the WebM project. To compile it from source:

  1. Navigate to the source directory and clone the repository:

    cd ~/ffmpeg_sources
    git clone --depth 1 https://chromium.googlesource.com/webm/libvpx.git
  2. Move into the directory, configure the build, compile, and install it:

    cd libvpx
    ./configure --prefix="$HOME/ffmpeg_build" --disable-examples --disable-unit-tests --enable-vp9-highbitdepth --as=yasm
    make -j$(nproc)
    make install

Step 4: Download and Compile FFmpeg

With libvpx compiled, you can now configure FFmpeg to link against it.

  1. Navigate to the source directory and clone the FFmpeg repository:

    cd ~/ffmpeg_sources
    git clone --depth 1 https://git.ffmpeg.org/ffmpeg.git ffmpeg
  2. Move into the FFmpeg directory:

    cd ffmpeg
  3. Configure the build. You must define the paths to the newly compiled libvpx inside ~/ffmpeg_build using PKG_CONFIG_PATH:

    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" \
      --bindir="$HOME/bin" \
      --enable-gpl \
      --enable-libvpx \
      --enable-nonfree
  4. Compile and install FFmpeg:

    PATH="$HOME/bin:$PATH" make -j$(nproc)
    make install
    hash -r

Step 5: Verify the Installation

To verify that your newly compiled FFmpeg binary is installed and includes VP9 support, run the following commands:

Check the binary location and version:

~/bin/ffmpeg -version

Verify that the libvpx-vp9 encoder is available:

~/bin/ffmpeg -encoders | grep libvpx

The output should list libvpx-vp9 as an active encoder, indicating the compilation was successful. You can now use FFmpeg to encode WebM VP9 videos.