1. 程式人生 > >android ffmpeg 動態庫編譯

android ffmpeg 動態庫編譯

  • 環境搭建
  • 程式碼下載
  • 配置編譯
  • 動態庫輸出

環境搭建

tar -xzvf android-ndk-r14b-linux-x86_64.zip
cd android-ndk-r14b
pwd
/home/xxxxxx/ndk/android-ndk-r14b

程式碼下載

配置編譯

下載完成後,進入工程目錄
#cd FFmpeg-Android
# ls
drwxr-xr-x 3 huanyu.ke szsoftware 19 Jul 20 13:13 build
-rw-r–r– 1 huanyu.ke szsoftware 7651 Jul 14 20:15 COPYING.LGPLv3
drwxr-xr-x 18 huanyu.ke szsoftware 4096 Jul 20 11:43 ffmpeg
-rwxr-xr-x 1 huanyu.ke szsoftware 3481 Jul 20 2017 FFmpeg-Android.sh
-rw-r–r– 1 huanyu.ke szsoftware 40 Jul 20 13:13 ffmpeg-version
-rw-r–r– 1 huanyu.ke szsoftware 22840 Jul 14 20:15 FFmpeg-VPlayer.patch
drwxr-xr-x 8 huanyu.ke szsoftware 4096 Jul 20 16:42 .git
-rw-r–r– 1 huanyu.ke szsoftware 0 Jul 14 20:15 .gitignore
-rw-r–r– 1 huanyu.ke szsoftware 2267 Jul 14 20:15 LICENSE
-rw-r–r– 1 huanyu.ke szsoftware 20624 Jul 14 20:15 MAINTAINERS
-rw-r–r– 1 huanyu.ke szsoftware 282 Jul 14 20:16 README.md
給編譯指令碼FFmpeg-Android.sh 打上patch
#patch -p1 < android-ffmpeg-complise.patch
主要是設定NDK路徑,工具鏈路徑。注意SYSROOT,NDK 指定的路徑platform要一致,還要新增ccache路徑,以及一些警告當錯誤的flag去掉。
android-ffmpeg-complise.patch的內容如下:

diff --git a/FFmpeg-Android.sh b/FFmpeg-Android.sh
index 8aa15c3..a436431 100755
--- a/FFmpeg-Android.sh
+++ b/FFmpeg-Android.sh
@@ -18,26 +18,33 @@ patch -p1 <../FFmpeg-VPlayer.patch

 git log --pretty=format:%H -1 > ../ffmpeg-version

+CCACHE=/home/xxxx.xx/usr/local/bin
+ANDROID_NDK=/home/xxxx.xx/ndk/android-ndk-r14b
+
+#notice: The Access permissions for directories
 TOOLCHAIN=/tmp/vplayer
-SYSROOT
=$TOOLCHAIN/sysroot/
-$ANDROID_NDK/build/tools/make-standalone-toolchain.sh --platform=android-14 --install-dir=$TOOLCHAIN +SYSROOT=$ANDROID_NDK/platforms/android-23/arch-arm +$ANDROID_NDK/build/tools/make-standalone-toolchain.sh --platform=android-23 --install-dir=$TOOLCHAIN -export PATH=$TOOLCHAIN/bin:$PATH
+export PATH=$TOOLCHAIN/bin:$PATH:$CCACHE export CC="ccache arm-linux-androideabi-gcc" export LD=arm-linux-androideabi-ld export AR=arm-linux-androideabi-ar CFLAGS="-O3 -Wall -mthumb -pipe -fpic -fasm \ -finline-limit=300 -ffast-math \ - -fstrict-aliasing -Werror=strict-aliasing \ + -fstrict-aliasing \ -fmodulo-sched -fmodulo-sched-allow-regmoves \ -Wno-psabi -Wa,--noexecstack \ -D__ARM_ARCH_5__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5TE__ \ -DANDROID -DNDEBUG" + +# config the flags of the ffmpeg FFMPEG_FLAGS="--target-os=linux \ --arch=arm \ --enable-cross-compile \ + --sysroot=$SYSROOT \ --cross-prefix=arm-linux-androideabi- \ --enable-shared \ --disable-symver \ @@ -102,14 +109,21 @@ for version in neon armv7 vfp armv6; do cp config.* $PREFIX [ $PIPESTATUS == 0 ] || exit 1 + ## clean the old file make clean - make -j4 || exit 1 + ## start to compile + make -j16 || exit 1 + ## start to install the ffmpeg library and the header file make install || exit 1 rm libavcodec/inverse.o + + ## start to package the dynamic library libffmpeg.so $CC -lm -lz -shared --sysroot=$SYSROOT -Wl,--no-undefined -Wl,-z,noexecstack $EXTRA_LDFLAGS libavutil/*.o libavutil/arm/*.o libavcodec/*.o libavcodec/arm/*.o libavfo + ## output the dynamic library libffmpeg.so cp $PREFIX/libffmpeg.so $PREFIX/libffmpeg-debug.so + ## compress the libffmpeg.so arm-linux-androideabi-strip --strip-unneeded $PREFIX/libffmpeg.so done

配置完成後就開始編譯。
# ./FFmpeg-Android.sh

動態庫輸出

編譯完成之後,build目錄下會生成四個平臺的動態庫libffmpeg.so
#find . -iname “libffmpeg.so”
./build/ffmpeg/neon/libffmpeg.so
./build/ffmpeg/armv7/libffmpeg.so
./build/ffmpeg/vfp/libffmpeg.so
./build/ffmpeg/armv6/libffmpeg.so
生成的動態庫就可以提供給JNI 封裝,最後提供給APP使用。