一、linux环境编译 (ubuntu1604_64)
1)下载FFmpeg源码
https://ffmpeg.org/download.html
下载最新的版本,ffmpeg-x.x.x.tar.gz,再通过命令解压:tar -xjvf ffmpeg-x.x.x.tar.bz2
或者git拉下来,git clone "https://git.ffmpeg.org/ffmpeg.git" ffmpeg
2)下载NDK
https://developer.android.google.cn/ndk/downloads/index.html
可以点击下载,也可以终端:wget https://dl.google.com/android/repository/android-ndk-r17c-linux-x86_64.zip
最后都用uzip命令解压到当前目录。
可以下低一点的版本比如r14b,但是编译时有问题,我编译的时候下载的是17c,但是还是会遇到问题,后面会一一列举。
更高版本的就不知道了,没试过最新的怎么编。
PS:为了方便管理我都是把解压好放到一个目录下,比如:
ls ~/ffmpeg
android-ndk-r17c ffmpeg-4.2.1
3)修改配置
目的是编译android可用的动态库so。
1.进入ffmpeg目录
cd ~/ffmpeg/ffmpeg-4.2.1/
2.修改configure
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)'
这样生成的动态库命名方式可以被Android平台识别。
3.创建build_android.sh脚本
在configure同级目录下,新建脚本build_android.sh,加入如下内容:
#!/bin/bash
set -x
API=28
NDK=D:/NDK/android-ndk-r17c
SYSROOT=$NDK/platforms/android-$API/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64
function function_one
{
./configure \
--prefix=$PREFIX \
--enable-shared \
--disable-static \
--disable-doc \
--disable-ffplay \
--disable-ffprobe \
--disable-symver \
--disable-ffmpeg \
--cc=$TOOLCHAIN/bin/arm-linux-androideabi-gcc \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--target-os=linux \
--arch=arm \
--enable-cross-compile \
--sysroot=$SYSROOT \
--extra-cflags="-I$NDK/sysroot/usr/include/arm-linux-androideabi -isysroot $NDK/sysroot -fPIC -DANDROID -D__thumb__ -mthumb -Wfatal-errors -Wno-deprecated -mfloat-abi=softfp -marm -march=armv7-a" \
--enable-neon
make clean all
make
make install
}
CPU=armv7-a
PREFIX=$(pwd)/android/$CPU
function_one
改完之后,修改build_android.sh权限为777:chmod 777 build_android.sh
然后运行脚本build_android.sh:./build_android.sh
二、windows环境编译(win10)
1)下载FFmpeg源码
https://ffmpeg.org/download.html
2)下载NDK
https://developer.android.google.cn/ndk/downloads/index.html
3)下载MinGW
https://sourceforge.net/projects/mingw/files/
FFmpeg和DNK的下载和linux一样的,MinGW是为了代替cmd。可以类似linux的环境执行命令。
下载后解压出来,再放到同一个目录下,比如我的都放D盘。
4)配置系统环境参数
在我的电脑--属性--高级系统设置--环境变量--系统变量--Path,添加以下几个路径:
D:\MinGW\bin
D:\MinGW\msys\1.0\bin
D:\NDK\android-ndk-r17c
一路确定返回。win+R,输入cmd,回车调出终端。分别输入ndk-build和gcc -v来验证配置成功。
5)修改配置
1. 和linux的一样,进入ffmpeg的目录,修改configure和新增build_android.sh
2.进入目录D:\MinGW\msys\1.0,双击打开msys.bat,就能像linux那样执行bash命令;
cd /d/ffmpeg/ffmpeg-4.2.1
chmod 777 build_android.sh
./build_android.sh
↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ 上面是下载源文件和配置 ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ 下面是编译时遇到的问题和解决方法 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
0.如果windows提示“../arm-linux-androideabi-gcc is unable to create an executable file.”
在build_android.sh中加多一项
export TMPDIR=D:/ffmpeg/src/ffmpeg-4.2.1/temp
1.提示type.h找不到
/NDK/android-ndk-r17c/sysroot/usr/include/linux/types.h:21:23: fatal error: asm/types.h: No such file or directory
#include <asm/types.h>
查看中build_android.sh中--extra-cflags=中有没有加上了 -isysroot $NDK/sysroot
2.提示变量名B0出问题
libavcodec/aaccoder.c: In function 'search_for_ms':
libavcodec/aaccoder.c:803:25: error: expected identifier or '(' before numeric constant
int B0 = 0, B1 = 0;
^
把libavcodec/aaccoder.c中的所有变量B0都改成b0
3.提示y0000000' undeclared
libavcodec/hevc_mvs.c: In function 'derive_spatial_merge_candidates':
libavcodec/hevc_mvs.c:208:15: error: 'y0000000' undeclared (first use in this function)
((y ## v) >> s->ps.sps->log2_min_pu_size))
把libavcodec/hevc_mvs.c文件的所有变量B0改成b0,xB0改成xb0,yB0改成yb0
4.提示变量名B0出问题
libavcodec/opus_pvq.c: In function 'quant_band_template':
libavcodec/opus_pvq.c:498:9: error: expected identifier or '(' before numeric constant
int B0 = blocks;
把libavcodec/opus_pvq.c中所有变量B0都改成b0
基本经历过这几个错误之后就能正常编完了,编好的库在ffmpeg-4.2.1\android\armv7-a\lib下。
参考:
https://blog.csdn.net/qq_38261174/article/details/83210458