上篇讲了如何编译 FFmpeg for android,但是里面包含了多个 so 库,很多时候我们希望只有一个 so 库,这篇介绍如何打包成一个库。

修改 FFmpeg 的 configure

如果你使用过上篇教程,则需要把 configure 改回来。

如果你和我一样是用 git 下载的源码,那么很简单了,直接 git checkout 完事儿。

编写 Android 编译脚本

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
#!/bin/bash
NDK=/home/gavin/Develop/android-sdk/ndk-bundle
SYSROOT=$NDK/platforms/android-15/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64

function build_one
{
./configure \
--prefix=$PREFIX \
--enable-static \
--disable-shared \
--disable-doc \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--disable-avdevice \
--disable-doc \
--disable-symver \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--target-os=linux \
--arch=arm \
--cc=$TOOLCHAIN/bin/arm-linux-androideabi-gcc \
--nm=$TOOLCHAIN/bin/arm-linux-androideabi-nm \
--enable-cross-compile \
--sysroot=$SYSROOT \
--extra-cflags="-Os -fpic $ADDI_CFLAGS" \
--extra-ldflags="$ADDI_LDFLAGS" \
$ADDITIONAL_CONFIGURE_FLAG

make clean
make -j4
make install

$TOOLCHAIN/bin/arm-linux-androideabi-ld \
-rpath-link=$SYSROOT/usr/lib \
-L$SYSROOT/usr/lib \
-L$PREFIX/lib \
-soname libffmpeg.so -shared -nostdlib -Bsymbolic --whole-archive --no-undefined -o \
$PREFIX/libffmpeg.so \
$PREFIX/lib/libavcodec.a $PREFIX/lib/libavfilter.a \
$PREFIX/lib/libavformat.a $PREFIX/lib/libavutil.a \
$PREFIX/lib/libswresample.a $PREFIX/lib/libswscale.a \
-lc -lm -lz -ldl -llog --dynamic-linker=/system/bin/linker \
$TOOLCHAIN/lib/gcc/arm-linux-androideabi/4.9.x/libgcc.a
}

CPU=arm
ADDI_CFLAGS="-marm"
PREFIX=$(pwd)/android/$CPU
build_one

主要的变化在于 :

  1. enable-static ,disable-shared
1
2
--enable-static \
--disable-shared \
  1. make install 后面那句。

编译

再次编译就能得到一个名为 libffmpeg.so 的库。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
├── include
│   ├── libavcodec
│   ├── libavfilter
│   ├── libavformat
│   ├── libavutil
│   ├── libswresample
│   └── libswscale
├── lib
│   ├── libavcodec-57.so
│   ├── libavcodec.a
│   ├── libavfilter-6.so
│   ├── libavfilter.a
│   ├── libavformat-57.so
│   ├── libavformat.a
│   ├── libavutil-55.so
│   ├── libavutil.a
│   ├── libswresample-2.so
│   ├── libswresample.a
│   ├── libswscale-4.so
│   └── libswscale.a
└── libffmpeg.so

参考资料