1. 程式人生 > >android全平臺編譯ffmpeg以及x264與fdk-aac實踐

android全平臺編譯ffmpeg以及x264與fdk-aac實踐

目錄

編譯環境

作業系統:ubuntu 16.05

android-ndk-r10e

注意 這裡如果使用高版本比如android-ndk-r16b 編譯會報錯

ffmpeg-3.3.8版本

fdk-aac-0.1.6版本

單獨編譯完整功能ffmpeg庫

編輯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)'

編寫config.sh環境配置檔案

#NDK路徑
export ANDROID_NDK_ROOT=/home/byhook/android/android-ndk-r10e

export AOSP_TOOLCHAIN_SUFFIX=4.9

export AOSP_API="android-21"

#架構
if [ "$#" -lt 1 ]; then
	THE_ARCH=armv7
else
	THE_ARCH=$(tr [A-Z] [a-z] <<< "$1")
fi

#根據不同架構配置環境變數
case
"$THE_ARCH" in arm|armv5|armv6|armv7|armeabi) TOOLCHAIN_BASE="arm-linux-androideabi" TOOLNAME_BASE="arm-linux-androideabi" AOSP_ABI="armeabi" AOSP_ARCH="arch-arm" HOST="arm-linux-androideabi" AOSP_FLAGS="-march=armv5te -mtune=xscale -mthumb -msoft-float -funwind-tables -fexceptions -frtti" FF_EXTRA_CFLAGS="-O3 -fpic -fasm -Wno-psabi -fno-short-enums -fno-strict-aliasing -finline-limit=300 -mfloat-abi=softfp -mfpu=vfp -marm -march=armv6 " FF_CFLAGS="-O3 -Wall -pipe -ffast-math -fstrict-aliasing -Werror=strict-aliasing -Wno-psabi -Wa,--noexecstack -DANDROID " ;; armv7a|armeabi-v7a) TOOLCHAIN_BASE="arm-linux-androideabi" TOOLNAME_BASE="arm-linux-androideabi" AOSP_ABI="armeabi-v7a" AOSP_ARCH="arch-arm" HOST="arm-linux-androideabi" AOSP_FLAGS="-march=armv7-a -mthumb -mfpu=vfpv3-d16 -mfloat-abi=softfp -Wl,--fix-cortex-a8 -funwind-tables -fexceptions -frtti" FF_EXTRA_CFLAGS="-DANDROID -fPIC -ffunction-sections -funwind-tables -fstack-protector -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fomit-frame-pointer -fstrict-aliasing -funswitch-loops -finline-limit=300 " FF_CFLAGS="-O3 -Wall -pipe -ffast-math -fstrict-aliasing -Werror=strict-aliasing -Wno-psabi -Wa,--noexecstack -DANDROID " ;; hard|armv7a-hard|armeabi-v7a-hard) TOOLCHAIN_BASE="arm-linux-androideabi" TOOLNAME_BASE="arm-linux-androideabi" AOSP_ABI="armeabi-v7a" AOSP_ARCH="arch-arm" HOST="arm-linux-androideabi" AOSP_FLAGS="-mhard-float -D_NDK_MATH_NO_SOFTFP=1 -march=armv7-a -mfpu=vfpv3-d16 -mfloat-abi=softfp -Wl,--fix-cortex-a8 -funwind-tables -fexceptions -frtti -Wl,--no-warn-mismatch -Wl,-lm_hard" FF_EXTRA_CFLAGS="-DANDROID -fPIC -ffunction-sections -funwind-tables -fstack-protector -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fomit-frame-pointer -fstrict-aliasing -funswitch-loops -finline-limit=300 " FF_CFLAGS="-O3 -Wall -pipe -ffast-math -fstrict-aliasing -Werror=strict-aliasing -Wno-psabi -Wa,--noexecstack -DANDROID " ;; neon|armv7a-neon) TOOLCHAIN_BASE="arm-linux-androideabi" TOOLNAME_BASE="arm-linux-androideabi" AOSP_ABI="armeabi-v7a" AOSP_ARCH="arch-arm" HOST="arm-linux-androideabi" AOSP_FLAGS="-march=armv7-a -mfpu=vfpv3-d16 -mfloat-abi=softfp -Wl,--fix-cortex-a8 -funwind-tables -fexceptions -frtti" FF_EXTRA_CFLAGS="-DANDROID -fPIC -ffunction-sections -funwind-tables -fstack-protector -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fomit-frame-pointer -fstrict-aliasing -funswitch-loops -finline-limit=300 " FF_CFLAGS="-O3 -Wall -pipe -ffast-math -fstrict-aliasing -Werror=strict-aliasing -Wno-psabi -Wa,--noexecstack -DANDROID " ;; armv8|armv8a|aarch64|arm64|arm64-v8a) TOOLCHAIN_BASE="aarch64-linux-android" TOOLNAME_BASE="aarch64-linux-android" AOSP_ABI="arm64-v8a" AOSP_ARCH="arch-arm64" HOST="aarch64-linux" AOSP_FLAGS="-funwind-tables -fexceptions -frtti" FF_EXTRA_CFLAGS="" FF_CFLAGS="-O3 -Wall -pipe -ffast-math -fstrict-aliasing -Werror=strict-aliasing -Wno-psabi -Wa,--noexecstack -DANDROID " ;; mips|mipsel) TOOLCHAIN_BASE="mipsel-linux-android" TOOLNAME_BASE="mipsel-linux-android" AOSP_ABI="mips" AOSP_ARCH="arch-mips" HOST="mipsel-linux" AOSP_FLAGS="-funwind-tables -fexceptions -frtti" ;; mips64|mipsel64|mips64el) TOOLCHAIN_BASE="mips64el-linux-android" TOOLNAME_BASE="mips64el-linux-android" AOSP_ABI="mips64" AOSP_ARCH="arch-mips64" HOST="mipsel64-linux" AOSP_FLAGS="-funwind-tables -fexceptions -frtti" ;; x86) TOOLCHAIN_BASE="x86" TOOLNAME_BASE="i686-linux-android" AOSP_ABI="x86" AOSP_ARCH="arch-x86" HOST="i686-linux" AOSP_FLAGS="-march=i686 -mtune=intel -mssse3 -mfpmath=sse -funwind-tables -fexceptions -frtti" FF_EXTRA_CFLAGS="-O3 -DANDROID -Dipv6mr_interface=ipv6mr_ifindex -fasm -Wno-psabi -fno-short-enums -fno-strict-aliasing -fomit-frame-pointer -march=k8 " FF_CFLAGS="-O3 -Wall -pipe -ffast-math -fstrict-aliasing -Werror=strict-aliasing -Wno-psabi -Wa,--noexecstack -DANDROID " ;; x86_64|x64) TOOLCHAIN_BASE="x86_64" TOOLNAME_BASE="x86_64-linux-android" AOSP_ABI="x86_64" AOSP_ARCH="arch-x86_64" HOST="x86_64-linux" AOSP_FLAGS="-march=x86-64 -msse4.2 -mpopcnt -mtune=intel -funwind-tables -fexceptions -frtti" FF_EXTRA_CFLAGS="-O3 -DANDROID -Dipv6mr_interface=ipv6mr_ifindex -fasm -Wno-psabi -fno-short-enums -fno-strict-aliasing -fomit-frame-pointer -march=k8 " FF_CFLAGS="-O3 -Wall -pipe -ffast-math -fstrict-aliasing -Werror=strict-aliasing -Wno-psabi -Wa,--noexecstack -DANDROID " ;; *) echo "ERROR: Unknown architecture $1" [ "$0" = "$BASH_SOURCE" ] && exit 1 || return 1 ;; esac echo "TOOLCHAIN_BASE="$TOOLCHAIN_BASE echo "TOOLNAME_BASE="$TOOLNAME_BASE echo "AOSP_ABI="$AOSP_ABI echo "AOSP_ARCH="$AOSP_ARCH echo "AOSP_FLAGS="$AOSP_FLAGS echo "HOST="$HOST

