Mac系统下编译FFmpeg(支持libx264)for iOS

前言

iOS平台的FFmpeg(支持libx264)已经编译完成。经过之前Android平台上的编译过程,iOS平台的编译总体来说少走了很多弯路,但还是遇到了一些问题,现在做一下总结。

一、编译x264

首先还是要编译x264。
1、在Mac上新建一个工作目录workplace,x264的源码下载不再赘述,将之前下载的x264源码包解压到workplace目录下,得到x264-snapshot-20190111-2245文件夹。
2、网上有现成的x264-iOS编译脚本,我们无需自己新建,直接下载然后简单修改就行。
下载地址:x264-iOS编译脚本下载
build-x264.sh脚本文件放在workplace目录下,跟x264-snapshot-20190111-2245文件夹同级,将脚本中的SOURCE修改为自己的x264源码目录,最终修改后的脚本内容如下:

#!/bin/sh

CONFIGURE_FLAGS="--enable-static --enable-pic --disable-cli"

ARCHS="arm64 armv7 armv7s x86_64 i386"

# directories
SOURCE="x264-snapshot-20190111-2245"
FAT="x264-iOS"

SCRATCH="scratch-x264"
# must be an absolute path
THIN=`pwd`/"thin-x264"

COMPILE="y"
LIPO="y"

