1.首先要有个GNU的环境。如果在Windows系统下,就要弄一个虚拟环境,安装一个Cygwin。如果是Linux环境,那么Linux中是有GNU的。
2.下载ffmpeg的源码,不用爬梯,直接官网可以下载:http://www.ffmpeg.org/download.html
下载NDK,在Google官网下载,不用爬梯:https://developer.android.google.cn/ndk/downloads/
3.编译Android可用的动态库。需要配置某些东西,主要跟动态库名字有关,不修改,Android无法识别。修改如下:
下载FFmpeg源代码之后,首先需要对源代码中的configure文件进行修改。由于编译出来的动态库文件名的版本号在.so之后(例如“libavcodec.so.5.100.1”),而android平台不能识别这样文件名,所以需要修改这种文件名。在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)'
#!/bin/sh
cd ffmpeg
make clean
export NDK=./android-ndk-r17
export PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt
export PLATFORM=$NDK/platforms/android-24/arch-arm
export PREFIX=./simplefflib
build_one(){
./configure --target-os=linux --prefix=$PREFIX \
--enable-cross-compile \
--enable-runtime-cpudetect \
--disable-asm \
--arch=arm \
--cc=$PREBUILT/linux-x86_64/bin/arm-linux-androideabi-gcc \
--cross-prefix=$PREBUILT/linux-x86_64/bin/arm-linux-androideabi- \
--disable-stripping \
--nm=$PREBUILT/linux-x86_64/bin/arm-linux-androideabi-gcc-nm \
--sysroot=$PLATFORM \
--enable-gpl --enable-shared --disable-static --enable-small \
--disable-ffprobe --disable-ffplay --disable-ffmpeg --disable-ffserver --disable-debug \
--extra-cflags="-fPIC -DANDROID -D__thumb__ -mthumb -Wfatal-errors -Wno-deprecated -mfloat-abi=softfp -marm -march=armv7-a"
}
build_one
make
sudo make install
cd ..
该脚本中前面几个变量“NDK”、“PREBUILT”、“PLATFORM”根据NDK路径的不同需要做相应的修改。另外需要注意64位NDK和32位NDK中某些文件夹名称也有一些区别:例如32位NDK中文件夹名称为“linux-x86”而64位NDK中文件夹名称为“linux-x86_64”。
在修改一个宏后,就可以执行,发现说少了很多文件。比如:ffmpeg makefile:2:config.mak:No such file or directory
然后执行以下三个命令后,那么缺少的文件就有了:
sudo apt-get install yasm
./configure
make
sudo make install
注意那些make要在ffmpeg根目录执行。
运行完上面几个命令后,就不报 缺少文件的错误了,大概编译几分钟后,顺利编完了,但是此时中间文件都编译出来了,按照网上说的应该按照PREFIX=./simplefflib所定义的,在./simplefflib文件夹中出现so文件,但是编译完后,没有simplefflib文件夹出现,自己建了一个同名的文件夹再编一遍,还是没有,而且在ffmpeg整个目录都找不到任何so库。然后翻墙找到另外一种脚本如下:
#!/bin/sh
cd ffmpeg
make clean
export NDK=./android-ndk-r17
export PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt
export PLATFORM=$NDK/platforms/android-24/arch-arm
sed -i 's/-lflite"/-lflite -lasound"/' configure &&
./configure --prefix=/usr \
--enable-gpl \
--enable-version3 \
--enable-nonfree \
--disable-static \
--enable-shared \
--disable-debug \
--enable-avresample \
--docdir=/usr/share/doc/ffmpeg-4.0 &&
make &&
gcc tools/qt-faststart.c -o tools/qt-faststart
cd ..
可以成功编译并且在各个模块的源码目录下生成了so文件。注意make前最好使用make clean。
以下是一个介绍ffmpeg-4.0的文档,需要梯子:
http://www.linuxfromscratch.org/blfs/view/svn/multimedia/ffmpeg.html
下面是这个文档的内容:
FFmpeg-4.0
Introduction to FFmpeg
FFmpeg is a solution to record, convert and stream audio and video. It is a very fast video and audio converter and it can also acquire from a live audio/video source. Designed to be intuitive, the command-line interface (ffmpeg) tries to figure out all the parameters, when possible. FFmpeg can also convert from any sample rate to any other, and resize video on the fly with a high quality polyphase filter. FFmpeg can use a Video4Linux compatible video source and any Open Sound System audio source.
This package is known to build and work properly using an LFS-8.2 platform.
Package Information
Download (HTTP): http://ffmpeg.org/releases/ffmpeg-4.0.tar.xz
Download MD5 sum: 1cc9e8cb027b9fd4c54f598f51002c19
Download size: 8.3 MB
Estimated disk space required: 141 MB (add 3.1 GB for the FATE suite/tests, add 17 MB for docs)
Estimated build time: 1.7 SBU (using parallelism=4; add 3.8 SBU (with THREADS=4) to run the FATE suite after sample files are downloaded; add 0.4 SBU for docs)