A great question for all the build enthusiasts out there!
By default, FFmpeg links its dependencies dynamically, which means the resulting binaries (e.g., ffmpeg, ffprobe, etc.) will depend on the shared libraries (e.g., libavcodec.so, libx264.so, etc.) being present on the system where you run them. To link FFmpeg with its dependencies statically, you need to build FFmpeg and its dependencies with static linking enabled. Here's a step-by-step guide:
Prerequisites
- GCC (or Clang) and Make installed on your system (Linux, macOS, or Windows with MSYS2/MinGW).
- yasm or nasm (for assembling x86 code) installed.
- A clean build environment (e.g., a fresh directory).
Step 1: Build static dependencies
FFmpeg depends on various libraries (e.g., libx264, libx265, libvpx, libopus, etc.). We'll build these dependencies statically first. For brevity, I'll cover only a few popular ones. You can extend this process to other dependencies as needed.
Create a directory for your static dependencies (e.g., ffmpeg_static_deps):
mkdir ffmpeg_static_deps
cd ffmpeg_static_deps
1.1. Build static libx264 (H.264 encoder)
git clone https://code.videolan.org/videolan/x264.git
cd x264
./configure --enable-static --disable-shared --disable-asm
make -j$(nproc)
cd ..
1.2. Build static libx265 (H.265 encoder)
hg clone https://bitbucket.org/multicoreware/x265
cd x265/build/linux
cmake -G "Unix Makefiles" -DENABLE_SHARED:bool=off -DENABLE_CLI:bool=off ../../source
make -j$(nproc)
cd ../../..
1.3. Build static libvpx (VP8/VP9 encoder/decoder)
git clone https://chromium.googlesource.com/webm/libvpx
cd libvpx
./configure --enable-static --disable-shared --disable-examples --disable-tools --disable-docs
make -j$(nproc)
cd ..
1.4. Build static libopus (Opus audio codec)
git clone https://github.com/xiph/opus.git
cd opus
./autogen.sh
./configure --enable-static --disable-shared
make -j$(nproc)
cd ..
Repeat this process for other dependencies you need (e.g., libmp3lame, libvorbis, libtheora, etc.).
Step 2: Configure and build static FFmpeg
Now, let's configure FFmpeg to link against these static dependencies.
Clone FFmpeg:
cd ..
git clone https://github.com/FFmpeg/FFmpeg.git
cd FFmpeg
Configure FFmpeg with static dependencies:
./configure \
--enable-static \
--disable-shared \
--disable-debug \
--disable-ffplay \
--disable-ffprobe \
--disable-doc \
--enable-gpl \
--enable-libx264 \
--enable-libx265 \
--enable-libvpx \
--enable-libopus \
--extra-cflags="-I../ffmpeg_static_deps/x264 -I../ffmpeg_static_deps/x265/source -I../ffmpeg_static_deps/libvpx -I../ffmpeg_static_deps/opus/include" \
--extra-ldflags="-L../ffmpeg_static_deps/x264 -L../ffmpeg_static_deps/x265/build/linux -L../ffmpeg_static_deps/libvpx -L../ffmpeg_static_deps/opus/.libs" \
--extra-libs="-lx265 -lvpx -lopus -lm -lpthread"
Here's what's happening:
--enable-static: Build FFmpeg statically.
--disable-shared: Don't build shared libraries.
--disable-debug: Disable debug symbols (optional, but reduces binary size).
--disable-ffplay and --disable-ffprobe: We only need ffmpeg (you can enable these if required).
--enable-gpl: Required for libx264 and libx265 (GPL licensed).
--enable-lib*: Enable the respective libraries.
--extra-cflags: Tell FFmpeg where to find the headers for our static dependencies.
--extra-ldflags: Tell FFmpeg where to find the static libraries (.a files).
--extra-libs: Link additional required libraries (order matters!).
Now, build FFmpeg:
make -j$(nproc)
This will take a while...
Step 3: Verify static linking
After the build completes, check if FFmpeg is indeed statically linked:
ldd ./ffmpeg
On Linux, ldd should output something like:
not a dynamic executable
This indicates that ffmpeg is statically linked.
Alternatively, use objdump (on Linux/macOS) or dumpbin (on Windows) to inspect the binary:
objdump -x ./ffmpeg | grep NEEDED
This should not list any dynamic library dependencies (e.g., libavcodec.so, libx264.so, etc.).
Congratulations! You now have a statically linked FFmpeg binary with the dependencies we've built.
Tips & Variations
- To reduce binary size, consider adding
--disable-stripping to the ./configure line and running strip -s ./ffmpeg afterwards.
- If you encounter issues, inspect
config.log and ffbuild/config.mak for clues.
- For Windows builds, use MinGW/MSYS2 and adjust the configure flags accordingly (e.g.,
--extra-libs="-lmingwex -lmingw32").
- To build shared FFmpeg with static dependencies, swap
--enable-static with --enable-shared and omit --disable-shared.
- Automate this process using scripts (e.g., Bash, Python) to manage dependencies and configurations.
Happy building!