Build Minimal FFmpeg with H.264 and AAC Decoders

This article demonstrates how to configure and build a lightweight, minimal version of FFmpeg containing only the decoders for H.264 video and AAC audio. By stripping away thousands of unused formats, protocols, and encoders, you can drastically reduce the final binary size, decrease build times, and minimize the attack surface for embedded systems or single-purpose applications.

Prerequisites

Clone the FFmpeg source repository and navigate into the source directory:

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

Ensure standard build essentials (gcc, make, yasm, or nasm) are installed on your host system.

The Configuration Script

To create a minimal build, you must start by disabling all default components using --disable-everything and selectively re-enabling only what is required to parse, demux, and decode H.264 and AAC streams.

Create a build script named build_minimal_ffmpeg.sh with the following contents:

#!/usr/bin/env bash
set -e

./configure \
  --prefix="./dist" \
  --disable-everything \
  --disable-doc \
  --disable-debug \
  --disable-network \
  --disable-autodetect \
  --enable-small \
  --enable-protocol=file \
  --enable-demuxer=mov \
  --enable-demuxer=h264 \
  --enable-demuxer=aac \
  --enable-parser=h264 \
  --enable-parser=aac \
  --enable-decoder=h264 \
  --enable-decoder=aac \
  --enable-ffmpeg

make -j"$(nproc)"
make install

Breakdown of Configuration Flags

  • --disable-everything: Turns off all muxers, demuxers, encoders, decoders, filters, parsers, and bitstream filters by default.
  • --disable-doc, --disable-debug, --disable-network: Strips documentation generation, debugging symbols, and network libraries to save space.
  • --disable-autodetect: Prevents the configure script from automatically linking external system libraries, ensuring a self-contained build.
  • --enable-small: Optimizes code size over raw execution speed via compiler flags (such as -Os).
  • --enable-protocol=file: Allows reading media files directly from the local filesystem.
  • --enable-demuxer=mov,h264,aac: Enables container support for MP4/MOV files (which typically package H.264 and AAC), as well as raw H.264 and AAC byte-streams.
  • --enable-parser=h264,aac: Required by FFmpeg to identify frame boundaries within compressed bitstreams before passing them to the decoders.
  • --enable-decoder=h264,aac: Activates the native, built-in FFmpeg decoders for H.264 video and AAC audio without external dependencies.
  • --enable-ffmpeg: Ensures the command-line utility binary is generated.

Compiling and Verifying

Make the script executable and run it:

chmod +x build_minimal_ffmpeg.sh
./build_minimal_ffmpeg.sh

Once compilation finishes, verify that only the targeted components are present by inspecting the generated binary in the dist/bin/ folder:

./dist/bin/ffmpeg -decoders

The output will list only aac and h264, producing an executable that is a fraction of the standard FFmpeg distribution size.