Compile FFmpeg with VideoToolbox on macOS

Compiling FFmpeg with Apple’s VideoToolbox framework allows you to leverage hardware-accelerated video encoding and decoding on macOS. This guide provides a straightforward, step-by-step walkthrough to install the necessary dependencies, configure the FFmpeg source code with the VideoToolbox flag, and compile the final binary on your Mac.

Step 1: Install Prerequisites

Before compiling FFmpeg, you need to install the Xcode Command Line Tools and the Homebrew package manager to download essential build dependencies.

  1. Open your Terminal and install the Xcode Command Line Tools:

    xcode-select --install
  2. Install Homebrew (if you do not have it already):

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  3. Install the required compilation tools and libraries via Homebrew:

    brew install git pkg-config nasm yasm

Step 2: Clone the FFmpeg Source Code

Clone the official FFmpeg git repository to your local machine and navigate into the directory:

git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg
cd ffmpeg

To use a specific stable release instead of the master branch, you can check out a release tag (e.g., n7.0):

git checkout n7.0

Step 3: Configure the Build with VideoToolbox

Run the ./configure script to prepare the build. The crucial flag here is --enable-videotoolbox, which enables hardware acceleration for macOS.

./configure \
  --prefix=/usr/local \
  --enable-gpl \
  --enable-nonfree \
  --enable-videotoolbox \
  --enable-audiotoolbox \
  --disable-shared \
  --enable-static

Configuration Flags Explained:

Step 4: Compile and Install FFmpeg

Compile the source code using multiple CPU cores to speed up the process, then install the compiled binary.

  1. Compile the code (replace $(sysctl -n hw.ncpu) with the number of CPU cores you want to allocate):

    make -j$(sysctl -n hw.ncpu)
  2. Install the binary to your system:

    sudo make install

Step 5: Verify the Installation

After the installation completes, verify that FFmpeg is installed and that the VideoToolbox encoders are successfully enabled.

  1. Check the FFmpeg version:

    ffmpeg -version
  2. List the available VideoToolbox encoders to confirm hardware acceleration is supported:

    ffmpeg -encoders | grep videotoolbox

You should see output listing encoders such as h264_videotoolbox, hevc_videotoolbox, and prores_videotoolbox, confirming that your custom-built FFmpeg binary is ready to use hardware acceleration.