macOS--编译FFmpeg For Android

柳宏深
2023-12-01

介绍

FFmpeg Android build script

测试环境:

  • FFmpeg 4.4.1
  • NDK-R22d
  • MacOS 11.6.1 (20G221)

使用方法

配置脚本

# 编译平台
# "aarch64 arm"
ARCH=""
# 目标Android版本
API="24"
# 支持的CPU架构
CPU="armv8-a"
# so库输出目录
OUTPUT="../FFMpeg-$CPU-Android"
# NDK的路径
NDK=""

进入到当前目录直接执行脚本即可,如遇无法执行,可能是文件权限问题

# 进入目录
cd android-ffmpeg-build-script-ilongge
cd ffmpeg-4.4.1
# 赋予脚本可执行权限
chmod +x buidl-ffmpeg-android.sh
# 执行脚本
./buidl-ffmpeg-android.sh  

Thanks

本脚本是摘抄自 Mac环境下编译ffmpeg生成so库文件-CSDN

学习后加以改造

感谢原作者!!!

Gitee仓库
Github仓库

脚本内容

#!/bin/bash

# 编译平台
# "aarch64 arm"
ARCH=""
# 目标Android版本
API="24"
# 支持的CPU架构 armv7-a armv8-a
CPU="armv8-a"
# 动态度/DYNAMIC  静态库/STATIC
LIBRARY="DYNAMIC"
# so库输出目录
OUTPUT="FFMpeg-Android$API"
# NDK的路径
NDK="/Users/ilongge/PersonalCode/AndroidNDK/android-ndk-r22b"
if [ ! -r "$NDK" ]; then
	echo "请配置NDK路径!!!"
	exit 1
fi
# 编译工具链路径
TOOLCHAIN="$NDK/toolchains/llvm/prebuilt/darwin-x86_64"
# 编译环境
SYSROOT="$TOOLCHAIN/sysroot"
# 编译参数
CONFIGURE_FLAGS=""

if [ $LIBRARY = "DYNAMIC" ]; then
	CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-shared"
	CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-static"
elif [ $LIBRARY = "STATIC" ]; then
	CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-static"
	CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-shared" 
	sed -i "" "s/SLIBNAME_WITH_MAJOR='\$(SLIBNAME).\$(LIBMAJOR)'/SLIBNAME_WITH_MAJOR='\$(SLIBPREF)\$(FULLNAME)-\$(LIBMAJOR)\$(SLIBSUF)'/" configure
	sed -i "" "s/LIB_INSTALL_EXTRA_CMD='\$\$(RANLIB) \"\$(LIBDIR)\\/\$(LIBNAME)\"'/LIB_INSTALL_EXTRA_CMD='\$\$(RANLIB) \"\$(LIBDIR)\\/\$(LIBNAME)\"'/" configure
	sed -i "" "s/SLIB_INSTALL_NAME='\$(SLIBNAME_WITH_VERSION)'/SLIB_INSTALL_NAME='\$(SLIBNAME_WITH_MAJOR)'/" configure
	sed -i "" "s/SLIB_INSTALL_LINKS='\$(SLIBNAME_WITH_MAJOR) \$(SLIBNAME)'/SLIB_INSTALL_LINKS='\$(SLIBNAME)'/" configure
fi

CROSS_PREFIX_CLANG="$TOOLCHAIN/bin"
EXTRA_CFFLAGS="-fPIC"
CROSS_PREFIX=""
LIBGCC=""
STRIP=""
LD=""
if [ $CPU = "armv8-a" ]; then
	OUTPUT="$OUTPUT-armv8a-$LIBRARY"
	ARCH="aarch64"
	CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-x86asm"
	CROSS_PREFIX="$CROSS_PREFIX_CLANG/$ARCH-linux-android-"
	CROSS_PREFIX_CLANG="$CROSS_PREFIX_CLANG/$ARCH-linux-android$API-"
	LIBGCC=$TOOLCHAIN/lib/gcc/$ARCH-linux-android/4.9.x/libgcc.a
	STRIP=$TOOLCHAIN/bin/$ARCH-linux-android-strip
	LD=$TOOLCHAIN/$ARCH-linux-android/bin/ld
elif [ $CPU = "armv7-a" ]; then
	OUTPUT="$OUTPUT-armv7a-$LIBRARY"
	ARCH="arm"
	CROSS_PREFIX="$CROSS_PREFIX_CLANG/arm-linux-androideabi-"
	CROSS_PREFIX_CLANG="$CROSS_PREFIX_CLANG/armv7a-linux-androideabi$API-"
	CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-asm"
	LIBGCC=$TOOLCHAIN/lib/gcc/arm-linux-androideabi/4.9.x/libgcc.a
	STRIP=$TOOLCHAIN/bin/arm-linux-androideabi-strip 
	LD=$TOOLCHAIN/arm-linux-androideabi/bin/ld
fi

CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-cross-compile"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-debug"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-programs"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-doc"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-ffplay"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-ffprobe"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-symver"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-ffmpeg"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-avdevice"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-pic"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-neon"

echo "./configure"
echo "    --prefix=$OUTPUT"
echo "    --target-os=android"
echo "    --arch=$ARCH"
echo "    --cpu=$CPU"
for FLAG in $CONFIGURE_FLAGS; do
	echo "    "$FLAG
done
echo "    --sysroot=$SYSROOT"
echo "    --cross-prefix=$CROSS_PREFIX""clang"
echo "    --cross-prefix-clang=$CROSS_PREFIX_CLANG""clang"
echo "    --extra-cflags=$EXTRA_CFFLAGS"

./configure \
	--prefix=$OUTPUT \
	--target-os=android \
	--arch=$ARCH \
	--cpu=$CPU \
	$CONFIGURE_FLAGS \
	--sysroot=$SYSROOT \
	--cross-prefix=$CROSS_PREFIX \
	--cross-prefix-clang=$CROSS_PREFIX_CLANG \
	--extra-cflags=$EXTRA_CFFLAGS || exit 1
#获取机器CPU核心数 尽可能加快编译
THREAD_COUNT=$(sysctl -n machdep.cpu.thread_count)
echo "make -j $THREAD_COUNT install"
make -j$THREAD_COUNT install || exit 1

echo "开始清理编译生成的中间文件"
make clean
echo "清理完成"
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "+  Congratulations ! ! !                            +"
echo "+  Build FFMpeg-Android Success ! ! !               +"
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
 类似资料: