前言
FFmpeg库是一个非常强大的音视频编解码库,只要有过流媒体相关开发经验的,相信对它都不会陌生。笔者是一个Android 开发者,所做的工作都是为Android服务的,其他的开发者,可自行略过。废话不多说,开始正题。
编译
笔者编译环境如下:
- 系统 :Mac OS X 10.11.6
- FFmpeg 版本 :3.1.2 (此版本已加入Android 的硬解码,之前用的是3.0版本,无硬解码)
- NDK 版本: android-ndk-r10e
1、修改ffmpeg 根目录下的configure文件
找到如下代码(3.0版本在3050行左右,笔者版本在3208行)
SLIBNAME_WITH_MAJOR='$(SLIBNAME).$(LIBMAJOR)'
LIB_INSTALL_EXTRA_CMD='$$(RANLIB) "$(LIBDIR)/$(LIBNAME)"'
SLIB_INSTALL_NAME='$(SLIBNAME_WITH_VERSION)'
SLIB_INSTALL_LINKS='$(SLIBNAME_WITH_MAJOR) $(SLIBNAME)'
修改为
SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF)'
LIB_INSTALL_EXTRA_CMD='$$(RANLIB)"$(LIBDIR)/$(LIBNAME)"'
SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)'
SLIB_INSTALL_LINKS='$(SLIBNAME)'
目的是因为FFmpeg在生成so库时默认生成是libavcodec.so.56(生成.a文件时无此问题)这种格式,在Android中无法识别,要把后面的.56提到.so之前,当然也可以省略。
2、编写sh文件
笔者把文件命名为:build_andorid.sh,文件内容如下:
NDK=xxx/android-ndk-r10e #指向你的NDK路径
SYSROOT=$NDK/platforms/android-9/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64
function build_android
{
./configure \
--prefix=$PREFIX \
--enable-hwaccel=h264_vaapi \
--enable-hwaccel=h264_vaapi \
--enable-hwaccel=h264_dxva2 \
--enable-hwaccel=mpeg4_vaapi \
--enable-hwaccels \
--enable-shared \ #编译出.so文件
--enable-jni \ #这行和下一行是打开mediacodec硬解码用的,
--enable-mediacodec \
--disable-static \ #不生成.a文件
--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 \
--enable-cross-compile \
--sysroot=$SYSROOT \
--extra-cflags="-Os -fpic $ADDI_CFLAGS" \
--extra-ldflags="$ADDI_LDFLAGS" \
$ADDITIONAL_CONFIGURE_FLAG
make clean
make -j4
make install
}
CPU=arm
PREFIX=$(pwd)/android/$CPU
ADDI_CFLAGS="-marm"
build_android
3 、正式编译
把sh文件放入到FFmpeg的根目录,打开电脑的终端,进入到FFmpeg的根目录
运行 sh build_andorid.sh
剩下的就是等待
4、说明
编译完成之后会在FFmpeg的根目录下生成一个名为android的文件夹
编译出来的文件会存放在这里面
这种方式编译出的是多个.so 文件
下一篇会介绍如何编译单个.so 文件
网友评论
# build_andorid.sh
NDK=/Users/xiaoyunfei/Library/Android/android-ndk-r10e
SYSROOT=$NDK/platforms/android-9/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64
function build_android
{
./configure \
--prefix=$PREFIX \
--enable-neon \
--enable-hwaccel=h264_vaapi \
--enable-hwaccel=h264_vaapi \
--enable-hwaccel=h264_dxva2 \
--enable-hwaccel=mpeg4_vaapi \
--enable-hwaccels \
--enable-shared \
--enable-jni \
--enable-mediacodec \
--disable-static \
--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 \
--enable-cross-compile \
--sysroot=$SYSROOT \
--extra-cflags="-Os -fpic $ADDI_CFLAGS" \
--extra-ldflags="$ADDI_LDFLAGS" \
$ADDITIONAL_CONFIGURE_FLAG
make clean
make -j4
make install
}
CPU=arm
PREFIX=$(pwd)/android/$CPU
ADDI_CFLAGS="-marm"
build_android
Unknown option "".
See ./configure --help for available options.
build_android.sh: line 15: --enable-jni: command not found
build_android.sh: line 17: --enable-mediacodec: command not found
build_android.sh: line 19: --disable-doc: command not found
Makefile:2: config.mak: No such file or directory
Makefile:67: /common.mak: No such file or directory
Makefile:109: /libavutil/Makefile: No such file or directory
Makefile:109: /library.mak: No such file or directory
Makefile:111: /doc/Makefile: No such file or directory
Makefile:198: /tests/Makefile: No such file or directory
make: *** No rule to make target `/tests/Makefile'. Stop.
Makefile:2: config.mak: No such file or directory
Makefile:67: /common.mak: No such file or directory
Makefile:109: /libavutil/Makefile: No such file or directory
Makefile:109: /library.mak: No such file or directory
Makefile:111: /doc/Makefile: No such file or directory
Makefile:198: /tests/Makefile: No such file or directory
make: *** No rule to make target `/tests/Makefile'. Stop.
Makefile:2: config.mak: No such file or directory
Makefile:67: /common.mak: No such file or directory
Makefile:109: /libavutil/Makefile: No such file or directory
Makefile:109: /library.mak: No such file or directory
Makefile:111: /doc/Makefile: No such file or directory
Makefile:198: /tests/Makefile: No such file or directory
make: *** No rule to make target `/tests/Makefile'. Stop.