How to Compile/Build Latest ffmpeg for Android and Use it in Android Studio 2024

Share this Article

FFMPEG is one of the most popular or powerful media processing library with multiple platform support and it is capable of doing Most of Media processing task.

How to Compile Latest FFMPEG for Android with Latest Android NDK.

1. Prepare Your Host Computer.

  • Make sure to use Linux operating system or any Virtualization if You don’t have Linux.
  • You can use Oracle VMBox if you are using Windows or Mac.
  • Download Linux image for Virtualbox from Here.

2. Now Open Your Virtual Machine or PC.

3. And Download required files and packages.

4. And if you are using a Virtual machine then make Sure You Downloaded These files inside your virtual machine.

5. Now Create a Directory and put ffmpeg source and android ndk files inside this directory.

6. Now Open Terminal inside this Directory and Run following command.

sudo apt install build-essential

7. And after that create a new file named android.sh in this directory.

8. Now open android.sh file using any text editor and paste following code in it.

  • You can Customize FFMPEG Modules to Enable or Disable just use appropriate Flags.
#!/bin/bash

### Describe Your Target Architectures ###
ARCH_LIST=( "armv8a" "armv7a" "x86" "x86-64") 

### Change As per Yours ####
ANDROID_API_LEVEL="21"
ANDROID_NDK_PATH="/home/kali/Desktop/ffmpeg-build/android-ndk-r26d"
FFMPEG_SOURCE_DIR="/home/kali/Desktop/ffmpeg-build/ffmpeg"
FFMPEG_BUILD_DIR="/home/kali/Desktop/ffmpeg-build/ffmpeg_android"


### Enable FFMPEG BUILD MODULES ####
ENABLED_CONFIG="\
        --enable-small \
		--enable-avcodec \
		--enable-avformat \
		--enable-avutil \
		--enable-swscale \
		--enable-swresample \
		--enable-demuxers \
		--enable-parser=* \
		--enable-decoders \
		--enable-shared "


### Disable FFMPEG BUILD MODULES ####
DISABLED_CONFIG="\
		--disable-zlib \
		--disable-v4l2-m2m \
		--disable-cuda-llvm \
		--disable-indevs \
		--disable-libxml2 \
		--disable-avdevice \
		--disable-network \
		--disable-static \
		--disable-debug \
		--disable-ffplay \
		--disable-ffprobe \
		--disable-doc \
		--disable-symver \
		--disable-gpl \
		--disable-programs "



### Dont Change ####
SYSROOT="$ANDROID_NDK_PATH/toolchains/llvm/prebuilt/linux-x86_64/sysroot"
LLVM_AR="$ANDROID_NDK_PATH/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar"
LLVM_NM="$ANDROID_NDK_PATH/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-nm"
LLVM_RANLIB="$ANDROID_NDK_PATH/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ranlib"
LLVM_STRIP="$ANDROID_NDK_PATH/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip"


configure_ffmpeg(){
   TARGET_ARCH=$1
   TARGET_CPU=$2
   CROSS_PREFIX=$3
   EXTRA_CFLAGS=$4
   EXTRA_CONFIG=$5
   
   CLANG="${CROSS_PREFIX}clang"
   CLANGXX="${CROSS_PREFIX}clang++"
   PREFIX="${FFMPEG_BUILD_DIR}/$TARGET_ARCH-$ANDROID_API_LEVEL"
   
   cd $FFMPEG_SOURCE_DIR
   
   ./configure \
   --disable-everything \
   --target-os=android \
   --arch=$TARGET_ARCH \
   --cpu=$TARGET_CPU \
   --enable-cross-compile \
   --cross-prefix="$CROSS_PREFIX" \
   --cc="$CLANG" \
   --cxx="$CLANGXX" \
   --sysroot="$SYSROOT" \
   --prefix="$PREFIX" \
   --extra-cflags="-fPIC -DANDROID $EXTRA_CFLAGS" \
   --extra-ldflags="-L$SYSROOT/usr/lib/$TARGET_ARCH-linux-android/$ANDROID_API_LEVEL" \
   ${ENABLED_CONFIG} \
   ${DISABLED_CONFIG} \
   --ar="$LLVM_AR" \
   --nm="$LLVM_NM" \
   --ranlib="$LLVM_RANLIB" \
   --strip="$LLVM_STRIP" \
   ${EXTRA_CONFIG}

   make clean
   make -j2
   make install -j2
   
}

echo -e "\e[1;32mCompiling FFMPEG for Android...\e[0m"

