很长时间交叉编译ffmpeg的时候,感觉脚本写的非常啰嗦,但是由于shell不是很精通,直到在github上面发现vitamio使用的ffmpeg编译脚本。我们对ffmpeg的使用仅限于播放器,并且限定于特定的几个视频格式。我们的使用方式是从ffmpeg官方网站下载最新的代码包,把脚本copy到解压后的代码包中,然后使用经过裁剪之后脚本编译即可,这里需要注意的是要先设置gas-preprocessor.pl脚本,看下面注意事项二。
vitamio的github脚本地址:https://github.com/yixia/FFmpeg-Vitamio
经过裁剪之后的脚本如下:
#!/bin/bash
echo " _ __ _ __ _ "
echo " | | / / (_) / /_ ____ _ ____ ___ (_) ___ "
echo " | | / / / / / __/ / __ \/ / __ __ \ / / / __ \ "
echo " | |/ / / / / /_ / /_/ / / / / / / / / / / /_/ / "
echo " |___/ /_/ \__/ \____/ /_/ /_/ /_/ /_/ \____/ "
DEVELOPER="/Applications/Xcode.app/Contents/Developer"
SDKVERSION="8.1"
pwd | grep -q '[[:blank:]]' && {
echo "Source path: $(pwd)"
echo "Out of tree builds are impossible with whitespace in source path."
exit 1;
}
selfname=$0
DEST=`pwd`/build/ios
SOURCE=`pwd`
# SSL=${SOURCE}/../opensslmirror/build/ios
# SSLINCLUDE=${SSL}/release/universal/include
# SSLLIBS=${SSL}/release/universal/lib
# RTMP=${SOURCE}/../rtmpdump/build/ios/built
# RTMPINCLUDE=${RTMP}/release/universal/include
# RTMPLIBS=${RTMP}/release/universal/lib
export DEVRootReal="${DEVELOPER}/Platforms/iPhoneOS.platform/Developer"
export SDKRootReal="${DEVRootReal}/SDKs/iPhoneOS${SDKVERSION}.sdk"
export DEVRootSimulator="${DEVELOPER}/Platforms/iPhoneSimulator.platform/Developer"
export SDKRootSimulator="${DEVRootSimulator}/SDKs/iPhoneSimulator${SDKVERSION}.sdk"
export PATH=$HOME/bin:$PATH
export CCACHE=; type ccache >/dev/null 2>&1 && export CCACHE=ccache
# export PKG_CONFIG_LIBDIR=${SSLLIBS}/pkgconfig:${RTMPLIBS}/pkgconfig
function die()
{
kill $$
exit 1; exit 1; exit 1; exit 1;
}
function doConfigureOnline()
{
./configure \
--prefix=${DIST} \
\
--enable-version3 \
\
--disable-shared \
--disable-small \
--disable-runtime-cpudetect \
\
--disable-programs \
--disable-doc \
--enable-swscale \
--disable-avresample \
--enable-network \
\
--disable-everything \
--disable-muxers \
--enable-muxer=mp4 \
--enable-filters \
--enable-parsers \
--disable-parser=hevc \
--enable-protocol=file \
--enable-protocol=http \
--enable-demuxer=flv \
--enable-decoder=mpeg4 \
--enable-decoder=mpegvideo \
--enable-decoder=mpeg1video \
--enable-decoder=mpeg2video \
--enable-decoder=h264 \
--enable-decoder=h263 \
--enable-decoder=flv \
--enable-decoder=aac \
--enable-decoder=ac3 \
--enable-decoder=mp3 \
--enable-decoder=nellymoser \
--enable-cross-compile \
--sysroot=${SDKRoot} \
--target-os=darwin \
--cc="${CC}" \
--extra-cflags="-DVPLAYER_IOS -arch ${ARCH} -pipe ${EXCFLAGS} -miphoneos-version-min=${SDKVERSIONMIN}" \
--extra-ldflags="-arch ${ARCH} -isysroot ${SDKRoot} ${EXCLDFLAGS} -miphoneos-version-min=${SDKVERSIONMIN}" \
${ADVANCED} \
--enable-pic \
--enable-thumb \
--disable-symver \
--enable-hardcoded-tables \
--disable-memalign-hack \
\
${OPTZFLAGS} \
\
${DEBUGS}
}
function doConfigureAll()
{
# *NEED* gas-preprocessor.pl file in $PATH , use for asm compile.
# wget --no-check-certificate https://raw.github.com/libav/gas-preprocessor/master/gas-preprocessor.pl
# wget --no-check-certificate https://raw.github.com/yuvi/gas-preprocessor/master/gas-preprocessor.pl
./configure \
--prefix=${DIST} \
\
--enable-version3 \
\
--disable-shared \
--disable-small \
--disable-runtime-cpudetect \
\
--disable-programs \
--disable-doc \
\
--disable-avdevice \
--disable-postproc \
--enable-network \
\
--enable-vda \
\
--disable-muxers \
--enable-muxer=mp4 \
--enable-demuxers \
--disable-demuxer=sbg \
--disable-demuxer=dts \
--disable-encoders \
--enable-decoders \
--disable-decoder=dca \
--disable-decoder=svq3 \
--disable-parser=dca \
--disable-devices \
\
--enable-bzlib \
--enable-zlib \
--enable-iconv \
--disable-librtmp \
--enable-openssl \
\
--enable-cross-compile \
--sysroot=${SDKRoot} \
--target-os=darwin \
--cc="${CC}" \
--extra-cflags="-DVPLAYER_IOS -arch ${ARCH} -pipe -I${SSLINCLUDE} ${EXCFLAGS} -miphoneos-version-min=${SDKVERSIONMIN}" \
--extra-ldflags="-arch ${ARCH} -isysroot ${SDKRoot} -L${SSLLIBS} ${EXCLDFLAGS} -miphoneos-version-min=${SDKVERSIONMIN}" \
${ADVANCED} \
--enable-pic \
--enable-thumb \
--disable-symver \
--enable-hardcoded-tables \
--disable-memalign-hack \
\
${OPTZFLAGS} \
\
${DEBUGS}
}
# $1 version
function doConfigure()
{
ver="$1";
if [[ $ver == "online" ]]; then
doConfigureOnline
else
doConfigureAll
fi
ret=$?;
perl -i -pe "s|^#define HAVE_GETADDRINFO 1|#define HAVE_GETADDRINFO 0|" config.h # getaddrinfo() has some issues in iOS
cp -f ./config.log ./config.h ${DIST}/; [[ $ret != 0 ]] && die
}
function doMake()
{
rm -f ./-.d
(make clean && make) || die
## MMS stream failed by optimizations flag "-O1/-O2/-O3", so ...
sed -e '/^CFLAGS=/s/ -O[123s]/ -O0/' config.mak > config.O0.mak
mv -f config.O0.mak config.mak
cd libavformat && rm -f mms.o mmst.o mmsh.o mmsu.o && cd ..
make && make install || die
}
path_old=$PATH
build_date=`date "+%Y%m%dT%H%M%S"`
#build_versions="release debug online"
# build_versions="online release"
build_versions="online"
build_archs="arm64 x86_64 armv7 armv7s i386"
for iver in $build_versions; do
case $iver in
release|online) export DEBUGS="--disable-debug --disable-optimizations --optflags=-O3" ;; # -O3 failed to open mms stream!
debug) export DEBUGS="--enable-debug=3 --disable-optimizations" ;;
esac
lipo_archs=
for iarch in $build_archs; do
[[ $iver == debug && ($iarch == armv7s || $iarch == x86_64) ]] && continue
export ARCH=$iarch
export SDKVERSIONMIN=6.0 # 注意这里需要随着Xcode的版本号升级
export DIST=${DEST}/$build_date/$iver/$iarch && mkdir -p ${DIST}
export PATH=${DEVELOPER}/Toolchains/XcodeDefault.xctoolchain/usr/bin:$path_old
export CC="${DEVELOPER}/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang"
libdir=${DIST}/lib && mkdir -p $libdir
confInfo=${DIST}/configure_out.log
makeInfo=${DIST}/make_out.log
case $iarch in
arm*)
export SDKRoot="$SDKRootReal"
export OPTZFLAGS="--enable-asm --disable-armv5te --disable-armv6 --disable-armv6t2"
case $iarch in
armv7)
export EXCFLAGS="-mfpu=neon -mfloat-abi=hard" # "-mcpu= or -march=" set by "--cpu="
export ADVANCED="--arch=armv7 --cpu=cortex-a8"
;;
armv7s)
export EXCFLAGS="-mfpu=neon -mfloat-abi=hard"
export ADVANCED="--arch=armv7s --cpu=cortex-a9"
;;
arm64)
export OPTZFLAGS="--disable-asm"
export EXCFLAGS="-mfpu=vfpv4 -mfloat-abi=hard"
export ADVANCED="--arch=arm64"
export SDKVERSIONMIN=7.0.0
;;
esac
;;
i386|x86_64)
export SDKRoot="$SDKRootSimulator"
export OPTZFLAGS="--disable-asm"
export EXCFLAGS=
export ADVANCED="--arch=$iarch --cpu=$iarch"
;;
esac
cd $SOURCE && cp -f $selfname $DIST
doConfigure "$iver" 2>&1 | tee -a $confInfo
objs=$(make -n -B | sed -n -e '/printf "AR.*; ar rc /p' | sed -e 's/^printf .* ar rc .*\.a//')
doMake 2>&1 | tee -a $makeInfo
ar rc $libdir/libffmpeg.a $objs
lipo_archs="$lipo_archs $libdir/libffmpeg.a"
done
export PATH=${DEVRootReal}/usr/bin:$path_old
univs=${DEST}/$build_date/$iver/universal
univslib=$univs/lib && mkdir -p $univslib
lipo $lipo_archs -create -output $univslib/libffmpeg.a
ranlib $univslib/libffmpeg.a
[[ $iver != "debug" ]] && strip -S $univslib/libffmpeg.a
done
[[ $build_date != built ]] && cd ${DEST} && rm -f built && ln -s $build_date built
printf "\nFFmpeg build successfully!!\n\n"
exit 0
编译过程中,有一些地方需要额外注意一下:
1. 出现错误:
“/Applications/XCode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang is unable to create an executable file.
C compiler test failed.”
这个问题可能有两个地方导致,第一个是需要注意的是,SDKVERSION是需要设置为和当前Xcode匹配的版本号才行。第二个是SDKVERSIONMIN的设置,这个也需要根据当前Xcode的版本号修改才行,这个值设置过低的话,也会报这个错误。
2. 出现错误:
“/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: /Users/jason/Desktop/ffmpeg-2.5.3/build/ios/20150119T110056/online/x86_64/lib/libffmpeg.a(atomic.o) has no symbols
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
GNU assembler not found, install/update gas-preprocessor”
出现这个问题,是由于没有设置gas-preprocessor这个脚本的缘故,修改方式,在https://github.com/yuvi/gas-preprocessor或者https://github.com/libav/gas-preprocessor下载最新的脚本,把这个脚本移动到/usr/local/bin目录下即可。我们使用的libav的脚本,这个更新日期更新一些。