編寫build_ffmpeg.sh檔案

#!/bin/bash

ARCH=$1

source config.sh $ARCH
LIBS_DIR=$(cd `dirname $0`; pwd)/libs/lib-ffmpeg
echo "LIBS_DIR="$LIBS_DIR

cd ffmpeg-3.3.8

PLATFORM=$ANDROID_NDK_ROOT/platforms/$AOSP_API/$AOSP_ARCH
TOOLCHAIN=$ANDROID_NDK_ROOT/toolchains/$TOOLCHAIN_BASE-$AOSP_TOOLCHAIN_SUFFIX/prebuilt/linux-x86_64

PREFIX=$LIBS_DIR/$AOSP_ABI

./configure \
--prefix=$PREFIX \
--enable-cross-compile \
--disable-runtime-cpudetect \
--disable-asm \
--arch=$AOSP_ABI \
--target-os=android \
--cc=$TOOLCHAIN/bin/$TOOLNAME_BASE-gcc \
--cross-prefix=$TOOLCHAIN/bin/$TOOLNAME_BASE- \
--disable-stripping \
--nm=$TOOLCHAIN/bin/$TOOLNAME_BASE-nm \
--sysroot=$PLATFORM \
--enable-gpl \
--enable-shared \
--disable-static \
--enable-small \
--disable-outdevs \
--disable-ffprobe \
--disable-ffplay \
--disable-ffmpeg \
--disable-ffserver \
--disable-debug \
--disable-ffprobe \
--disable-ffplay \
--disable-ffmpeg \
--disable-postproc \
--disable-avdevice \
--disable-symver \
--disable-stripping \
--extra-cflags="$FF_EXTRA_CFLAGS  $FF_CFLAGS" \
--extra-ldflags="  "

