环境是ubuntu16.04 LTS
首先需要安装NDK,安装步骤参考https://blog.csdn.net/fanx9339/article/details/81116332,NDK版本我用的是android-ndk-r10e
1、下载ffmpeg source code
链接:http://www.ffmpeg.org/download.html
最新版编译会有问题,我这里使用的是ffmpeg-3.0.11,复制到Linux的目录下,然后使用tar -xvf ffmpeg-3.0.11.tar.gz命令解压
2、编译ffmpeg
编译前我们需要修改ffmpeg目录下的configure文件。由于编译出来的动态库文件名的版本号在.so之后(例如“libavcodec.so.5.100.1”),而android平台不能识别这样文件名,所以需要修改这种文件名。在configure文件中找到下面几行代码(在3072-3076行)::
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)'
保存后,接下里就是编写编译脚本了。最好是在Linux环境下编写,不要有任何多的空格,防止有意想不到的错误,NDK,SYSROOT,TOOLCHAIN指向自己实际的目录:
fanx@ubuntu:~/ffmpeg/ffmpeg-4.0.2$ vi build_android.sh
#!/bin/bash
CPU=arm
PREFIX=$(pwd)/android/$CPU
ADDI_CFLAGS="-marm"
NDK=/home/fanx/ANDROID_NDK/android-ndk-r10e
SYSROOT=$NDK/platforms/android-21/arch-arm
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64
./configure \
--prefix=$PREFIX \
--target-os=linux \
--enable-cross-compile \
--enable-runtime-cpudetect \
--disable-asm \
--arch=arm \
--enable-shared \
--disable-static \
--disable-doc \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-symver \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--sysroot=$SYSROOT \
--extra-cflags="-Os -fpic $ADDI_CFLAGS" \
--extra-ldflags="$ADDI_LDFLAGS" \
$ADDITIONAL_CONFIGURE_FLAG
make
make install
直接保存退出,赋予执行权限:
fanx@ubuntu:~/ffmpeg/ffmpeg-4.0.2$ chmod +x build_andoird.sh
然后运行脚本开始编译:
fanx@ubuntu:~/ffmpeg/ffmpeg-4.0.2$ ./build_andoird.sh
等一段时间后,编译完成,编译生成在../android目录结构:
fanx@ubuntu:~/ffmpeg/ffmpeg-3.0.11$ tree android/
android/
└── arm
├── bin
│ └── ffserver
├── include
│ ├── libavcodec
│ │ ├── avcodec.h
│ │ ├── avdct.h
│ │ ├── avfft.h
│ │ ├── d3d11va.h
│ │ ├── dirac.h
│ │ ├── dv_profile.h
│ │ ├── dxva2.h
│ │ ├── qsv.h
│ │ ├── vaapi.h
│ │ ├── vda.h
│ │ ├── vdpau.h
│ │ ├── version.h
│ │ ├── videotoolbox.h
│ │ ├── vorbis_parser.h
│ │ └── xvmc.h
│ ├── libavdevice
│ │ ├── avdevice.h
│ │ └── version.h
│ ├── libavfilter
│ │ ├── avfiltergraph.h
│ │ ├── avfilter.h
│ │ ├── buffersink.h
│ │ ├── buffersrc.h
│ │ └── version.h
│ ├── libavformat
│ │ ├── avformat.h
│ │ ├── avio.h
│ │ └── version.h
│ ├── libavutil
│ │ ├── adler32.h
│ │ ├── aes_ctr.h
│ │ ├── aes.h
│ │ ├── attributes.h
│ │ ├── audio_fifo.h
│ │ ├── avassert.h
│ │ ├── avconfig.h
│ │ ├── avstring.h
│ │ ├── avutil.h
│ │ ├── base64.h
│ │ ├── blowfish.h
│ │ ├── bprint.h
│ │ ├── bswap.h
│ │ ├── buffer.h
│ │ ├── camellia.h
│ │ ├── cast5.h
│ │ ├── channel_layout.h
│ │ ├── common.h
│ │ ├── cpu.h
│ │ ├── crc.h
│ │ ├── des.h
│ │ ├── dict.h
│ │ ├── display.h
│ │ ├── downmix_info.h
│ │ ├── error.h
│ │ ├── eval.h
│ │ ├── ffversion.h
│ │ ├── fifo.h
│ │ ├── file.h
│ │ ├── frame.h
│ │ ├── hash.h
│ │ ├── hmac.h
│ │ ├── imgutils.h
│ │ ├── intfloat.h
│ │ ├── intreadwrite.h
│ │ ├── lfg.h
│ │ ├── log.h
│ │ ├── lzo.h
│ │ ├── macros.h
│ │ ├── mastering_display_metadata.h
│ │ ├── mathematics.h
│ │ ├── md5.h
│ │ ├── mem.h
│ │ ├── motion_vector.h
│ │ ├── murmur3.h
│ │ ├── opt.h
│ │ ├── parseutils.h
│ │ ├── pixdesc.h
│ │ ├── pixelutils.h
│ │ ├── pixfmt.h
│ │ ├── random_seed.h
│ │ ├── rational.h
│ │ ├── rc4.h
│ │ ├── replaygain.h
│ │ ├── ripemd.h
│ │ ├── samplefmt.h
│ │ ├── sha512.h
│ │ ├── sha.h
│ │ ├── stereo3d.h
│ │ ├── tea.h
│ │ ├── threadmessage.h
│ │ ├── timecode.h
│ │ ├── time.h
│ │ ├── timestamp.h
│ │ ├── tree.h
│ │ ├── twofish.h
│ │ ├── version.h
│ │ └── xtea.h
│ ├── libswresample
│ │ ├── swresample.h
│ │ └── version.h
│ └── libswscale
│ ├── swscale.h
│ └── version.h
├── lib
│ ├── libavcodec-57.so
│ ├── libavcodec.so -> libavcodec-57.so
│ ├── libavdevice-57.so
│ ├── libavdevice.so -> libavdevice-57.so
│ ├── libavfilter-6.so
│ ├── libavfilter.so -> libavfilter-6.so
│ ├── libavformat-57.so
│ ├── libavformat.so -> libavformat-57.so
│ ├── libavutil-55.so
│ ├── libavutil.so -> libavutil-55.so
│ ├── libswresample-2.so
│ ├── libswresample.so -> libswresample-2.so
│ ├── libswscale-4.so
│ ├── libswscale.so -> libswscale-4.so
│ └── pkgconfig
│ ├── libavcodec.pc
│ ├── libavdevice.pc
│ ├── libavfilter.pc
│ ├── libavformat.pc
│ ├── libavutil.pc
│ ├── libswresample.pc
│ └── libswscale.pc
└── share
└── ffmpeg
├── examples
│ ├── avio_dir_cmd.c
│ ├── avio_reading.c
│ ├── decoding_encoding.c
│ ├── demuxing_decoding.c
│ ├── extract_mvs.c
│ ├── filter_audio.c
│ ├── filtering_audio.c
│ ├── filtering_video.c
│ ├── http_multiclient.c
│ ├── Makefile
│ ├── metadata.c
│ ├── muxing.c
│ ├── qsvdec.c
│ ├── README
│ ├── remuxing.c
│ ├── resampling_audio.c
│ ├── scaling_video.c
│ ├── transcode_aac.c
│ └── transcoding.c
├── ffprobe.xsd
├── libvpx-1080p50_60.ffpreset
├── libvpx-1080p.ffpreset
├── libvpx-360p.ffpreset
├── libvpx-720p50_60.ffpreset
└── libvpx-720p.ffpreset
3、移植到Android Studio
请参考另外一位兄弟的文章:https://blog.csdn.net/ywl5320/article/details/75136986