FFmpeg iOS平台库编译


iOS平台库编译

上面的几种安装方式,只是让我们在Mac OS平台实现了FFmpeg的开发能力,但是作为一个iOS开发人员的话,我们还需要编译出iOS平台下的库文件,以提供移动平台开发使用。

1、编译FFmpeg

从下面地址下载编译的脚本。

1
https://github.com/kewlbear/FFmpeg-iOS-build-script

需要修改的内容有

  • FF_VERSION 指定ffmpeg的版本号。
  • CONFIGURE_FLAGS 修改ffmpeg的编译配置,去掉不必要的编译内容,减小包体的大小。
  • ARCHS 指定编译的cpu架构,对iOS开发而言,有真机和模拟器。
  • X264 编译好的libx264的路径,如果需要将libx264编译进去。
  • FDK_AAC 编译好的libfdk-aac的路径,如果需要将libfdk-aac编译进去。 在终端执行命令 ./build-ffmpeg.sh,编译完成后会有如下的文件结构。

image.png

2、编译libfdk-aac

从官网下载稳定版本的FDK-AAC

1
https://sourceforge.net/p/opencore-amr/fdk-aac/ci/v2.0.2/tree/

在同级目录下创建build_aac.sh脚本文件,文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
CONFIGURE_FLAGS="--enable-static --with-pic=yes --disable-shared"
#指定编译平台
ARCHS="arm64 armv7"
# 源码位置
SOURCE="fdk-aac-2.0.2"
FAT="fdk-aac-ios"
SCRATCH="scratch"
# must be an absolute path
THIN=`pwd`/"thin"
COMPILE="y"
LIPO="y"

if [ "$*" ]
then
if [ "$*" = "lipo" ]
then
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..."
mkdir -p "$SCRATCH/$ARCH"
cd "$SCRATCH/$ARCH"
CFLAGS="-arch $ARCH"
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="--host=x86_64-apple-darwin"
else
CFLAGS="$CFLAGS -mios-simulator-version-min=7.0"
HOST="--host=i386-apple-darwin"
fi
else
PLATFORM="iPhoneOS"
if [ $ARCH = arm64 ]
then
HOST="--host=aarch64-apple-darwin"
else
HOST="--host=arm-apple-darwin"
fi
CFLAGS="$CFLAGS -fembed-bitcode"
fi

XCRUN_SDK=`echo $PLATFORM | tr '[:upper:]' '[:lower:]'`
CC="xcrun -sdk $XCRUN_SDK clang -Wno-error=unused-command-line-argument-hard-error-in-future"
AS="$CWD/$SOURCE/extras/gas-preprocessor.pl $CC"
CXXFLAGS="$CFLAGS"
LDFLAGS="$CFLAGS"

$CWD/$SOURCE/configure \
$CONFIGURE_FLAGS \
$HOST \
$CPU \
CC="$CC" \
CXX="$CC" \
CPP="$CC -E" \
AS="$AS" \
CFLAGS="$CFLAGS" \
LDFLAGS="$LDFLAGS" \
CPPFLAGS="$CFLAGS" \
--prefix="$THIN/$ARCH" || exit 1
make -j8 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
复制代码

在终端执行该脚本,编译完成后的库文件在文件夹fdk-aac-ios中。

image.png

在这里,新创建的脚本文件,可能会存在权限问题,执行chmod -R 777 build_aac.sh命令即可。

3、编译libx264

下载x264的源码

1
2
3
访问 http://www.videolan.org/developers/x264.html
或者
git clone git://git.videolan.org/x264.git

在同级目录下创建build_x264.sh脚本文件,文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
CONFIGURE_FLAGS="--enable-static --enable-pic --disable-cli --disable-shared"
# 指定架构平台
ARCHS="arm64 armv7"
#源码位置
SOURCE="x264-master"
FAT="x264-iOS"
SCRATCH="scratch-x264"
THIN=`pwd`/"thin-x264"

COMPILE="y"
LIPO="y"

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

if [ "$COMPILE" ]
then
CWD=`pwd`
for ARCH in $ARCHS
do
echo "building $ARCH..."
mkdir -p "$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=9.0"
HOST=
else
CFLAGS="$CFLAGS -mios-simulator-version-min=9.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=9.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 -j8 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

在终端执行该脚本,编译完成后的库文件在文件夹x264-iOS中。

image.png

4、使用

  • 1、创建工程,导入编译好的库文件
  • 2、添加依赖libbz2.tbdlibiconv.tbdlibz.tbd
  • 3、在工程的Build Settings选项中修改Header Search Paths添加头文件引用,修改Library Search Paths添加库文件的路径引用。 image.png