Building FFmpeg from source for Android on macOS | Qt Multimedia | Qt 6.11.0
Description: This document describes how to build FFmpeg from source code.

- Qt 6.11
- Qt Multimedia
- Building Qt Multimedia from sources
- Building FFmpeg from source
- Building FFmpeg from source for Android on macOS
- Qt 6.11.0 Reference Documentation

Building FFmpeg for Android on Windows ◦ Building FFmpeg for Android on Linux

# Building FFmpeg from source for Android on macOS

This page explains how to configure and build FFmpeg for Android. Compilation for Android is a cross-compilation and presumes using macOS as a host system. The required steps are:

- Get the FFmpeg source code.
- Install the required dependencies.
- Configure FFmpeg from the command line.
- Build the development libraries.

## Get the FFmpeg source code

You can get the FFmpeg source code in these ways:

- Download from the FFmpeg download page.
- Clone from git. For example, this command clones the version 7.1.3 of the FFmpeg sources to `~/ffmpeg`.

       git clone --branch n7.1.3 https://git.ffmpeg.org/ffmpeg.git ffmpeg

It is recommended to use the same FFmpeg version as documented in the Qt Multimedia main page.

The following paragraphs assumes that you store the FFmpeg source code under `~/ffmpeg`.

## Prerequisites

To build FFmpeg, these tools and packages are required:

- Homebrew.
- Homebrew packages (yasm).

You'll also need the Android NDK, SDK, and JDK installed and configured. You can find more information on setting these up here Getting Started with Qt for Android.

### Installing Homebrew

To install Homebrew, run:

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

### Installing Homebrew packages

To install Homebrew package Yasm, run:

     brew install yasm

## Set environment variables

This part is not strictly necessary but it'll help with keeping configure command a bit cleaner and shorter.

The following command assumes that Andorid SDK is installed to `~/Library/Android/sdk` and Android NDK version is 26.1.10909125.

     export ANDROID_NDK_ROOT=~/Library/Android/sdk/ndk/26.1.10909125

It is recommended to use the same NDK version as documented in the Getting Started with Qt for Android.

The architecture you should build for depends on the target devices:

- aarch64 (ARM64-v8a): Used by most modern Android devices (64-bit).
- armv7 (armeabi-v7a): For older 32-bit ARM devices.
- x86: Mainly for Android emulators running on Intel processors.
- x86_64: For 64-bit Intel-based emulators or specialized devices.

Setup architecture-specific variables

- aarch64

       export ARCH=aarch64
       export TOOLCHAIN_ARCH=aarch64-linux-android
       export CPU=armv8-a

- armv7

       export ARCH=armv7
       export TOOLCHAIN_ARCH=armv7a-linux-androideabi
       export CPU=armv7-a

- x86

       export ARCH=x86
       export TOOLCHAIN_ARCH=i686-linux-android
       export CPU=i686

- x86_64

       export ARCH=x86_64
       export TOOLCHAIN_ARCH=x86_64-linux-android
       export CPU=x86-64

## Configuring and building FFmpeg

Create a `build-android` directory inside the `~/ffmpeg` directory and navigate into it:

     mkdir ~/ffmpeg/build-android
     cd ~/ffmpeg/build-android

To configure FFmpeg, run:

     ../configure --prefix=../install-android --disable-doc --enable-network --enable-shared \
         --host-os=darwin-x86_64 --target-os=android \
         --enable-cross-compile --arch=${ARCH} --cpu=${CPU} \
         --enable-jni --enable-mediacodec \
         --sysroot=${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/darwin-x86_64/sysroot \
         --sysinclude=${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/ \
         --cc=${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/darwin-x86_64/bin/${TOOLCHAIN_ARCH}24-clang \
         --cxx=${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/darwin-x86_64/bin/${TOOLCHAIN_ARCH}24-clang++ \
         --strip=${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/darwin-x86_64/bin/llvm-strip

The `--prefix` argument specifies a path where the FFmpeg development libraries are installed after building. The documentation is not needed, but network features should be enabled. To build FFmpeg as static libraries, omit the `--enable-shared` option.

If you're building FFmpeg with a security backend, you have 4 options to choose from (the same as when building for Linux) but only OpenSSL is tested by QtMultimedia maintainers for now. Choose the appropriate option and add it during FFmpeg configuration:

     --enable-openssl # For OpenSSL
     --enable-gnutls # For GnuTLS
     --enable-libtls # For LibreSSL (libtls)
     --enable-mbedtls # For MbedTLS

If you're using OpenSSL you also need to add following options during FFmpeg configuration. Don't forget to replace `<ANDROID_OPENSSL_INCLUDE_DIR>` and `<ANDROID_OPENSSL_LIBS_DIR>` with actual paths.

     --extra-cflags=-I<ANDROID_OPENSSL_INCLUDE_DIR> --extra-ldflags=-L<ANDROID_OPENSSL_LIBS_DIR>

If security backend is included, you should take care about its delivery yourself, ensuring correct libraries are installed on target platform or using stubs. The OpenSSL libraries that are linked must be called `libssl.so` and `libcrypto.so`, without any versioning suffixes. The user has to guarantee that libraries are of the same ABI version as OpenSSL headers FFmpeg was compiled with. For more information, see Adding OpenSSL Support for Android.

Google Play requires 16KB page sizes, to build compatible FFmpeg binaries, add the following configuration options to FFmpeg.

     --extra-ldflags=-Wl,-z,max-page-size=16384

For Google Play requirements, see Google Play 16 KB page size compatibility requirement.

Once the `configure` command finishes, build and install FFmpeg using the `make` command.

     make -j install

If the build completes without errors, FFmpeg development libraries are installed in the `../install-android` directory. If you build Qt Multimedia, this path is stored in the `FFMPEG_DIR` variable used when configuring Qt Multimedia.

Building FFmpeg for Android on Windows ◦ Building FFmpeg for Android on Linux