做多媒体,离不开ffmpeg。互联网时代,离不开Android。所以,Android集成ffmpeg的需求,来开发多媒体相关功能,越来越多。

幸运的是,github已经有专门的项目,来跟进。集成ffmpeg变的简单了。

1. 下载源码

项目地址:https://github.com/tanersener/mobile-ffmpeg

2. 编译准备

先设置2个环境变量。

export ANDROID_HOME=<Android SDK Path>
export ANDROID_NDK_ROOT=<Android NDK Path>

请注意,mobile-ffmpeg有要求最低版本号:

Android SDK 4.1 Jelly Bean (API Level 16) or later
Android NDK r21 or later with LLDB and CMake

Android sdk路径,就是你AndroidStudio的SDK路径。如果不知道在哪里,就打开AndroidStudio -> Tools -> SDK Manager。
在这里插入图片描述
ndk呢,也可以通过AndroidStudio下载一个。

还是Tools -> SDK Manager,点击SDK Tools 面板, Show Packages Details选中,然后NDK选择一个版本。

注意,mobileffmpeg有要求NDK的版本,如果没有,会编译失败。例如,我就遇到如下错误:

> No version of NDK matched the requested version 21.3.6528147. Versions available locally: 20.0.5594570, 20.1.5948944, 20.1.5948944

怎么看mobile-ffmpeg需要什么版本呢?打开如下文件:
./android/app/build.gradle
里面会写ndkVersion,我这里的是:

ndkVersion "21.3.6528147"

于是,我在AndroidStudio就需要下载这个版本。
在这里插入图片描述

下载好以后,上面的ANDROID_NDK_ROOT环境变量,就设置为具体路径。最后我设置的是:

export ANDROID_HOME=/home/chenxiaofeng/Android/Sdk
export ANDROID_NDK_ROOT=/home/chenxiaofeng/Android/Sdk/ndk/21.3.6528147

你也可以把这2个设置,写到home目录的 .bashrc文件,这样每次打开Terminal都会自动设置了。

3. 编译

编译只需要执行android.sh,默认不会加ffmpeg的额外模块,CPU类型也会全部编译。如果要定制,就加参数。具体参数,可以来个–help了解一下

$ ./android.sh --help

'android.sh' builds FFmpeg and MobileFFmpeg for Android platform. By default five Android ABIs (armeabi-v7a, armeabi-v7a-neon, arm64-v8a, x86 and x86_64) are built without any external libraries enabled. Options can be used to disable ABIs and/or enable external libraries. Please note that GPL libraries (external libraries with GPL license) need --enable-gpl flag to be set explicitly. When compilation ends an Android Archive (AAR) file is created under the prebuilt folder.

Usage: ./android.sh [OPTION]...

Specify environment variables as VARIABLE=VALUE to override default build options.

Options:
  -h, --help			display this help and exit
  -v, --version			display version information and exit
  -d, --debug			build with debug information
  -s, --speed			optimize for speed instead of size
  -l, --lts			build lts packages to support API 16+ devices
  -f, --force			ignore warnings

Licensing options:
  --enable-gpl			allow use of GPL libraries, created libs will be licensed under GPLv3.0 [no]

Platforms:
  --disable-arm-v7a		do not build arm-v7a platform [yes]
  --disable-arm-v7a-neon	do not build arm-v7a-neon platform [yes]
  --disable-arm64-v8a		do not build arm64-v8a platform [yes]
  --disable-x86			do not build x86 platform [yes]
  --disable-x86-64		do not build x86-64 platform [yes]

Libraries:
  --full			enables all external libraries
  --enable-android-media-codec	build with built-in Android MediaCodec support[no]
  --enable-android-zlib		build with built-in zlib support[no]
  --enable-chromaprint		build with chromaprint [no]
  --enable-fontconfig		build with fontconfig [no]
  --enable-freetype		build with freetype [no]
  --enable-fribidi		build with fribidi [no]
  --enable-gmp			build with gmp [no]
  --enable-gnutls		build with gnutls [no]
  --enable-kvazaar		build with kvazaar [no]
  --enable-lame			build with lame [no]
  --enable-libaom		build with libaom [no]
  --enable-libass		build with libass [no]
  --enable-libiconv		build with libiconv [no]
  --enable-libilbc		build with libilbc [no]
  --enable-libtheora		build with libtheora [no]
  --enable-libvorbis		build with libvorbis [no]
  --enable-libvpx		build with libvpx [no]

比如我的需求,是值需要armv8,且需要编译libvpx & libopus,则编译命令如下:

./android.sh --disable-arm-v7a --disable-arm-v7a-neon --disable-x86 --disable-x86-64 --enable-libvpx --enable-android-media-codec --enable-opus

编译结果,会帮我们打包出aar,内部有简单的JNI封装和so。

Building mobile-ffmpeg library for Android

Architectures: arm64-v8a
Libraries: android-media-codec, cpu-features, libvpx, opus

Building arm64-v8a platform on API level 24

libvpx: already built
opus: already built
cpu-features: already built

ffmpeg: ok

mobile-ffmpeg: ok


Creating Android archive under prebuilt/android-aar: ok

如果你编译出错,可以打开源码目录下的build.log,根据日志具体看什么问题。

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