Build Minimal FFmpeg with H.264 and AAC Decoders
Compiling a custom, lightweight version of FFmpeg can drastically reduce binary size and memory usage by stripping away unused codecs and features. This guide provides a precise shell configuration script and compilation steps to build a minimal FFmpeg binary optimized strictly for decoding H.264 video and AAC audio.
The Minimal Configuration Script
To build a stripped-down version of FFmpeg, you must start by
disabling all default components using the
--disable-everything flag. You then selectively re-enable
only the H.264 and AAC decoders, along with the essential demuxers,
parsers, and protocols required to read and process container formats
like MP4.
Create a file named build_minimal_ffmpeg.sh and paste
the following script:
#!/usr/bin/env bash
# Exit immediately if a command exits with a non-zero status
set -e
# Run the configure script with minimal flags
./configure \
--disable-gpl \
--disable-doc \
--disable-programs \
--enable-ffmpeg \
--disable-everything \
--disable-network \
--disable-autodetect \
--enable-decoder=h264 \
--enable-decoder=aac \
--enable-parser=h264 \
--enable-parser=aac \
--enable-demuxer=mov \
--enable-demuxer=h264 \
--enable-demuxer=aac \
--enable-protocol=file \
--disable-hwaccels \
--disable-filters \
--disable-swscale \
--disable-devicesUnderstanding the Configuration Flags
--disable-everything: This is the most crucial flag. It disables all encoders, decoders, muxers, demuxers, filters, and protocols, giving you a completely blank slate.--enable-ffmpeg: Re-enables the creation of the executableffmpegcommand-line tool.--enable-decoder=h264and--enable-decoder=aac: Explicitly enables the native software decoders for H.264 video and AAC audio.--enable-parser=h264and--enable-parser=aac: Enables the parsers required to split the raw container streams into decodable packets.--enable-demuxer=mov: Enables the demuxer for MP4, MOV, M4A, and 3GP containers, which are the primary carriers of H.264 and AAC streams.--enable-protocol=file: Allows FFmpeg to read input files from local storage.
Compilation Steps
Once the configuration script is ready, execute the following commands in your terminal from the root of the FFmpeg source directory:
Make the script executable:
chmod +x build_minimal_ffmpeg.shRun the configuration script:
./build_minimal_ffmpeg.shCompile the binary using your available CPU cores (e.g., 4 cores):
make -j4
Upon successful compilation, the resulting ffmpeg binary
will be located in the root source directory. This binary will be
significantly smaller than a standard distribution build, often
occupying only a few megabytes.