for ARCH in "${ARCH_LIST[@]}"; do
    case "$ARCH" in
        "armv8-a"|"aarch64"|"arm64-v8a"|"armv8a")
            echo -e "\e[1;32m$ARCH Libraries\e[0m"
            TARGET_ARCH="aarch64"
            TARGET_CPU="armv8-a"
            TARGET_ABI="aarch64"
            CROSS_PREFIX="$ANDROID_NDK_PATH/toolchains/llvm/prebuilt/linux-x86_64/bin/$TARGET_ABI-linux-android${ANDROID_API_LEVEL}-"
            EXTRA_CFLAGS="-O3 -marm -march=$TARGET_CPU -mfpu=neon -fomit-frame-pointer"
            EXTRA_CONFIG="\
            		--enable-neon "
            ;;
        "armv7-a"|"armeabi-v7a"|"armv7a")
            echo -e "\e[1;32m$ARCH Libraries\e[0m"
            TARGET_ARCH="arm"
            TARGET_CPU="armv7-a"
            TARGET_ABI="armv7a"
            CROSS_PREFIX="$ANDROID_NDK_PATH/toolchains/llvm/prebuilt/linux-x86_64/bin/$TARGET_ABI-linux-androideabi${ANDROID_API_LEVEL}-"
            EXTRA_CFLAGS="-O3 -marm -march=$TARGET_CPU -mfpu=neon -fomit-frame-pointer"
            EXTRA_CONFIG="\
            		--disable-armv5te \
            		--disable-armv6 \
            		--disable-armv6t2 \
            		--enable-neon "
            ;;
        "x86-64"|"x86_64")
            echo -e "\e[1;32m$ARCH Libraries\e[0m"
            TARGET_ARCH="x86_64"
            TARGET_CPU="x86-64"
            TARGET_ABI="x86_64"
            CROSS_PREFIX="$ANDROID_NDK_PATH/toolchains/llvm/prebuilt/linux-x86_64/bin/$TARGET_ABI-linux-android${ANDROID_API_LEVEL}-"
            EXTRA_CFLAGS="-O3 -march=$TARGET_CPU -fomit-frame-pointer"
            		
            EXTRA_CONFIG="\
            		  "
            ;;
        "x86"|"i686")
            echo -e "\e[1;32m$ARCH Libraries\e[0m"
            TARGET_ARCH="i686"
            TARGET_CPU="i686"
            TARGET_ABI="i686"
            CROSS_PREFIX="$ANDROID_NDK_PATH/toolchains/llvm/prebuilt/linux-x86_64/bin/$TARGET_ABI-linux-android${ANDROID_API_LEVEL}-"
            EXTRA_CFLAGS="-O3 -march=$TARGET_CPU -fomit-frame-pointer"
            EXTRA_CONFIG="\
            		--disable-asm "
            ;;
           * )
            echo "Unknown architecture: $ARCH"
            exit 1
            ;;
    esac
    configure_ffmpeg "$TARGET_ARCH" "$TARGET_CPU" "$CROSS_PREFIX" "$EXTRA_CFLAGS" "$EXTRA_CONFIG"
done

9. Now open Terminal where you created android.sh file and enter following commands.

chmod +x android.sh
./android.sh

10. And Now Your ffmpeg Compilation is Begins and make sure you mentioned your ffmpeg source directory or android ndk directory in the above script.

Also Watch this Video Tutorial:


How to use ffmpeg in Android studio Projects.

1. Download or Clone Precompiled ffmpeg Android GitHub Repository.

  • You can use Your Own Compiled FFMPEG .iso files if you compiled using above compilation Tutorial.

2. Now Create jniLibs folder inside your projects /main folder.

Example \<Your Project Directory>\<Your Project>\src\main\jniLibs

3. Copy and paste these ffmpegAndroid files inside jniLibs folder.

4. And after that add following code in Your cmakeList.txt file.


set(IMPORT_DIR ${CMAKE_SOURCE_DIR}/../jniLibs)

# FFmpeg include file
include_directories(${IMPORT_DIR}/${ANDROID_ABI}/include)
# Codec library
add_library(
        avcodec
        SHARED
        IMPORTED
)
set_target_properties(
        avcodec
        PROPERTIES IMPORTED_LOCATION
        ${IMPORT_DIR}/${ANDROID_ABI}/libavcodec.so
)
# The filter library is temporarily out of use
add_library(
        avfilter
        SHARED
        IMPORTED
)
set_target_properties(
        avfilter
        PROPERTIES IMPORTED_LOCATION
        ${IMPORT_DIR}/${ANDROID_ABI}/libavfilter.so
)

# File format libraries are required for most operations
add_library(
        avformat
        SHARED
        IMPORTED
)

set_target_properties(
        avformat
        PROPERTIES IMPORTED_LOCATION
        ${IMPORT_DIR}/${ANDROID_ABI}/libavformat.so
)

# Tool library
add_library(
        avutil
        SHARED
        IMPORTED
)
set_target_properties(
        avutil
        PROPERTIES IMPORTED_LOCATION
        ${IMPORT_DIR}/${ANDROID_ABI}/libavutil.so
)

# The resampling library is mainly used for audio conversion.
add_library(
        swresample
        SHARED
        IMPORTED
)
set_target_properties(
        swresample
        PROPERTIES IMPORTED_LOCATION
        ${IMPORT_DIR}/${ANDROID_ABI}/libswresample.so
)

# Video format conversion library is mainly used for video conversion.
add_library(
        swscale
        SHARED
        IMPORTED
)
set_target_properties(
        swscale
        PROPERTIES IMPORTED_LOCATION
        ${IMPORT_DIR}/${ANDROID_ABI}/libswscale.so
)


# The main android library, native window, requires this library
target_link_libraries(
        <Your-Native-Library>
        ${log-lib}
        android
        avcodec
        avfilter
        avformat
        avutil
        swresample
        swscale
)

5. Also Add following code in your Module build.gradle file.

defaultConfig {
        //............//
        ndk.abiFilters  'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++14 -fexceptions -frtti"
                arguments "-DANDROID_STL=c++_shared"
            }
        }

    }

6. And now just rebuild your project and start using ffmpeg c++ libraries in your Android Studio Project.


Share this Article

By MOHD UMIR

Video Games Lover By Passion. Programmer by skills and part time Blogger by Hobby. And Owner of UMIRGAMING.COM and UMIRTECH.COM

Related Articles

2 thoughts on “How to Compile/Build Latest ffmpeg for Android and Use it in Android Studio 2024”
  1. Hi , Thank you for code and article, followed your steps but
    stddef.h
    stdint.h
    and many other files not found

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.