準備環(huán)節(jié):
需要的環(huán)境和文件準備好了之后,就可以開始準備編譯咯
為了讓編譯得到的庫能適合Android宫补,需要修改配置文件ffmpeg/configure
#SLIBNAME_WITH_MAJOR='$(SLIBNAME).$(LIBMAJOR)'
SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF)'
#LIB_INSTALL_EXTRA_CMD='$$(RANLIB) "$(LIBDIR)/$(LIBNAME)"'
LIB_INSTALL_EXTRA_CMD='$$(RANLIB)"$(LIBDIR)/$(LIBNAME)"'
#SLIB_INSTALL_NAME='$(SLIBNAME_WITH_VERSION)'
SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)'
#SLIB_INSTALL_LINKS='$(SLIBNAME_WITH_MAJOR) $(SLIBNAME)'
SLIB_INSTALL_LINKS='$(SLIBNAME)'
- 在jni目錄下面創(chuàng)建一個文件
build_ffmpeg_with_x264.sh
檬姥,用于配置環(huán)境變量和控制編譯主流程; - 在x264目錄下面創(chuàng)建一個文件
build_x264_for_android.sh
粉怕,用于編譯x264健民; - 在ffmpeg目錄下面創(chuàng)建一個文件
build_ffmpeg_for_android.sh
,用于編譯ffmpeg贫贝;
這樣就可以編譯出ffmpeg的庫秉犹,如果在編譯時提醒需要什么工具,直接安裝完之后再重新執(zhí)行腳本稚晚。
上面說到三個文件現(xiàn)在來逐一解析一下崇堵。
首先,看看build_ffmpeg_with_x264.sh
客燕,從文件名可以看出鸳劳,該腳本就是把ffmpeg和x264混合編譯。
#!/bin/bash
#配置NDK路徑
export NDK=/opt/Sdk/ndk-bundle
# 檢測NDK
if [[ -z "$NDK" ]]; then
echo "The NDK dir is empty, If the shell can not run normally, you should set the NDK variable to your local ndk.dir"
exit 1
fi
# 檢測系統(tǒng)
OS=`uname`
HOST_ARCH=`uname -m`
export CCACHE=; type ccache >/dev/null 2>&1 && export CCACHE=ccache
if [ $OS == 'Linux' ]; then
export HOST_SYSTEM=linux-$HOST_ARCH
elif [ $OS == 'Darwin' ]; then
export HOST_SYSTEM=darwin-$HOST_ARCH
fi
# 配置 SYSROOT 和 CROSS_PREFIX也搓,基于android-15
SYSROOT=$NDK/platforms/android-15/arch-arm
CROSS_PREFIX=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/$HOST_SYSTEM/bin/arm-linux-androideabi-
echo "CROSS_PREFIX=$CROSS_PREFIX"
ROOT_DIR=`pwd`/
COMMOND_DIR=command_source
echo $ROOT_DIR
#配置輸出路徑
PREFIX=$ROOT_DIR/build
if [[ ! -d "$PREFIX" ]]; then
mkdir $PREFIX
fi
#### 編譯步驟 開始 ####
#1赏廓、編譯x264
#2、編譯ffmpeg
#3傍妒、ndk-build
#### 編譯步驟 結(jié)束 ####
#預(yù)先編譯x264楚昭,編譯類型為靜態(tài)庫#
X264_SOURCE_DIR=x264
if [[ ! -d "$X264_SOURCE_DIR" ]]; then
git clone http://git.videolan.org/git/x264.git $X264_SOURCE_DIR
fi
cp $ROOT_DIR$COMMOND_DIR/build_x264_for_android.sh $X264_SOURCE_DIR/build_x264_for_android.sh
TARGET_X264_DIR=$ROOT_DIR$X264_SOURCE_DIR
source $X264_SOURCE_DIR/build_x264_for_android.sh
#編譯ffmpeg,編譯為多個靜態(tài)庫#
FFMPEG_SOURCE_DIR=ffmpeg-3.3
if [[ ! -d "$FFMPEG_SOURCE_DIR" ]]; then
git clone git://source.ffmpeg.org/ffmpeg.git $FFMPEG_SOURCE_DIR
fi
cp $ROOT_DIR$COMMOND_DIR/build_for_android.sh $FFMPEG_SOURCE_DIR/build_for_android.sh
TARGET_FFMPEG_DIR=$ROOT_DIR$FFMPEG_SOURCE_DIR
source $FFMPEG_SOURCE_DIR/build_ffmpeg_for_android.sh
#執(zhí)行ndk-build
$NDK/ndk-build
build_x264_for_android.sh
文件內(nèi)容如下:
#!/bin/bash
echo "###### 開始編譯 x264 ######"
SOURCE=$TARGET_X264_DIR
echo $SOURCE
cd $SOURCE
EXTRA_CFLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=neon -D__ARM_ARCH_7__ -D__ARM_ARCH_7A__"
EXTRA_LDFLAGS="-nostdlib"
./configure \
配置輸出路徑
--prefix=$PREFIX \
配置交叉編譯路徑前綴
--cross-prefix=$CROSS_PREFIX \
--extra-cflags="$EXTRA_CFLAGS" \
--extra-ldflags="$EXTRA_LDFLAGS" \
--enable-static \
--enable-pic \
--enable-strip \
--disable-cli \
--host=arm-linux \
--sysroot=$SYSROOT
make clean
make && make install
echo "###### x264編譯完成 ######"
cd ../
運行前需要把上面的兩行中文注釋刪除拍顷,因為configure中間不能有注釋,所以我就直接沒有加#來注釋塘幅,便于發(fā)現(xiàn)問題昔案。
build_ffmpeg_for_android.sh
文件內(nèi)容如下:
#!/bin/bash
echo "###### 開始編譯 ffmpeg ######"
SOURCE=$TARGET_FFMPEG_DIR
echo $SOURCE
cd $SOURCE
ADD_H264_FEATURE="--enable-encoder=aac \
--enable-decoder=aac \
--enable-gpl \
--enable-encoder=libx264 \
--enable-libx264 \
--extra-cflags=-I$PREFIX/include \
--extra-ldflags=-L$PREFIX/lib "
function build_one_so
{
./configure \
--prefix=$PREFIX \
--disable-shared \
--enable-pthreads \
--enable-gpl \
--enable-version3 \
--enable-nonfree \
--enable-static \
--enable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--disable-doc \
--disable-symver \
--enable-avdevice \
--enable-avfilter \
--enable-libx264 \
--enable-small \
--enable-asm \
--enable-neon \
--cross-prefix=$CROSS_PREFIX \
--target-os=linux \
--arch=arm \
--enable-cross-compile \
--enable-runtime-cpudetect \
--sysroot=$SYSROOT \
--disable-parsers \
--enable-parser=aac \
--enable-parser=h264 \
--enable-parser=mjpeg \
--enable-parser=png \
--enable-parser=bmp \
--enable-parser=mpegvideo \
--enable-parser=mpegaudio \
--disable-encoders \
--enable-encoder=h263 \
--enable-encoder=libx264 \
--enable-encoder=mpeg4 \
--enable-encoder=aac \
--enable-encoder=png \
--enable-encoder=gif \
--enable-encoder=bmp \
--disable-decoders \
--enable-decoder=h263 \
--enable-decoder=h264 \
--enable-decoder=mpeg4 \
--enable-decoder=mjpeg \
--enable-decoder=gif \
--enable-decoder=mp3 \
--enable-decoder=aac \
--enable-decoder=png \
--enable-decoder=bmp \
--enable-decoder=yuv4 \
--disable-network \
--enable-protocols \
--enable-protocol=concat \
--enable-protocol=hls \
--enable-protocol=file \
--disable-demuxers \
--enable-demuxer=h263 \
--enable-demuxer=h264 \
--enable-demuxer=flv \
--enable-demuxer=gif \
--enable-demuxer=aac \
--enable-demuxer=ogg \
--enable-demuxer=dts \
--enable-demuxer=mp3 \
--enable-demuxer=mov \
--enable-demuxer=m4v \
--enable-demuxer=concat \
--enable-demuxer=mpegts \
--enable-demuxer=mjpeg \
--enable-demuxer=mpegvideo \
--enable-demuxer=rawvideo \
--enable-demuxer=yuv4mpegpipe \
--disable-muxers \
--enable-muxer=h264 \
--enable-muxer=flv \
--enable-muxer=gif \
--enable-muxer=mp3 \
--enable-muxer=dts \
--enable-muxer=mp4 \
--enable-muxer=mov \
--enable-muxer=mpegts \
--disable-filters \
--enable-filter=aresample \
--enable-filter=asetpts \
--enable-filter=setpts \
--enable-filter=ass \
--enable-filter=scale \
--enable-filter=concat \
--enable-filter=atempo \
--enable-filter=movie \
--enable-filter=overlay \
--enable-filter=rotate \
--enable-filter=transpose \
--enable-filter=hflip \
--extra-cflags="-mfloat-abi=softfp -mfpu=neon -marm -march=armv7-a" \
$ADD_H264_FEATURE
make clean
make -j4
make install
}
build_one_so
echo "###### ffmpeg編譯完成 ######"
cd ../
這個腳本稍微多一點尿贫,主要是對ffmpeg的定制,由于ffmpeg全部功能打開的話踏揣,最終生成的庫會很大庆亡,通常我們需要根據(jù)需求,對ffmpeg進行定制裁剪捞稿,根據(jù)encoder又谋,decoder,muxer娱局,demuxer彰亥,parser,filter衰齐,protocol等七個方面進行任斋。首先都會全部disable,然后根據(jù)需求再enable需要的格式和功能耻涛。
上面是編譯ffmpeg需要的配置废酷,為了編譯出Android可用的so文件,還需要配置Android.mk
文件
LOCAL_PATH := $(call my-dir)
#include $(call all-subdir-makefiles)
#static version of libavcodec
include $(CLEAR_VARS)
LOCAL_MODULE:= libavcodec_static
LOCAL_SRC_FILES:= ./build/lib/libavcodec.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
#static version of libavformat
include $(CLEAR_VARS)
LOCAL_MODULE:= libavformat_static
LOCAL_SRC_FILES:= ./build/lib/libavformat.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
#static version of libswscale
include $(CLEAR_VARS)
LOCAL_MODULE:= libswscale_static
LOCAL_SRC_FILES:= ./build/lib/libswscale.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
#static version of libavutil
include $(CLEAR_VARS)
LOCAL_MODULE:= libavutil_static
LOCAL_SRC_FILES:= ./build/lib/libavutil.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
#static version of libavdevice
include $(CLEAR_VARS)
LOCAL_MODULE:= libavdevice_static
LOCAL_SRC_FILES:= ./build/lib/libavdevice.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
#static version of libavfilter
include $(CLEAR_VARS)
LOCAL_MODULE:= libavfilter_static
LOCAL_SRC_FILES:= ./build/lib/libavfilter.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
#static version of libswresample
include $(CLEAR_VARS)
LOCAL_MODULE:= libswresample_static
LOCAL_SRC_FILES:= ./build/lib/libswresample.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
#static version of libpostproc
include $(CLEAR_VARS)
LOCAL_MODULE:= libpostproc_static
LOCAL_SRC_FILES:= ./build/lib/libpostproc.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
#static version of libx264
include $(CLEAR_VARS)
LOCAL_MODULE:= libx264_static
LOCAL_SRC_FILES:= ./build/lib/libx264.a
LOCAL_CFLAGS := -march=armv7-a -mfloat-abi=softfp -mfpu=neon -O3 -ffast-math -funroll-loops
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := ffmpeg
LOCAL_C_INCLUDES := ./build/include \
LOCAL_SRC_FILES := decoder.c encoder.c
LOCAL_LDLIBS := -llog -lz
LOCAL_CFLAGS := -march=armv7-a -mfloat-abi=softfp -mfpu=neon -O3 -ffast-math -funroll-loops
LOCAL_WHOLE_STATIC_LIBRARIES := libavformat_static \
libavcodec_static \
libavutil_static \
libpostproc_static \
libswscale_static \
libswresample_static \
libx264_static \
libavfilter_static \
libavdevice_static \
include $(BUILD_SHARED_LIBRARY)
還需要一個Application.mk
文件
APP_ABI := armeabi-v7a
APP_PLATFORM := android-15
# system - 使用默認最小的C++運行庫抹缕,這樣生成的應(yīng)用體積小澈蟆,內(nèi)存占用小,但部分功能將無法支持
# stlport_static - 使用STLport作為靜態(tài)庫卓研,這項是Android開發(fā)網(wǎng)極力推薦的
# stlport_shared - STLport 作為動態(tài)庫趴俘,這個可能產(chǎn)生兼容性和部分低版本的Android固件,目前不推薦使用鉴分。
# gnustl_static - 使用 GNU libstdc++ 作為靜態(tài)庫
# 使用STLport作為靜態(tài)庫哮幢,這項是Android開發(fā)網(wǎng)極力推薦的
#APP_STL := stlport_static
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -std=gnu++11
APP_OPTIM := release
這樣配置完成后的目錄結(jié)構(gòu)如下:
CodeBlog是我做的一個編程技術(shù)學(xué)習(xí)客戶端,集成了很多技術(shù)網(wǎng)站上的博客志珍,應(yīng)用寶詳情頁