準(zhǔn)備
1.下載最新的ndk,ffmpeg包,android studio
一.因?yàn)閒fmpeg是c,c++的東西所以通過ndk編譯成os文件供開發(fā)使用
1-> 解壓ndk到桌面路徑: ? ? /Users/用戶名/Desktop/android-ndk-r12b(后面idk路徑用”&NDK_PATH"表示)
2->將ffmpeg包解壓到: ? ? &NDK_PATH/sources/ffmpeg-3.1.1
3.->修改ffmpeg-3.1.1目錄下的configure文件,目的是將生成的庫類文件名字libavcodec.so.56的56版本號去掉(可用文本編輯器打開修改)
找到
修改成
4->編譯FFmpeg
·在ffmpeg-3.1.1創(chuàng)建文件build_android.sh
·添加內(nèi)容
#!/bin/bash
NDK=&NDK_PATH(NDK路徑)
SYSROOT=$NDK/platforms/android-21/arch-arm/(刪:指定NDK所用的編譯平臺)
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64(刪:指定編譯工具集的位置)
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
}
CPU=arm
PREFIX=$(pwd)/android/$CPU
ADDI_CFLAGS="-marm"
build_one
·保存后在終端執(zhí)行
cd /…/…/ffmpeg3.1.3(idk中ffmpeg的文件夾路徑)
sudo chmod +x build_android.sh
./build_android.sh
編譯需要等待上一段時間,編譯成功后會出現(xiàn)下面這樣一個目錄
其中l(wèi)ibavcodec.so赴魁、libavfilter.so赋访、libavformat.so够吩、libavutil.so终畅、libswresample.so坷备、libswscale.so都是軟鏈虹蒋,沒有用蹋凝,可以刪掉
·然后在arm文件夾中創(chuàng)建一個Android.mk文件內(nèi)容如下
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:= libavcodec
LOCAL_SRC_FILES:= lib/libavcodec-57.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= libavformat
LOCAL_SRC_FILES:= lib/libavformat-57.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
# FFmpeg library
include $(CLEAR_VARS)
LOCAL_MODULE:= libswscale
LOCAL_SRC_FILES:= lib/libswscale-4.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= libavutil
LOCAL_SRC_FILES:= lib/libavutil-54.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= libavfilter
LOCAL_SRC_FILES:= lib/libavfilter-6.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= libswresample
LOCAL_SRC_FILES:= lib/libswresample-2.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
# Program
include $(CLEAR_VARS)
LOCAL_MODULE := herobayboomerangLive
LOCAL_SRC_FILES :=com.herobay.liangchao.herobayboomerang.MainActivity.c
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include? ? ? ? #頭文件搜索路徑
LOCAL_LDLIBS :=-llog -lz? ? ? ? ? ? ? ? ? ? ? ? ?#附加的鏈接器選項(xiàng)
LOCAL_SHARED_LIBRARIES := avcodec avdevice avfilter avformat avutil postproc swresample swscale
include $(BUILD_SHARED_LIBRARY)
要注意其中.so前面的數(shù)字應(yīng)該改成你的ffmpeg版本編譯出來的數(shù)字
到這里就完成了ffmpeg庫文件的編譯可以再android項(xiàng)目中使用了
二.在android studio中進(jìn)行使用
1.將下面的內(nèi)容添加到項(xiàng)目中的jni文件夾中
2.在local.properties文件配置ndk路徑
ndk.dir=&NDK_PATH 如:
3.配置app的build.gradle文件
·添加這一段禁止自動ndk-build
sourceSets.main.jni.srcDirs?= []
·添加這一段讓它知道調(diào)用哪個庫
ndk {
abiFilter "armeabi"
moduleName "ovsplayer"
ldLibs "log", "z", "m", "jnigraphics", "android"
}
如:
4.在grade.properties文件中添加:android.useDeprecatedNdk=true
5.進(jìn)入Project中的src/main/jni文件夾,修改其中的Application.mk文件,添加:APP_PLATFORM := android-21(因?yàn)樵揺xample采用-ljnigraphics默認(rèn)庫,這個庫在android-3中沒有,如果不聲明的話,idk會默認(rèn)采用android-3進(jìn)行build)
接下來就可以開始FFmpeg的使用了