熟悉命令之后,自然是對(duì)其根據(jù)自己的需求進(jìn)行應(yīng)用了。所以久等的第三編文章就來(lái)放放水。記錄一下在Android端的集成。
文章組織架構(gòu)
接下來(lái)幾遍文章將會(huì)按照以下結(jié)構(gòu)來(lái)進(jìn)行組織。
- 編譯FFmpeg For Android.
- 簡(jiǎn)單編寫(xiě)對(duì)應(yīng)的NDK來(lái)完成操作。
- 使用時(shí)遇到的一個(gè)大坑。
- gradle文件的修改和Cmake文件的編寫(xiě)
以上所有內(nèi)容都來(lái)自筆者的親身經(jīng)歷,如有巧合,必定是同道中人。
編譯FFmpeg For Android
編譯環(huán)境 MAC
其實(shí)這部分,不比多言了。雖然在網(wǎng)上可以找到很多類(lèi)似的經(jīng)驗(yàn),但其實(shí)第一次使用還是要花費(fèi)不少的時(shí)間。
Step1.修改FFmpeg的configure文件
下載完ffmpeg,并解壓。
打開(kāi) 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能夠使用的 名稱(chēng)-版本.so文件的格式。
Step2.編寫(xiě)Android編譯的腳本
!/bin/bash
#第一行是你自己的NDK路徑。后面兩行是對(duì)應(yīng)需要編譯的系統(tǒng)和編譯使用的toolchain
NDK=/Users/gavin/Develop/android-sdk/ndk-bundle
SYSROOT=$NDK/platforms/android-15/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64
CPU=arm
# 這個(gè)是輸出的路徑
PREFIX=$(pwd)/android/$CPU
ADDI_CFLAGS="-marm"
function build_one{
./configure \
--prefix=$PREFIX \
--enable-shared \
--disable-static \
--disable-doc \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--disable-avdevice \
--disable-doc \
--disable-symver \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--target-os=linux \
--arch=arm \
--enable-cross-compile \
--sysroot=$SYSROOT \
--extra-cflags="-Os -fpic $ADDI_CFLAGS" \
--extra-ldflags="$ADDI_LDFLAGS" \
$ADDITIONAL_CONFIGURE_FLAG
make clean
make
make install
}
build_one
NDK目錄改為自己的NDK目錄即可,關(guān)于configure配置,可以根據(jù)自己需要進(jìn)行配置,可配置的屬性在FFmpeg root dir的configure
文件中show_help方法中查看,我摘一段:
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--disable-ffserver
disable ffserver build
--disable-ffmpeg
意為禁用ffmpeg工具,編譯時(shí)不編譯出ffmpeg工具,--enable-ffmpeg
為啟用,但是configure文件配置有個(gè)特點(diǎn),F(xiàn)Fmpeg的默認(rèn)的配置不是以show_help方法中的配置配置的,而是以前綴disable or enable取反配置的,也就是FFmpeg中各屬性默認(rèn)的配置把show_help中各個(gè)配置的前綴取反即可,disable
變?yōu)閑nable,enable變?yōu)閐isable,如:
在上面那段配置中,--disable-ffmpeg
意為啟用ffmpeg工具。
但是我們編譯的bash腳本中卻不是這樣,而是配置的實(shí)意配置,disable
就是disable
。
這里需要注意的
so文件的大小
- 實(shí)際上這個(gè)腳本執(zhí)行完,會(huì)編譯出現(xiàn)多個(gè)so文件。而且會(huì)發(fā)現(xiàn),就算是單個(gè)
libavcodec-57.so
比較大,有7.6M。如果你的APP比較在意包的大小,使用就會(huì)很尷尬。所以,在編譯時(shí),我們可以針對(duì)自己需要的功能來(lái)進(jìn)行配置,更改bash腳本,加入配置:
--disable-everything
該屬性會(huì)把下列所有的組件都不加入編譯:
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 enable protocol NAME
--disable-protocol=NAME disable protocol NAME
--disable-protocols disable all protocols
--enable-indev=NAME enable input device NAME
--disable-indev=NAME disable input device NAME
--disable-indevs disable input devices
--enable-outdev=NAME enable output device NAME
--disable-outdev=NAME disable output device NAME
--disable-outdevs disable output devices
--disable-devices disable all devices
--enable-filter=NAME enable filter NAME
--disable-filter=NAME disable filter NAME
--disable-filters disable all filters
而我們生成的libavcodec-57.so
過(guò)大也是由于加入了過(guò)多我們不需要使用的組件,禁用了所有的,接下來(lái)就是把自己需要的加入到bash腳本中配置即可。比如加入下面兩個(gè)配置確保h264和aac的解碼功能的保留:
--enable-decoder=h264
--enable-decoder=aac
等等,當(dāng)然還有其它的,可以參考上面禁用的配置進(jìn)行選擇性保留。
2. 編譯支持H.264編碼的FFmpeg動(dòng)態(tài)鏈接庫(kù)
因?yàn)镕Fmpeg默認(rèn)是只支持H.264的解碼,不支持H.264的編碼。所以我們需要編譯H.264的編譯器。
編譯x264
首先下載x264源碼x264是一個(gè)開(kāi)源的H.264編碼器,據(jù)說(shuō)是最好的視頻有損編碼器。
和編譯FFmpeg類(lèi)似,要編譯x264成動(dòng)態(tài)so庫(kù),一樣通過(guò)configure配置文件,需要先修改configure文件中的:
echo "SOSUFFIX=so" >> config.mak
echo "SONAME=libx264.so.$API" >> config.mak
echo "SOFLAGS=-shared -Wl,-soname,\$(SONAME) $SOFLAGS" >> config.mak
替換成:
echo "SOSUFFIX=so" >> config.mak
echo "SONAME=libx264-$API.so" >> config.mak
echo "SOFLAGS=-shared -Wl,-soname,\$(SONAME) $SOFLAGS" >> config.mak
目的一樣,確保可以在Android平臺(tái)下可用,然后使用下面的bash腳本進(jìn)行編譯,arm為例:
#!/bin/bash?
NDK=$HOME/Library/Android/sdk/ndk-bundle
SYSROOT=$NDK/platforms/android-14/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64?
CPU=arm
PREFIX=$(pwd)/android/$CPUADDI_CFLAGS=""
ADDI_LDFLAGS=""
?function build_arm{
./configure \
--prefix=$PREFIX \
--enable-shared \
--disable-asm \
--enable-pic \
--enable-strip \
--host=arm-linux-androideabi \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--sysroot=$SYSROOT --extra-cflags="-Os -fpic $ADDI_CFLAGS"\
--extra-ldflags="$ADDI_LDFLAGS" \
$ADDITIONAL_CONFIGURE_FLAG
make clean
make
make install
}?build_arm
NDK目錄同樣改為自己的運(yùn)行上面腳本:
cd x264./build_x264_arm.sh
編譯完成后可以android目錄找到libx264-148.so
庫(kù)
靜態(tài)鏈接x264編譯
FFmpeg為我們提供了眾多配置,可添加額外功能庫(kù)的支持,方便我們自己鏈接所需要額外功能的庫(kù),在FFmpeg的configure文件配置中,可以看到這么一段配置:
External library support://...
--enable-libfdk-aac
enable AAC de/encoding via libfdk-aac [no]--enable-libopenh264
enable H.264 encoding via OpenH264 [no]--enable-libopenjpeg
enable JPEG 2000 de/encoding via OpenJPEG [no]--enable-librtmp
enable RTMP[E] support via librtmp [no]--enable-libwebp
enable WebP encoding via libwebp [no]--enable-libx264
enable H.264 encoding via x264 [no]--enable-opengl
enable OpenGL rendering [no]--enable-openssl
enable openssl, needed for https supportif gnutls is not used [no]//...
避免篇幅太長(zhǎng),做了一部分刪減,留了一些比較眼熟的,F(xiàn)Fmpeg可以讓我們自己配置添加需要的功能庫(kù),如使用fdk-aac庫(kù)來(lái)做aac的編解碼,H.264編碼可選x264或openh264以及可選其它功能庫(kù)的支持
要編譯支持x264編碼的FFmpeg動(dòng)態(tài)鏈接庫(kù),首先是編譯出x264靜態(tài)鏈接庫(kù)libx264.a
,然后修改FFmpeg的編譯腳本,配置x264靜態(tài)鏈接庫(kù)和頭文件路徑,接下來(lái)即可編譯了
-
編譯x264靜態(tài)鏈接庫(kù)**
因?yàn)镕Fmpeg是使用靜態(tài)鏈接方式鏈接其它額外的外部功能庫(kù),所以需要把x264庫(kù)編譯成.a靜態(tài)庫(kù),通過(guò)--enable-static
配置參數(shù)來(lái)編譯出.a靜態(tài)庫(kù),而--enable-shared
則是編譯動(dòng)態(tài)鏈接so庫(kù),所以只需在上面的x264腳本中增加個(gè)配置:
--enable-static
編譯成功后可以看到如下目錄結(jié)構(gòu): Fmpeg腳本中配置x264靜態(tài)鏈接庫(kù)和頭文件路徑及編譯**
在此之前,先安裝yasm:
brew install yasm
然后在之前FFmpeg的腳本基礎(chǔ)上添加以下配置:
--enable-gpl \
--enable-libx264 \
--enable-yasm \
--extra-cflags="-I../x264/android/arm/include" \
--extra-ldflags="-L../x264/android/arm/lib" \
#FFmpeg默認(rèn)的LICENSE是LGPL,而libx264需要GPL,所以加入
--enable-gpl
- 最后面兩項(xiàng)配置是配置編譯x264出的頭文件和靜態(tài)鏈接庫(kù)路徑,在這里我把x264和FFmpeg放在了同一目錄層級(jí),故可這樣配置。如果你需要修改
--extra-cflags
和--extra-ldflags
路徑為自己x264頭文件和靜態(tài)鏈接庫(kù)路徑,必須符合--extra-cflags
以-I開(kāi)頭,--extra-ldflags
以-L開(kāi)頭,這些參數(shù)含義為:
-D:用于在編譯時(shí)定義宏
-I:編譯階段生效的參數(shù),用于指定頭文件的搜索路徑
-L:鏈接階段生效的參數(shù),用于指定鏈接庫(kù)的搜索路徑,
-l用于指定鏈接庫(kù)的名稱(chēng),一般兩者一起使用的話,就可以指定動(dòng)態(tài)鏈接庫(kù)
比如x264在桌面上,路徑為:
--extra-cflags="-I/Users/Sunzxyong/Desktop/x264/android/arm/include" \
--extra-ldflags="-L/Users/Sunzxyong/Desktop/x264/android/arm/lib" \
然后進(jìn)行編譯,編譯中可以看到encode的支持選擇中,多了libx264的支持:
其它外部添加庫(kù)如fdk-aac、openh264等也類(lèi)似方式鏈接編譯!
Gradle文件及Cmake
Gradle文件
添加Ndk
abiFilter
以及 jniLibs
android {
defaultConfig {
//添加`abiFilter `
ndk {
abiFilters 'armeabi-v7a'
}
}
//jniLibs.就是我們放so文件的路徑
sourceSets {
main {
jniLibs.srcDirs = ['src/main/libs']
jni.srcDirs = []
}
}
}
編寫(xiě)Cmake文件
cmake_minimum_required(VERSION 3.4.1)
# 設(shè)置變量,方便底下使用
set(INC_DIR ${PROJECT_SOURCE_DIR}/libs/include)
set(LINK_DIR ${PROJECT_SOURCE_DIR}/libs/${ANDROID_ABI})
# 添加so庫(kù)對(duì)應(yīng)的頭文件目錄
include_directories(${INC_DIR})
# 引入so庫(kù),IMPORT代表從第三方引入的意思
add_library( avcodec-57 SHARED IMPORTED)
# 設(shè)置編譯的庫(kù)文件存放的目錄
set_target_properties( avcodec-57 PROPERTIES IMPORTED_LOCATION ${LINK_DIR}/libavcodec-57.so)
add_library( avfilter-6 SHARED IMPORTED)
set_target_properties( avfilter-6 PROPERTIES IMPORTED_LOCATION ${LINK_DIR}/libavfilter-6.so)
add_library( avformat-57 SHARED IMPORTED)
set_target_properties( avformat-57 PROPERTIES IMPORTED_LOCATION ${LINK_DIR}/libavformat-57.so)
add_library( avutil-55 SHARED IMPORTED)
set_target_properties( avutil-55 PROPERTIES IMPORTED_LOCATION ${LINK_DIR}/libavutil-55.so)
add_library( swresample-2 SHARED IMPORTED)
set_target_properties( swresample-2 PROPERTIES IMPORTED_LOCATION ${LINK_DIR}/libswresample-2.so)
add_library( swscale-4 SHARED IMPORTED)
set_target_properties( swscale-4 PROPERTIES IMPORTED_LOCATION ${LINK_DIR}/libswscale-4.so)
# 自己本地的代碼所編譯的庫(kù)
add_library( # Sets the name of the library.
ffmpeg_real
# Sets the library as a shared library.
SHARED
src/main/cpp/ffmpeg_box.c src/main/cpp/cmdutils.c src/main/cpp/ffmpeg.c src/main/cpp/ffmpeg_filter.c src/main/cpp/ffmpeg_opt.c
)
add_library( # Sets the name of the library.
ffmpeg_wrapper
SHARED
src/main/cpp/ffmpeg_wrapper.c
)
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# 將其他庫(kù)文件鏈接到native-lib上
target_link_libraries(
ffmpeg_real
ffmpeg_wrapper
avcodec-57
avfilter-6
avformat-57
avutil-55
swresample-2
swscale-4
${log-lib})