Mac下编译FFmpeg(iOS平台)

    此文件主要目的是用FFmpeg编译iOS平台的静态库,网上此类文章较多,我就不在此过多陈述,以下脚本均由本人测试及正常使用过后总结。若CSDN积分富裕,可直接下载脚本按照上面注意事项修改之后进行编译:传送门。

环境

FFmpeg:3.4.2   

Mac:10.13.5

Xcode:9.4

iOS Target:8.0


脚本

#!/bin/sh
#armv7xcode9.1,
#sudo xcode-select -switch pathToXcode9.1/Contents/Developer
#xcode-select --print-path

#需要编译FFpmeg版本号
FF_VERSION="3.4.2"
if [[ $FFMPEG_VERSION != "" ]]; then
  FF_VERSION=$FFMPEG_VERSION
fi
SOURCE="ffmpeg-$FF_VERSION"
#输出路径
FAT="FFmpeg-iOS"

SCRATCH=`pwd`/"scratch"
THIN=`pwd`/"thin"
#编译之前需要删除临时缓存文件夹,防止和之前编译的或fdk-aac冲突
rm -rf "$SCRATCH"
rm -rf "$THIN"

#若不需要x264,fdk-aac可屏蔽以下2行
#X264=`pwd`/x264-iOS
#FDK_AAC=`pwd`/fdk-aac-ios

CONFIGURE_FLAGS="--enable-cross-compile --disable-debug --disable-programs --disable-doc --enable-pic --enable-static --disable-shared"

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

ARCHS="armv7 armv7s arm64 x86_64 i386"

COMPILE="y"
LIPO="y"

DEPLOYMENT_TARGET="8.0"

if [ "$*" ]
then
	if [ "$*" = "lipo" ]
    then
		COMPILE=
	else
		ARCHS="$*"
		if [ $# -eq 1 ]
		then
			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 "$SOURCE 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 -fembed-bitcode"
		    if [ "$ARCH" = "arm64" ]
		    then
		        EXPORT="GASPP_FIX_XCODE5=1"
		    fi
		fi

		XCRUN_SDK=`echo $PLATFORM | tr '[:upper:]' '[:lower:]'`
		CC="xcrun -sdk $XCRUN_SDK clang"

		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 "iOS FFmpeg bulid success!"

将上面脚本代码粘贴到build-ffmpeg-iOS.sh中,修改脚本执行权限:

sudo chmod 777 build-ffmpeg-iOS.sh

闲话

1、脚本文件参考kewlbear,本人略作了修改;

2、FFmpeg源码不需要下载,脚本中已包含自动下载功能,若编译其他版本可修改FF_VERSION="3.4.2"中的值;

3、此脚本已包含armv7框架,若不需要移除ARCHS="armv7 armv7s arm64 x86_64 i386"中的armv7。若使用Xcode编译出错,需要自行下载Xcode9或Xcode9.1,执行Xcode编译环境切换命令:

sudo xcode-select -switch Xcode9.1路径/Contents/Developer

4、脚本已关闭x264、fdk-aac的编译连接,若需要可自行打开。需要x264、fdk-aac的编译脚本,可下载上面的资源。

5、若有什么问题或建议,欢迎留言。


FFmpeg3.1.1版本已编译库:传送门,大于此版本的库因裁减较少,资源较大,请自行编译。

你可能感兴趣的:(音视频,FFmpeg)