1.下载最新版ffmpeg
2.解压缩
3.找到ios编译脚本
build-ffmpeg.sh
放到同级目录
修改脚本,把版本号改成4.2.1
#!/bin/sh
# directories
FF_VERSION="4.2.1"
if [[ $FFMPEG_VERSION != "" ]]; then
FF_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`/x264-iOS
#FDK_AAC=`pwd`/fdk-aac-ios
CONFIGURE_FLAGS="--enable-cross-compile --disable-debug --disable-programs \
--disable-doc --enable-pic --enable-nonfree"
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"
COMPILE="y"
LIPO="y"
DEPLOYMENT_TARGET="10.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 -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"
# 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
4.终端执行脚本 ./build-ffmpeg.sh
耐心等待
5.编译完成
添加第三方库:
ffmpeg 编译的时候是缺失一些功能的,比如ssl
因为https的原因 ffmpeg 本身不能下载https的链接,
所以需要第三方openssl 来做https的支持
首先是编译openssl
不是本文重点,编译openssl部分略过
修改 build-ffmpeg
脚本 重新编译
总共三处修改
1.添加openssl的路径
OPENSSL=`pwd`/openssl-ios
2.增加openssl的配置enable
if [ "OPENSSL" ]
then
#echo $CONFIGURE_FLAGS
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-openssl"
fi
3.增加openssl的编译路径
if [ "$OPENSSL" ]
then
CFLAGS="$CFLAGS -I$OPENSSL/include"
LDFLAGS="$LDFLAGS -L$OPENSSL/lib -lcrypto -lssl"
fi
具体位置查看全文
#!/bin/sh
# directories
FF_VERSION="4.2.1"
if [[ $FFMPEG_VERSION != "" ]]; then
FF_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`/x264-iOS
# FDK_AAC=`pwd`/fdk-aac-ios
OPENSSL=`pwd`/openssl-ios
CONFIGURE_FLAGS="--enable-cross-compile --disable-debug --disable-programs \
--disable-doc --enable-pic --enable-nonfree"
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
if [ "OPENSSL" ]
then
#echo $CONFIGURE_FLAGS
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-openssl"
fi
# avresample
#CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-avresample"
ARCHS="arm64 armv7 armv7s"
COMPILE="y"
LIPO="y"
DEPLOYMENT_TARGET="10.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 -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"
# 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
if [ "$OPENSSL" ]
then
CFLAGS="$CFLAGS -I$OPENSSL/include"
LDFLAGS="$LDFLAGS -L$OPENSSL/lib -lcrypto -lssl"
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
裁剪:
库一般都是没有裁剪的,所以包会比较大,几百m的样子,
所以就需要裁剪,把不用的裁剪掉,可以裁剪到10多m的样子
不过优先考虑能用,再考虑裁剪。
我们这里假设已经能够使用了,现在开始裁剪
build-ffmpeg.sh
中修改的部分
CONFIGURE_FLAGS="--enable-cross-compile --disable-debug --disable-programs --disable-doc --disable-everything \
--disable-avdevice --disable-postproc --disable-sdl2\
--disable-ffmpeg --disable-ffplay \
--enable-decoder=h264 --enable-decoder=aac --enable-parser=h264 --enable-parser=aac \
--enable-demuxer=mov --enable-protocol=file --enable-filter=scale \
--enable-pic --enable-nonfree"
完整版本
#!/bin/sh
# directories
FF_VERSION="4.2.1"
if [[ $FFMPEG_VERSION != "" ]]; then
FF_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`/x264-iOS
OPENSSL=`pwd`/openssl-ios
echo $OPENSSL
# FDK_AAC=`pwd`/fdk-aac-ios
CONFIGURE_FLAGS="--enable-cross-compile --disable-debug --disable-programs --disable-doc --disable-everything \
--disable-avdevice --disable-postproc --disable-sdl2\
--disable-ffmpeg --disable-ffplay \
--enable-decoder=h264 --enable-decoder=aac --enable-parser=h264 --enable-parser=aac \
--enable-demuxer=mov --enable-protocol=file --enable-filter=scale \
--enable-pic --enable-nonfree"
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
if [ "OPENSSL" ]
then
#echo $CONFIGURE_FLAGS
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-protocol=https --enable-openssl --enable-protocol=crypto"
fi
# avresample
#CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-avresample"
ARCHS="arm64 armv7"
COMPILE="y"
LIPO="y"
DEPLOYMENT_TARGET="10.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 -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"
# 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
if [ "$OPENSSL" ]
then
CFLAGS="$CFLAGS -I$OPENSSL/include"
LDFLAGS="$LDFLAGS -L$OPENSSL/lib -lssl -lcrypto"
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
裁剪后的效果
裁剪之前libavcodec.a大概170M左右,裁剪后13M左右
报错信息:
1.脚本报错
src/libavcodec/videotoolbox.c:636:14: error:
'VTDecompressionSessionDecodeFrame' is only available on iOS 8.0 or newer
src/libavcodec/videotoolbox.c:642:18: error:
'VTDecompressionSessionWaitForAsynchronousFrames' is only available on iOS
8.0 or newer [-Werror,-Wunguarded-availability]
src/libavcodec/videotoolbox.c:828:14: error: 'VTDecompressionSessionCreate' is
only available on iOS 8.0 or newer [-Werror,-Wunguarded-availability]
src/libavcodec/videotoolbox.c:876:9: error: 'VTDecompressionSessionInvalidate'
is only available on iOS 8.0 or newer [-Werror,-Wunguarded-availability]
是硬解码版本太低了 写这个脚本的估计5年前了 最低支持ios6.0 所以把编译最低版本DEPLOYMENT_TARGET
改下 我用的10.0然后重新编译的。
2.openssl最新库报错
building arm64...
ERROR: openssl not found
If you think configure made a mistake, make sure you are using the latest
version from Git. If the latest version fails, report the problem to the
ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.freenode.net.
Include the log file "ffbuild/config.log" produced by configure as this will help
solve the problem.
这里提示错误要去 ffbuild/config.log
看,找了很久发现,在arm64 文件夹
里面有这个文件
关键部分
1 error generated.
check_lib openssl openssl/ssl.h SSL_library_init -lssl -lcrypto -lws2_32 -lgdi32
check_func_headers openssl/ssl.h SSL_library_init -lssl -lcrypto -lws2_32 -lgdi32
test_ld cc -lssl -lcrypto -lws2_32 -lgdi32
test_cc
BEGIN /var/folders/z4/d0vkbw5j64n62dfdpytfksnnwjm3f9/T/ffconf.wMR9Mpcv/test.c
1 #include <openssl/ssl.h>
2 #include <stdint.h>
3 long check_SSL_library_init(void) { return (long) SSL_library_init; }
4 int main(void) { int ret = 0;
5 ret |= ((intptr_t)check_SSL_library_init) & 0xFFFF;
6 return ret; }
END /var/folders/z4/d0vkbw5j64n62dfdpytfksnnwjm3f9/T/ffconf.wMR9Mpcv/test.c
xcrun -sdk iphoneos clang -D_ISOC99_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -I$(SRC_PATH)/compat/dispatch_semaphore -DPIC -arch arm64 -mios-version-min=10.0 -fembed-bitcode -I/Volumes/WORKSPACE/ffm/openssl-ios/include -std=c11 -Werror=partial-availability -fomit-frame-pointer -fPIC -pthread -c -o /var/folders/z4/d0vkbw5j64n62dfdpytfksnnwjm3f9/T/ffconf.wMR9Mpcv/test.o /var/folders/z4/d0vkbw5j64n62dfdpytfksnnwjm3f9/T/ffconf.wMR9Mpcv/test.c
/var/folders/z4/d0vkbw5j64n62dfdpytfksnnwjm3f9/T/ffconf.wMR9Mpcv/test.c:3:51: error: use of undeclared identifier 'SSL_library_init'
long check_SSL_library_init(void) { return (long) SSL_library_init; }
^
1 error generated.
ERROR: openssl not found
干脆把这段代码删掉再试试。删掉部分
编译通过 还需要找个项目试试,不然也不知有无编译过了
这里显示已经编译进去了。
ffmpeg version:4.2.1
ffmpeg format version info:--target-os=darwin --arch=arm64 --cc='xcrun -sdk iphoneos clang' --as='gas-preprocessor.pl -arch aarch64 -- xcrun -sdk iphoneos clang' --enable-cross-compile --disable-debug --disable-programs --disable-doc --enable-pic --enable-nonfree --enable-protocol=https --enable-openssl --enable-protocol=crypto --enable-protocol=tls_openssl --extra-cflags='-arch arm64 -mios-version-min=10.0 -fembed-bitcode -I/Volumes/WORKSPACE/ffm/openssl-ios/include' --extra-ldflags='-arch arm64 -mios-version-min=10.0 -fembed-bitcode -L/Volumes/WORKSPACE/ffm/openssl-ios/lib -lssl -lcrypto' --prefix=/Volumes/WORKSPACE/ffm/thin/arm64
项目想要编译通过需要注意的地方
如果没把openssl的库加入head seachpath 和lib search path 会报错,报错如下
升级到高版本后遇到的报错
1.CODEC_CAP_TRUNCATED
找不到