1. 程式人生 > >android全平臺編譯ffmpeg合併為單個庫實踐

android全平臺編譯ffmpeg合併為單個庫實踐

目錄

編譯環境

作業系統:ubuntu 16.05

android-ndk-r10e

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

ffmpeg-3.3.8版本

配置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

注意: 需要將ANDROID_NDK_ROOT替換成你自己的路徑,筆者這裡使用的是android-ndk-r10e版本,高版本比如android-ndk-r16b會報錯

配置合併指令碼build_ffmpeg_merge.sh

#!/bin/bash

ARCH=$1

source config.sh $ARCH
LIBS_DIR=$(cd `dirname $0`; pwd)/libs/ffmpeg-merge
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-nonfree \
--disable-shared \
--enable-static \
--enable-version3 \
--enable-pthreads \
--enable-small \
--disable-vda \
--disable-iconv \
--disable-encoders \
--enable-neon \
--enable-yasm \
--enable-encoder=mpeg4 \
--enable-encoder=mjpeg \
--enable-encoder=png \
--enable-nonfree \
--enable-muxers \
--enable-muxer=mov \
--enable-muxer=mp4 \
--enable-muxer=h264 \
--enable-muxer=avi \
--enable-decoder=h264 \
--enable-decoder=mpeg4 \
--enable-decoder=mjpeg \
--enable-decoder=png \
--enable-demuxer=image2 \
--enable-demuxer=h264 \
--enable-demuxer=avi \
--enable-demuxer=mpc \
--enable-demuxer=mpegts \
--enable-demuxer=mov \
--enable-parser=ac3 \
--enable-parser=h264 \
--enable-protocols \
--enable-zlib \
--enable-avfilter \
--enable-avresample \
--enable-postproc \
--enable-avdevice \
--disable-outdevs \
--disable-ffprobe \
--disable-ffplay \
--disable-ffmpeg \
--disable-ffserver \
--disable-debug \
--disable-symver \
--disable-stripping \
--extra-cflags="$FF_EXTRA_CFLAGS  $FF_CFLAGS" \
--extra-ldflags=""

make clean
make -j8
make install

#合併
$TOOLCHAIN/bin/$TOOLNAME_BASE-ld -rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -L$PREFIX/lib -soname libffmpeg.so -shared -nostdlib -Bsymbolic --whole-archive --no-undefined -o $PREFIX/libffmpeg.so \
    libavcodec/libavcodec.a \
    libavfilter/libavfilter.a \
    libavresample/libavresample.a \
    libswresample/libswresample.a \
    libavformat/libavformat.a \
    libavutil/libavutil.a \
    libswscale/libswscale.a \
    libpostproc/libpostproc.a \
    libavdevice/libavdevice.a \
    -lc -lm -lz -ldl -llog --dynamic-linker=/system/bin/linker $TOOLCHAIN/lib/gcc/$TOOLNAME_BASE/4.9/libgcc.a

cd ..

開始合併

#合併單個
bash build_ffmpeg_merge.sh armeabi-v7a
#合併全平臺
bash build_ffmpeg_merge_all.sh

其中build_ffmpeg_merge_all.sh內容如下

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

目錄結構如下

全部指令碼地址:

https://github.com/byhook/ffmpeg4android

參考:
https://blog.csdn.net/leixiaohua1020/article/details/47011021