在进行编译之前,需要做一些准备工作安装必备文件:
1 安装 gas-preprocessor
FFmpeg-iOS-build-script 自动编译脚本需要使用到 gas-preprocessor .
执行
sudo git clone https://github.com/bigsen/gas-preprocessor.git /usr/local/bin/gas
sudo cp /usr/local/bin/gas/gas-preprocessor.pl /usr/local/bin/gas-preprocessor.pl
sudo chmod 777 /usr/local/bin/gas-preprocessor.pl
sudo rm -rf /usr/local/bin/gas/
2 安装 yams
brew install yams
3 下载编译脚本
git clone https://github.com/kewlbear/FFmpeg-iOS-build-script.git
4 执行脚本
cd 到 FFmpeg-iOS-build-script目录
cd FFmpeg-iOS-build-script
./build-ffmpeg.sh
耐心等待,此脚本会自动下载脚本中对应的FFmpeg 版本,然后编译,最后将生成的库放到FFmpeg-IOS目录中。
脚本如下:
#!/bin/sh# ffmpeg版本
FF_VERSION="4.3.6"
#FF_VERSION="snapshot-git"
if [[ $FFMPEG_VERSION != "" ]]; thenFF_VERSION=$FFMPEG_VERSION
fi
SOURCE="ffmpeg-$FF_VERSION"
FAT="FFmpeg-iOS"SCRATCH="scratch"
# must be an absolute path
THIN=`pwd`/"thin"# absolute path to x264 library
#X264=`pwd`/fat-x264#FDK_AAC=`pwd`/../fdk-aac-build-script-for-iOS/fdk-aac-iosCONFIGURE_FLAGS="--enable-cross-compile --disable-debug --disable-programs \--disable-doc --enable-pic"if [ "$X264" ]
thenCONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-gpl --enable-libx264"
fiif [ "$FDK_AAC" ]
thenCONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-libfdk-aac --enable-nonfree"
fi# avresample
#CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-avresample"ARCHS="arm64 armv7 x86_64 i386"COMPILE="y"
LIPO="y"DEPLOYMENT_TARGET="10.0"if [ "$*" ]
thenif [ "$*" = "lipo" ]then# skip compileCOMPILE=elseARCHS="$*"if [ $# -eq 1 ]then# skip lipoLIPO=fifi
fiif [ "$COMPILE" ]
thenif [ ! `which yasm` ]thenecho 'Yasm not found'if [ ! `which brew` ]thenecho 'Homebrew not found. Trying to install...'ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" \|| exit 1fiecho 'Trying to install Yasm...'brew install yasm || exit 1fiif [ ! `which gas-preprocessor.pl` ]thenecho 'gas-preprocessor.pl not found. Trying to install...'(curl -L https://github.com/libav/gas-preprocessor/raw/master/gas-preprocessor.pl \-o /usr/local/bin/gas-preprocessor.pl \&& chmod +x /usr/local/bin/gas-preprocessor.pl) \|| exit 1fiif [ ! -r $SOURCE ]thenecho 'FFmpeg source not found. Trying to download...'curl http://www.ffmpeg.org/releases/$SOURCE.tar.bz2 | tar xj \|| exit 1fiCWD=`pwd`for ARCH in $ARCHSdoecho "building $ARCH..."mkdir -p "$SCRATCH/$ARCH"cd "$SCRATCH/$ARCH"CFLAGS="-arch $ARCH"if [ "$ARCH" = "i386" -o "$ARCH" = "x86_64" ]thenPLATFORM="iPhoneSimulator"CFLAGS="$CFLAGS -mios-simulator-version-min=$DEPLOYMENT_TARGET"elsePLATFORM="iPhoneOS"CFLAGS="$CFLAGS -mios-version-min=$DEPLOYMENT_TARGET -fembed-bitcode"if [ "$ARCH" = "arm64" ]thenEXPORT="GASPP_FIX_XCODE5=1"fifiXCRUN_SDK=`echo $PLATFORM | tr '[:upper:]' '[:lower:]'`CC="xcrun -sdk $XCRUN_SDK clang"# force "configure" to use "gas-preprocessor.pl" (FFmpeg 3.3)if [ "$ARCH" = "arm64" ]thenAS="gas-preprocessor.pl -arch aarch64 -- $CC"elseAS="gas-preprocessor.pl -- $CC"fiCXXFLAGS="$CFLAGS"LDFLAGS="$CFLAGS"if [ "$X264" ]thenCFLAGS="$CFLAGS -I$X264/include"LDFLAGS="$LDFLAGS -L$X264/lib"fiif [ "$FDK_AAC" ]thenCFLAGS="$CFLAGS -I$FDK_AAC/include"LDFLAGS="$LDFLAGS -L$FDK_AAC/lib"fiTMPDIR=${TMPDIR/%\/} $CWD/$SOURCE/configure \--target-os=darwin \--arch=$ARCH \--cc="$CC" \--as="$AS" \$CONFIGURE_FLAGS \--extra-cflags="$CFLAGS" \--extra-ldflags="$LDFLAGS" \--prefix="$THIN/$ARCH" \|| exit 1make -j4 install $EXPORT || exit 1cd $CWDdone
fiif [ "$LIPO" ]
thenecho "building fat binaries..."mkdir -p $FAT/libset - $ARCHSCWD=`pwd`cd $THIN/$1/libfor LIB in *.adocd $CWDecho lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB 1>&2lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB || exit 1donecd $CWDcp -rf $THIN/$1/include $FAT
fiecho Done
这里使用的是4.3.6版本,目前最新版本已经更新到6.0+
在ffmpeg源码目录下,终端执行 ./configure --help可用查看全部参数。根据自己需要可以修改脚本中对应的编译参数。
5 最新版编译报错问题
FFmpeg-5.0以上编译,报audiotoolbox错误
src/libavdevice/audiotoolbox.m:78:5: error: unknown type name 'AudioDeviceID'; did you mean 'AudioFileID'?
AudioDeviceID *devices;
^~~~~~~~~~~~~
AudioFileID
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS16.2.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AudioFile.h:197:35: note: 'AudioFileID' declared here
typedef struct OpaqueAudioFileID *AudioFileID;
^
src/libavdevice/audiotoolbox.m:84:5: error: use of undeclared identifier 'AudioObjectPropertyAddress'
AudioObjectPropertyAddress prop;
audiotoolbox 是对音频的硬编解码库。可以在 build-ffmpeg.sh 中的在CONFIGURE_FLAGS 后面字符串中加入: --disable-audiotoolbox 字符串就可以解决
CONFIGURE_FLAGS="--enable-cross-compile --disable-debug --disable-programs \--disable-doc --enable-pic --disable-audiotoolbox"
另外注意:FFmpeg5.0以上的编译最低支持的系统版本是iOS 13.0。而且iOS 13.0以后不再支持i386架构,所以编译时要去掉 i386架构。