Logo
FFmpeg Compilation Guide

Building FFmpeg 7 for Windows, Android & macOS

A comprehensive guide to compiling FFmpeg 7 using MSYS2 on Windows, cross-compiling for Android via NDK, and standard macOS builds, complete with usage examples.

Step 1: Essential Toolchains

Download and configure compilers for each platform

MSYS2

Provides a Unix-like environment on Windows, essential for running FFmpeg's configure script.

Setup Command (MSYS2 Terminal):
pacman -S base-devel mingw-w64-x86_64-toolchain yasm

Android NDK

Contains the Clang/LLVM toolchain required to cross-compile Android .so libraries.

Usage Check:
ls $NDK/toolchains/llvm/prebuilt

Homebrew

The missing package manager for macOS. Used to install FFmpeg compilation dependencies like yasm.

Install Command (macOS Terminal):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Step 2: Windows Build (x86_64)

Compile standard .dll and .exe for Windows

build_windows.sh
#!/bin/bash
# Ensure you are in the FFmpeg source directory inside MSYS2
./configure \
--prefix=./build_win64 \
--arch=x86_64 \
--target-os=mingw32 \
--enable-shared \
--disable-static \
--enable-gpl \
--enable-w32threads

make -j8
make install
Usage on Windows

The build generates ffmpeg.exe and DLLs like avcodec-61.dll. You can use it directly in CMD or link it in Visual Studio C++ projects.

CMD Example:
C:\ffmpeg\bin> ffmpeg -i input.mp4 -vcodec libx264 output.mkv

Step 3: Android NDK Cross-Compile

Generate .so libraries using Android NDK Toolchain

build_android_arm64.sh
#!/bin/bash
# Set NDK Path (Replace with your actual NDK path inside MSYS2)
NDK="/c/Users/YourUsername/AppData/Local/Android/Sdk/ndk/26.1.10909125"
TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/windows-x86_64
API=24

./configure \
--prefix=./build_android_arm64 \
--enable-cross-compile \
--target-os=android \
--arch=aarch64 \
--sysroot=$TOOLCHAIN/sysroot \
--cc=$TOOLCHAIN/bin/aarch64-linux-android$API-clang \
--cxx=$TOOLCHAIN/bin/aarch64-linux-android$API-clang++ \
--enable-shared --disable-static \
--disable-doc --disable-programs

make -j8 && make install
Android Integration

Copy the generated .so files (e.g., libavcodec.so) into your app's src/main/cpp/libs directory. Configure your CMakeLists.txt to link these libraries.

Kotlin JNI Loading:
init { System.loadLibrary("avutil") System.loadLibrary("avcodec") System.loadLibrary("your_native_lib") }

Step 4: macOS Compilation

Building standard Apple binaries

Reality Check: Cross-compiling Mac on Windows

Native cross-compilation for macOS on Windows is extremely complex and requires proprietary Apple SDKs (via tools like osxcross). In practice, developers run the following script on a real Mac, a macOS VM, or via GitHub Actions (macOS runners).

build_macos.sh (Run on Mac/CI)
#!/bin/bash
# Install dependencies via Homebrew first: brew install yasm
./configure \
--prefix=./build_mac \
--cc=clang \
--cxx=clang++ \
--enable-shared \
--disable-static \
--enable-videotoolbox # Apple 硬件加速

make -j$(sysctl -n hw.logicalcpu)
make install
Usage on macOS

This will generate .dylib libraries. You can link these in Xcode for your Swift/Objective-C projects using an Objective-C++ wrapper, or use the generated ffmpeg CLI.

Swift/ObjC Usage Idea:
import Foundation
// FFmpegWrapper.h (Objective-C++)
#include <libavcodec/avcodec.h>