if [ "$*" ]
then
	if [ "$*" = "lipo" ]
	then
		# skip compile
		COMPILE=
	else
		ARCHS="$*"
		if [ $# -eq 1 ]
		then
			# skip lipo
			LIPO=
		fi
	fi
fi

if [ "$COMPILE" ]
then
	CWD=`pwd`
	for ARCH in $ARCHS
	do
		echo "building $ARCH..."
		mk "$SCRATCH/$ARCH"
		cd "$SCRATCH/$ARCH"
		CFLAGS="-arch $ARCH"
                ASFLAGS=

		if [ "$ARCH" = "i386" -o "$ARCH" = "x86_64" ]
		then
		    PLATFORM="iPhoneSimulator"
		    CPU=
		    if [ "$ARCH" = "x86_64" ]
		    then
		    	CFLAGS="$CFLAGS -mios-simulator-version-min=7.0"
		    	HOST=
		    else
		    	CFLAGS="$CFLAGS -mios-simulator-version-min=5.0"
			HOST="--host=i386-apple-darwin"
		    fi
		else
		    PLATFORM="iPhoneOS"
		    if [ $ARCH = "arm64" ]
		    then
		        HOST="--host=aarch64-apple-darwin"
			XARCH="-arch aarch64"
		    else
		        HOST="--host=arm-apple-darwin"
			XARCH="-arch arm"
		    fi
                    CFLAGS="$CFLAGS -fembed-bitcode -mios-version-min=7.0"
                    ASFLAGS="$CFLAGS"
		fi

		XCRUN_SDK=`echo $PLATFORM | tr '[:upper:]' '[:lower:]'`
		CC="xcrun -sdk $XCRUN_SDK clang"
		if [ $PLATFORM = "iPhoneOS" ]
		then
		    export AS="$CWD/$SOURCE/tools/gas-preprocessor.pl $XARCH -- $CC"
		else
		    export -n AS
		fi
		CXXFLAGS="$CFLAGS"
		LDFLAGS="$CFLAGS"

		CC=$CC $CWD/$SOURCE/configure \
		    $CONFIGURE_FLAGS \
		    $HOST \
		    --extra-cflags="$CFLAGS" \
		    --extra-asflags="$ASFLAGS" \
		    --extra-ldflags="$LDFLAGS" \
		    --prefix="$THIN/$ARCH" || exit 1

		make -j3 install || exit 1
		cd $CWD
	done
fi

if [ "$LIPO" ]
then
	echo "building fat binaries..."
	mkdir -p $FAT/lib
	set - $ARCHS
	CWD=`pwd`
	cd $THIN/$1/lib
	for LIB in *.a
	do
		cd $CWD
		lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB
	done

	cd $CWD
	cp -rf $THIN/$1/include $FAT
fi

3、打开Mac终端,进入workplace目录,执行build-x264.sh脚本文件,等待编译完成。
4、编译成功以后,workplace目录下会生成“scratch-x264”、“thin-x264”和“x264-iOS”三个文件夹,其中“thin-x264”里分目录存放了arm64、armv7、armv7s、x86-64和i386等架构的include文件和lib文件,而“x264-iOS”里存放的是经过lipo集成的lib文件,我们在编译FFmpeg时使用的也是“x264-iOS”的文件。

编译过程中遇到的问题:
a. 我的Mac上nasm版本过低,导致编译不过,最终从nasm官网上下载了最新的nasm文件替换掉/usr/bin里的nasm才编译通过。

二、编译FFmpeg

1、将之前下载的FFmpeg源码包解压到workplace目录下,得到ffmpeg-3.4.5文件夹。
2、FFmpeg-iOS的编译脚本网上也有,我们也是直接下载后简单修改使用。
下载地址:FFmpeg-iOS编译脚本下载
主要对一些指定目录和configure配置项做了修改,最终修改后的脚本内容如下:

#!/bin/sh

# directories
FF_VERSION="3.4.5"
#FF_VERSION="snapshot-git"
if [[ $FFMPEG_VERSION != "" ]]; then
  FF_VERSION=$FFMPEG_VERSION
fi
SOURCE="ffmpeg-$FF_VERSION"
FAT="FFmpeg-iOS"

SCRATCH="scratch-FFmpeg"
# must be an absolute path
THIN=`pwd`/"thin-FFmpeg"

# absolute path to x264 library
#X264=`pwd`/fat-x264
X264=`pwd`/x264-iOS

#FDK_AAC=`pwd`/../fdk-aac-build-script-for-iOS/fdk-aac-ios

CONFIGURE_FLAGS="--enable-static \
--disable-shared \
--enable-small \
--enable-runtime-cpudetect \
--disable-programs \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-doc \
--enable-pthreads \
--disable-decoders \
--enable-decoder=h264 \
--disable-encoders \
--enable-encoder=mjpeg \
--disable-hwaccels \
--disable-parsers \
--enable-parser=h264 \
--enable-parser=mjpeg \
--disable-demuxers \
--enable-muxers \
--disable-protocols \
--enable-protocol=file \
--disable-filters \
--disable-bsfs \
--disable-indevs \
--disable-outdevs \
--disable-v4l2_m2m \
--enable-cross-compile --disable-debug \
--disable-symver \
--enable-asm \
--enable-neon \
"

if [ "$X264" ]
then
	CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-gpl --enable-libx264"
fi

if [ "$FDK_AAC" ]
then
	CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-libfdk-aac --enable-nonfree"
fi

# avresample
#CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-avresample"

ARCHS="arm64 armv7 armv7s x86_64 i386"

COMPILE="y"
LIPO="y"

DEPLOYMENT_TARGET="8.0"

if [ "$*" ]
then
	if [ "$*" = "lipo" ]
	then
		# skip compile
		COMPILE=
	else
		ARCHS="$*"
		if [ $# -eq 1 ]
		then
			# skip lipo
			LIPO=
		fi
	fi
fi

if [ "$COMPILE" ]
then
	if [ ! `which yasm` ]
	then
		echo 'Yasm not found'
		if [ ! `which brew` ]
		then
			echo 'Homebrew not found. Trying to install...'
                        ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" \
				|| exit 1
		fi
		echo 'Trying to install Yasm...'
		brew install yasm || exit 1
	fi
	if [ ! `which gas-preprocessor.pl` ]
	then
		echo '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 1
	fi

	if [ ! -r $SOURCE ]
	then
		echo 'FFmpeg source not found. Trying to download...'
		curl http://www.ffmpeg.org/releases/$SOURCE.tar.bz2 | tar xj \
			|| exit 1
	fi

	CWD=`pwd`
	for ARCH in $ARCHS
	do
		echo "building $ARCH..."
		mkdir -p "$SCRATCH/$ARCH"
		cd "$SCRATCH/$ARCH"

		CFLAGS="-arch $ARCH"
		if [ "$ARCH" = "i386" -o "$ARCH" = "x86_64" ]
		then
		    PLATFORM="iPhoneSimulator"
		    CFLAGS="$CFLAGS -mios-simulator-version-min=$DEPLOYMENT_TARGET"
		else
		    PLATFORM="iPhoneOS"
		    CFLAGS="$CFLAGS -mios-version-min=$DEPLOYMENT_TARGET"
		    if [ "$ARCH" = "arm64" ]
		    then
		        EXPORT="GASPP_FIX_XCODE5=1"
		    fi
		fi

		XCRUN_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" ]
		then
		    AS="gas-preprocessor.pl -arch aarch64 -- $CC"
		else
		    AS="gas-preprocessor.pl -- $CC"
		fi

		CXXFLAGS="$CFLAGS"
		LDFLAGS="$CFLAGS"
		if [ "$X264" ]
		then
			CFLAGS="$CFLAGS -I$X264/include"
			LDFLAGS="$LDFLAGS -L$X264/lib"
		fi
		if [ "$FDK_AAC" ]
		then
			CFLAGS="$CFLAGS -I$FDK_AAC/include"
			LDFLAGS="$LDFLAGS -L$FDK_AAC/lib"
		fi

		TMPDIR=${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 1

		make -j3 install $EXPORT || exit 1
		cd $CWD
	done
fi

if [ "$LIPO" ]
then
	echo "building fat binaries..."
	mkdir -p $FAT/lib
	set - $ARCHS
	CWD=`pwd`
	cd $THIN/$1/lib
	for LIB in *.a
	do
		cd $CWD
		echo lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB 1>&2
		lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB || exit 1
	done

	cd $CWD
	cp -rf $THIN/$1/include $FAT
fi

echo Done

3、执行build-ffmpeg.sh脚本文件,等待编译完成。
4、编译成功以后,workplace目录下会生成“scratch-FFmpeg”、“thin-FFmpeg”和“FFmpeg-iOS”三个文件夹,其中“thin-FFmpeg”里分目录存放了arm64、armv7、armv7s、x86-64和i386等架构的include文件和lib文件,而“FFmpeg-iOS”里存放的是经过lipo集成的lib文件,我们在后续编译接口库framework时引用的是“FFmpeg-iOS”文件夹。

编译过程中遇到的问题:
a. build-ffmpeg.sh脚本会自动下载gas-preprocessor.pl文件放到/usr/local/bin目录下,如果Mac未联网,需要从其他电脑上下载gas-preprocessor.pl文件放到/usr/local/bin目录下(gas-preprocessor.pl下载地址)

你可能感兴趣的:(开源软件-FFmpeg)