概述
前言
本文介绍ffmpeg最新版4.1在mac的编译为android的方法跟说明,编译多个so打包为libffmpeg.so。
编译流程
下载FFMpeg源代码地址:
FFMpeg官网下载地址
github上FFmpeg的下载地址
准备好NDK
Android NDK 下载地址(推荐r17以下(包括r17c))
编写build for Android 脚本
在FFMpeg根目录新建sh文件,命名为:build_android.sh,复制脚本到build_android.sh, 内容如下:
ndk环境
export NDK=/Users/ali/Library/Android/android-ndk-r17c
export SYSROOT=
N
D
K
/
p
l
a
t
f
o
r
m
s
/
a
n
d
r
o
i
d
−
21
/
a
r
c
h
−
a
r
m
e
x
p
o
r
t
T
O
O
L
C
H
A
I
N
=
NDK/platforms/android-21/arch-arm export TOOLCHAIN=
NDK/platforms/android−21/arch−armexportTOOLCHAIN=NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64
CPU=armv7-a
ISYSROOT= N D K / s y s r o o t A S M = NDK/sysroot ASM= NDK/sysrootASM=ISYSROOT/usr/include/arm-linux-androideabi
要保存动态库的目录,这里保存在源码根目录下的android/armv7-a
export PREFIX=
(
p
w
d
)
/
a
n
d
r
o
i
d
/
(pwd)/android/
(pwd)/android/CPU
ADDI_CFLAGS="-marm"
编译配置详解答
设置编译针对的系统,网上查到很多资料说编译前需要修改configure文件,设置这项后就不用修改了,系统会自动修改,效果一样。
#–target-os=linux
编译后文件的输出目录
#–prefix=$PREFIX
#–enable-cross-compile
编译动态库,shared和static的开关是相对的。
#–enable-static
#–disable-shared
生成文档,以及相关的功能模块
#–disable-doc
禁止编译可执行文件
#–disable-programs
#–disable-ffmpeg
#–disable-ffplay
#–disable-ffprobe
#–disable-avdevice
#–disable-doc
#–disable-symver
设置交叉编译器,按照实际路径修改就可以了。
#–cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi-
设置编译器,不然会报错,系统默认会使用arm-linux-androideabi-clang,但是此编译器在NDK不存在,导致编译失败
#–cc=KaTeX parse error: Expected 'EOF', got ' ' at position 41: …ndroideabi-gcc ̲ ̲#--nm=TOOLCHAIN/bin/arm-linux-androideabi-nm
设置编译so库的架构,当前设置为arm,可以根据实际需求修改
#–arch=arm
设置编译针对的平台,可以根据实际需求进行设置,当前设置为最低支持android-21版本,arm架构。
#–sysroot=KaTeX parse error: Expected 'EOF', got ' ' at position 9: SYSROOT ̲ ̲#--extra-cflags…ASM -isysroot $ISYSROOT -D__ANDROID_API__=21 -U_FILE_OFFSET_BITS -Os -fPIC -DANDROID -Wno-deprecated -mfloat-abi=softfp -marm"
#–extra-ldflags="$ADDI_LDFLAGS"
$ADDITIONAL_CONFIGURE_FLAG
function build_android
{
echo -e “ 33[32m build start 33[0m”
./configure
–target-os=android
–prefix=KaTeX parse error: Expected 'EOF', got ' ' at position 8: PREFIX ̲ ̲ --enable…TOOLCHAIN/bin/arm-linux-androideabi-
–cc=KaTeX parse error: Expected 'EOF', got ' ' at position 41: …ndroideabi-gcc ̲ ̲ --nm=TOOLCHAIN/bin/arm-linux-androideabi-nm
–arch=arm
–sysroot=KaTeX parse error: Expected 'EOF', got ' ' at position 9: SYSROOT ̲ ̲ --extra-…ASM -isysroot KaTeX parse error: Expected group after '_' at position 12: ISYSROOT -D_̲_ANDROID_API__=…ADDI_LDFLAGS"
$ADDITIONAL_CONFIGURE_FLAG
make clean
make -j16
make install
echo -e “ 33[32m build successful 33[0m”
}
function merge
{
echo -e “ 33[32m package start 33[0m”
打包
KaTeX parse error: Expected 'EOF', got ' ' at position 40: …androideabi-ld ̲ ̲ -rpath-l…SYSROOT/usr/lib
-LKaTeX parse error: Expected 'EOF', got ' ' at position 17: …YSROOT/usr/lib ̲ ̲ -LPREFIX/lib
-soname libffmpeg.so -shared -nostdlib -Bsymbolic --whole-archive --no-undefined -o
$PREFIX/libffmpeg.so
libavcodec/libavcodec.a
libavfilter/libavfilter.a
libavformat/libavformat.a
libavutil/libavutil.a
libswresample/libswresample.a
libswscale/libswscale.a
-lc -lm -lz -ldl -llog --dynamic-linker=/system/bin/linker
$TOOLCHAIN/lib/gcc/arm-linux-androideabi/4.9.x/libgcc.a
strip 精简文件
$TOOLCHAIN/bin/arm-linux-androideabi-strip $PREFIX/libffmpeg.so
echo -e “ 33[32m package successful 33[0m”
}
build_android
merge
在Mac终端执行
./build_android.sh
相关流程说明
1、配置NDK相关环境
export NDK=/Users/ali/Library/Android/android-ndk-r17c
export SYSROOT=
N
D
K
/
p
l
a
t
f
o
r
m
s
/
a
n
d
r
o
i
d
−
21
/
a
r
c
h
−
a
r
m
e
x
p
o
r
t
T
O
O
L
C
H
A
I
N
=
NDK/platforms/android-21/arch-arm export TOOLCHAIN=
NDK/platforms/android−21/arch−armexportTOOLCHAIN=NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64
CPU=armv7-a
ISYSROOT=
N
D
K
/
s
y
s
r
o
o
t
A
S
M
=
NDK/sysroot ASM=
NDK/sysrootASM=ISYSROOT/usr/include/arm-linux-androideabi
2、配置configure
./configure
–target-os=android
–prefix=KaTeX parse error: Expected 'EOF', got ' ' at position 8: PREFIX ̲ ̲ --enabl…TOOLCHAIN/bin/arm-linux-androideabi-
–cc=KaTeX parse error: Expected 'EOF', got ' ' at position 41: …ndroideabi-gcc ̲ ̲ --nm=TOOLCHAIN/bin/arm-linux-androideabi-nm
–arch=arm
–sysroot=KaTeX parse error: Expected 'EOF', got ' ' at position 9: SYSROOT ̲ ̲ --extra…ASM -isysroot KaTeX parse error: Expected group after '_' at position 12: ISYSROOT -D_̲_ANDROID_API__=…ADDI_LDFLAGS"
$ADDITIONAL_CONFIGURE_FLAG
3、执行编译
make clean
make -j16
make install
4、执行打包
function merge
{
echo -e “ 33[32m package start 33[0m”
# 打包
$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
libavcodec/libavcodec.a
libavfilter/libavfilter.a
libavformat/libavformat.a
libavutil/libavutil.a
libswresample/libswresample.a
libswscale/libswscale.a
-lc -lm -lz -ldl -llog --dynamic-linker=/system/bin/linker
$TOOLCHAIN/lib/gcc/arm-linux-androideabi/4.9.x/libgcc.a
# strip 精简文件
$TOOLCHAIN/bin/arm-linux-androideabi-strip $PREFIX/libffmpeg.so
echo -e "