前言
上次解决了FFMpeg-kit编译的问题,但直接编译出来是没有硬件编码支持的,只有软件编解码,软编特别特别慢。所以这次还得继续改代码,让它支持在安卓上使用硬件编码。
如果你没有自己编译过FFMpeg-kit,建议先看看这篇编译教程:
【Android音视频】自编译FFMepg-Kit疑难杂症解决
目录结构
先简单了解一下这个项目,目录结构如下:
~/ffmpeg-kit$ tree -L 2
.
├── android <------安卓aar库项目,用于打包成aar
│ ├── build
│ ├── build.gradle
│ ├── ffmpeg-kit-android-lib
│ ├── gradle
│ ├── gradle.properties
│ ├── gradlew
│ ├── gradlew.bat
│ ├── jni
│ ├── libs
│ ├── obj
│ ├── README.md
│ └── settings.gradle
├── android.sh <------安卓一键编译打包脚本
├── apple <------苹果的库项目
│ ├── aclocal.m4
│ ├── ar-lib
│ ├── autogen.sh
│ ├── compile
│ ├── config.guess
│ ├── config.sub
│ ├── configure.ac
│ ├── depcomp
│ ├── Doxyfile
│ ├── install-sh
│ ├── Makefile.am
│ ├── missing
│ ├── README.md
│ └── src
├── apple.sh <------苹果的一键编译打包脚本
├── build.log <------编译日志,多次编译会把log追加在后面,按时间排列
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── docs <------离线文档
│ ├── android
│ ├── apple
│ ├── assets
│ ├── _config.yml
│ ├── index.md
│ └── linux
├── flutter <------Flutter的库项目
│ ├── flutter
│ └── flutter_platform_interface
├── ios.sh <------ios的一键打包编译脚本
├── LICENSE
├── linux <------Linux的库项目
│ ├── aclocal.m4
│ ├── ar-lib
│ ├── autogen.sh
│ ├── compile
│ ├── config.guess
│ ├── config.sub
│ ├── configure.ac
│ ├── depcomp
│ ├── Doxyfile
│ ├── install-sh
│ ├── m4
│ ├── Makefile.am
│ ├── missing
│ ├── README.md
│ └── src
├── linux.sh <------Linux的一键打包编译脚本
├── macos.sh <------MacOS的一键打包编译脚本
├── prebuilt <------已经编译好的组件
│ ├── android-arm
│ ├── android-arm64
│ ├── android-arm-neon
│ ├── android-x86
│ ├── android-x86_64
│ └── bundle-android-aar
├── react-native <------RN的库项目
│ ├── android
│ ├── babel.config.js
│ ├── ffmpeg-kit-react-native.podspec
│ ├── ios
│ ├── package.json
│ ├── README.md
│ └── src
├── README.md
├── scripts <------每个组件的编译脚本
│ ├── android
│ ├── apple
│ ├── function-android.sh
│ ├── function-apple.sh
│ ├── function-ios.sh
│ ├── function-linux.sh
│ ├── function-macos.sh
│ ├── function.sh
│ ├── function-tvos.sh
│ ├── linux
│ ├── main-android.sh
│ ├── main-ios.sh
│ ├── main-linux.sh
│ ├── main-macos.sh
│ ├── main-tvos.sh
│ ├── run-android.sh
│ ├── run-apple.sh
│ ├── run-linux.sh
│ ├── source.sh <------版本控制,里面有每个组件的版本和仓库路径
│ └── variable.sh
├── src <------源码,第三方库的源码也放在这里
│ ├── chromaprint
│ ├── cpu-features
│ ├── dav1d
│ ├── expat
│ ├── ffmpeg
│ ├── fontconfig
│ ├── freetype
│ ├── fribidi
│ ├── giflib
│ ├── gmp
│ ├── gnutls
│ ├── harfbuzz
│ ├── jpeg
│ ├── kvazaar
│ ├── lame
│ ├── leptonica
│ ├── libaom
│ ├── libass
│ ├── libiconv
│ ├── libilbc
│ ├── libogg
│ ├── libpng
│ ├── libsamplerate
│ ├── libsndfile
│ ├── libtheora
│ ├── libuuid
│ ├── libvidstab
│ ├── libvorbis
│ ├── libvpx
│ ├── libwebp
│ ├── libxml2
│ ├── nettle
│ ├── opencore-amr
│ ├── openh264
│ ├── openssl
│ ├── opus
│ ├── rubberband
│ ├── sdl
│ ├── shine
│ ├── snappy
│ ├── soxr
│ ├── speex
│ ├── srt
│ ├── tesseract
│ ├── tiff
│ ├── twolame
│ ├── vo-amrwbenc
│ ├── x264
│ ├── x265
│ ├── xvidcore
│ └── zimg
├── tools <------编译工具
│ ├── android
│ ├── apple
│ ├── clean.sh
│ ├── docs
│ ├── license
│ ├── patch
│ ├── protocols
│ └── source
└── tvos.sh <------tvos的一键打包编译脚本
95 directories, 67 files
这玩意支持多个平台的编译,我们只关心安卓平台。重点关注以下文件:
android.sh
scripts/android/ffmpeg.sh
source.sh
读参数
先看看编译的参数选项,安卓平台编译使用目录下的android.sh
这个脚本进行,先看看--help
./android.sh --help
'android.sh' builds FFmpegKit for Android platform. By default five Android architectures
(armeabi-v7a, armeabi-v7a-neon, arm64-v8a, x86 and x86_64) are built without any external
libraries enabled. Options can be used to disable architectures 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.
这里可以看到一共支持5个架构:armeabi-v7a, armeabi-v7a-neon, arm64-v8a, x86, x86_64
Usage: ./android.sh [OPTION]... [VAR=VALUE]...
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
-f, --force ignore warnings
-l, --lts build lts packages to support API 16+ devices
--api-level=api override Android api level
--no-ffmpeg-kit-protocols disable custom ffmpeg-kit protocols (saf)
加这个参数可以引用其他GPL第三方库
Licensing options:
--enable-gpl allow building GPL libraries, created libs will be licensed
under the GPLv3.0 [no]
加一条就会ban掉一个架构,每个架构都需要单独编译,所以不需要的就尽情地ban掉
虽然这里括号写的是yes,但实际上你不加参数的话它会编译全部的5个架构。
Architectures:
--disable-arm-v7a do not build arm-v7a architecture [yes]
--disable-arm-v7a-neon do not build arm-v7a-neon architecture [yes]
--disable-arm64-v8a do not build arm64-v8a architecture [yes]
--disable-x86 do not build x86 architecture [yes]
--disable-x86-64 do not build x86-64 architecture [yes]
这里是第三方库相关的支持,加一个就会单独加入一个库,full就是全加。
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-dav1d build with dav1d [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]
--enable-libwebp build with libwebp [no]
--enable-libxml2 build with libxml2 [no]
--enable-opencore-amr build with opencore-amr [no]
--enable-openh264 build with openh264 [no]
--enable-openssl build with openssl [no]
--enable-opus build with opus [no]
--enable-sdl build with sdl [no]
--enable-shine build with shine [no]
--enable-snappy build with snappy [no]
--enable-soxr build with soxr [no]
--enable-speex build with speex [no]
--enable-srt build with srt [no]
--enable-tesseract build with tesseract [no]
--enable-twolame build with twolame [no]
--enable-vo-amrwbenc build with vo-amrwbenc [no]
--enable-zimg build with zimg [no]
GPL库,加一个就是单独引入一个,必须和--enable-gpl一起使用
GPL libraries:
--enable-libvidstab build with libvidstab [no]
--enable-rubberband build with rubber band [no]
--enable-x264 build with x264 [no]
--enable-x265 build with x265 [no]
--enable-xvidcore build with xvidcore [no]
Custom libraries:
--enable-custom-library-[n]-name=value name of the custom library []
--enable-custom-library-[n]-repo=value git repository of the source code []
--enable-custom-library-[n]-repo-commit=value git commit to download the source code from []
--enable-custom-library-[n]-repo-tag=value git tag to download the source code from []
--enable-custom-library-[n]-package-config-file-name=value package config file installed by the build script []
--enable-custom-library-[n]-ffmpeg-enable-flag=value library name used in ffmpeg configure script to enable the library []
--enable-custom-library-[n]-license-file=value licence file path relative to the library source folder []
--enable-custom-library-[n]-uses-cpp flag to specify that the library uses libc++ []
Advanced options:
--reconf-LIBRARY run autoreconf before building LIBRARY [no]
--redownload-LIBRARY download LIBRARY even if it is detected as already downloaded [no]
--rebuild-LIBRARY build LIBRARY even if it is detected as already built [no]
这条表示编译之后不打包为aar
--no-archive do not build Android archive [no]
添加硬件编码支持
简单分析一下,我们需要支持安卓的硬件编码器,所以需要mediacodec
相关的库,以及名字带安卓的库,其他相关的库看心情加。
这里以arm64-v8a
平台为例,添加大多数常见的库,指令如下:
./android.sh --enable-gpl --enable-libvidstab --enable-rubberband --enable-x264 --enable-x265 --enable-xvidcore --disable-arm-v7a --disable-arm-v7a-neon --disable-x86 --disable-x86-64 --enable-android-media-codec --enable-android-zlib --enable-kvazaar --enable-lame --enable-libaom --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libwebp --enable-opencore-amr --enable-openh264 --enable-opus --enable-speex --enable-twolame --enable-vo-amrwbenc --enable-shine --enable-snappy --enable-soxr --enable-sdl
这些第三方库会下载到src/[库名称]
目录下,需要用到git
和gradle
,建议开魔法,如果gradle
的插件下载不下来,可以看看开头提到的上一篇文章有写怎么处理。
升级FFMpeg版本
除了FFMepg-kit
的参数以外,FFMepg
自己也有编译参数。
cd src/ffmpeg
./configure --help
Usage: configure [options]
Options: [defaults in brackets after descriptions]
Help options:
--help print this message
--quiet Suppress showing informative output
--list-decoders show all available decoders <------查看当前版本支持的解码器
--list-encoders show all available encoders <------查看当前版本支持的编码器
--list-hwaccels show all available hardware accelerators
--list-demuxers show all available demuxers
--list-muxers show all available muxers
--list-parsers show all available parsers
--list-protocols show all available protocols
--list-bsfs show all available bitstream filters
--list-indevs show all available input devices
--list-outdevs show all available output devices
--list-filters show all available filters
Standard options:
--logfile=FILE log tests and output to FILE [ffbuild/config.log]
--disable-logging do not log configure debug information
--fatal-warnings fail if any configure warning is generated
--prefix=PREFIX install in PREFIX [/usr/local]
--bindir=DIR install binaries in DIR [PREFIX/bin]
--datadir=DIR install data files in DIR [PREFIX/share/ffmpeg]
--docdir=DIR install documentation in DIR [PREFIX/share/doc/ffmpeg]
--libdir=DIR install libs in DIR [PREFIX/lib]
--shlibdir=DIR install shared libs in DIR [LIBDIR]
--incdir=DIR install includes in DIR [PREFIX/include]
--mandir=DIR install man page in DIR [PREFIX/share/man]
--pkgconfigdir=DIR install pkg-config files in DIR [LIBDIR/pkgconfig]
--enable-rpath use rpath to allow installing libraries in paths
not part of the dynamic linker search path
use rpath when linking programs (USE WITH CARE)
--install-name-dir=DIR Darwin directory name for installed targets
Licensing options:
--enable-gpl allow use of GPL code, the resulting libs <------使用第三方GPL库
and binaries will be under GPL [no]
--enable-version3 upgrade (L)GPL to version 3 [no]
--enable-nonfree allow use of nonfree code, the resulting libs
and binaries will be unredistributable [no]
Configuration options:
--disable-static do not build static libraries [no]
--enable-shared build shared libraries [no]
--enable-small optimize for size instead of speed
--disable-runtime-cpudetect disable detecting CPU capabilities at runtime (smaller binary)
--enable-gray enable full grayscale support (slower color)
--disable-swscale-alpha disable alpha channel support in swscale
--disable-all disable building components, libraries and programs
--disable-autodetect disable automatically detected external libraries [no]
Program options:
--disable-programs do not build command line programs
--disable-ffmpeg disable ffmpeg build
--disable-ffplay disable ffplay build
--disable-ffprobe disable ffprobe build
Documentation options:
--disable-doc do not build documentation
--disable-htmlpages do not build HTML documentation pages
--disable-manpages do not build man documentation pages
--disable-podpages do not build POD documentation pages
--disable-txtpages do not build text documentation pages
ban掉指定的组件
Component options:
--disable-avdevice disable libavdevice build
--disable-avcodec disable libavcodec build
--disable-avformat disable libavformat build
--disable-swresample disable libswresample build
--disable-swscale disable libswscale build
--disable-postproc disable libpostproc build
--disable-avfilter disable libavfilter build
--disable-pthreads disable pthreads [autodetect]
--disable-w32threads disable Win32 threads [autodetect]
--disable-os2threads disable OS/2 threads [autodetect]
--disable-network disable network support [no]
--disable-dwt disable DWT code
--disable-error-resilience disable error resilience code
--disable-lsp disable LSP code
--disable-faan disable floating point AAN (I)DCT code
--disable-iamf disable support for Immersive Audio Model
--disable-pixelutils disable pixel utils in libavutil
Individual component options:
--disable-everything disable all components listed below
--disable-encoder=NAME disable encoder NAME
--enable-encoder=NAME enable encoder NAME
--disable-encoders disable all encoders
--disable-decoder=NAME disable decoder NAME
--enable-decoder=NAME enable decoder NAME
--disable-decoders disable all decoders
--disable-hwaccel=NAME disable hwaccel NAME
--enable-hwaccel=NAME enable hwaccel NAME
--disable-hwaccels disable all hwaccels
--disable-muxer=NAME disable muxer NAME
--enable-muxer=NAME enable muxer NAME
--disable-muxers disable all muxers
--disable-demuxer=NAME disable demuxer NAME
--enable-demuxer=NAME enable demuxer NAME
--disable-demuxers disable all demuxers
--enable-parser=NAME enable parser NAME
--disable-parser=NAME disable parser NAME
--disable-parsers disable all parsers
--enable-bsf=NAME enable bitstream filter NAME
--disable-bsf=NAME disable bitstream filter NAME
--disable-bsfs disable all bitstream filters
--enable-protocol=NAME