make clean
make -j8
make install

cd ..

檔案目錄結構如下:

——ffmpeg-3.3.8
——config.sh
——build_ffmpeg.sh
——build_ffmpeg_all.sh

編譯指定架構的版本可以通過如下命令:

bash build_ffmpeg.sh armeabi-v7a
bash build_ffmpeg.sh x86_64
......

編譯全平臺支援的版本命令:

bash build_ffmpeg_all.sh

其中build_ffmpeg_all.sh指令碼內容如下:

for arch in armeabi armeabi-v7a arm64-v8a x86 x86_64
do
    bash build_ffmpeg.sh $arch
done

輸出結果:

開始編譯x264庫

先獲取最新的x264庫

git clone http://git.videolan.org/git/x264.git

指令碼檔案目錄結構如下:

——x264
——build_x264.sh
——build_all_x264.sh

開始編譯:

#編譯指定平臺
bash build_x264.sh armeabi-v7a
#編譯全平臺
bash build_all_x264.sh

稍等片刻即可生成相關的檔案

開始編譯fdk-aac庫

先下載 fdk-aac-0.1.6版本

目錄結構如下:

——fdk-aac-0.1.6
——build_fdkaac.sh
——build_all_fdkaac.sh

開始編譯:

#編譯指定平臺
bash build_fdkaac.sh armeabi-v7a
#編譯全平臺
bash build_all_fdkaac.sh

輸出如下:

ffmpeg混合編譯x264和fdk-aar庫

目錄結構如下:

——fdk-aac-0.1.6
——x264
——ffmpeg-3.3.8
——libs
——build_ffmpeg_with_x264_fdkaac.sh
——build_ffmpeg_with_x264_fdkaac_all.sh

開始編譯:

#編譯指定平臺
bash build_ffmpeg_with_x264_fdkaac.sh armeabi-v7a
#編譯全平臺
bash build_ffmpeg_with_x264_fdkaac_all.sh

混合輸出結果:

全部指令碼地址:
https://github.com/byhook/ffmpeg4android

參考:
https://blog.csdn.net/leixiaohua1020/article/details/47008825
https://blog.csdn.net/panda1234lee/article/details/53099203
https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu