首先須要去FFmpeg的官網http://www.ffmpeg.org/去下載FFmpeg的源碼,目前的版本號爲FFmpeg3.3(Hilbert)。java
下載的文件爲壓縮包,解壓後獲得ffmpeg-3.3目錄。linux
修改ffmpeg-3.3的configure文件:android
# 原來的配置內容:
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)'
緣由:若是不修改配置,直接進行編譯出來的so文件相似libavcodec.so.55.39.101,文件的版本號位於so以後,這樣在Android上沒法加載,因此須要修改!bash
編寫build_android.sh腳本文件:ide
在編譯FFmpeg以前須要進行配置,設置相應的環境變量等。全部的配置選項都在ffmpeg-3.3/configure這個腳本文件中,執行以下命令可查看全部的配置選項:函數
$ ./configure –help測試
下面將配置項和環境變量設置寫成一個sh腳本文件來運行以便編譯出Android平臺須要的so文件出來。ui
build_android.sh的內容以下:idea
#!/bin/bash NDK=/Users/renhui/framework/android-ndk-r14b SYSROOT=$NDK/platforms/android-9/arch-arm/ TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64 function build_one { ./configure \ --prefix=$PREFIX \ --enable-shared \ --disable-static \ --disable-doc \--enable-cross-compile \ --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \ --target-os=linux \ --arch=arm \ --sysroot=$SYSROOT \ --extra-cflags="-Os -fpic $ADDI_CFLAGS" \ --extra-ldflags="$ADDI_LDFLAGS" \ $ADDITIONAL_CONFIGURE_FLAG } CPU=arm PREFIX=$(pwd)/android/$CPU ADDI_CFLAGS="-marm" build_one
須要肯定的是NDK,SYSROOT和TOOLCHAIN是不是本地的環境,並肯定cross-prefix指向的路徑存在。spa
保存腳本文件後,將腳本的權限提高:
chmod 777 build_android.sh
而後執行腳本,該腳本會完成對ffmpeg的配置,並生成config.h等配置文件,後面的編譯會用到。若是未通過配置直接進行編譯會提示沒法找到config.h文件等錯誤。
而後執行下面兩個命令:
$make $make install
至此,會在ffmpeg-3.3目錄下生成一個android目錄,其/android/arm/lib目錄下的so庫文件就是可以在Android上運行的so庫。
建立Demo工程,測試上面生成的so文件可否正常使用:
package cn.renhui; public class FFmpegNative { static { System.loadLibrary("avutil-55"); System.loadLibrary("avcodec-57"); System.loadLibrary("swresample-2"); System.loadLibrary("avformat-57"); System.loadLibrary("swscale-4"); System.loadLibrary("avfilter-6"); System.loadLibrary("avdevice-57"); System.loadLibrary("ffmpeg_codec"); } public native int avcodec_find_decoder(int codecID); }
#include "cn_renhui_FFmpegNative.h" #ifdef __cplusplus extern "C" { #endif
JNIEXPORT jint JNICALL Java_cn_renhui_FFmpegNative_avcodec_1find_1decoder (JNIEnv *env, jobject obj, jint codecID) { AVCodec *codec = NULL; /* register all formats and codecs */ av_register_all(); codec = avcodec_find_decoder(codecID); if (codec != NULL) { return 0; } else { return -1; } } #ifdef __cplusplus } #endif
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := avcodec-57-prebuilt LOCAL_SRC_FILES := prebuilt/libavcodec-57.so include $(PREBUILT_SHARED_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := avdevice-57-prebuilt LOCAL_SRC_FILES := prebuilt/libavdevice-57.so include $(PREBUILT_SHARED_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := avfilter-6-prebuilt LOCAL_SRC_FILES := prebuilt/libavfilter-6.so include $(PREBUILT_SHARED_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := avformat-57-prebuilt LOCAL_SRC_FILES := prebuilt/libavformat-57.so include $(PREBUILT_SHARED_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := avutil-55-prebuilt LOCAL_SRC_FILES := prebuilt/libavutil-55.so include $(PREBUILT_SHARED_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := avswresample-2-prebuilt LOCAL_SRC_FILES := prebuilt/libswresample-2.so include $(PREBUILT_SHARED_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := swscale-4-prebuilt LOCAL_SRC_FILES := prebuilt/libswscale-4.so include $(PREBUILT_SHARED_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := ffmpeg_codec LOCAL_SRC_FILES := cn_dennishucd_FFmpegNative.c LOCAL_LDLIBS := -llog -ljnigraphics -lz -landroid LOCAL_SHARED_LIBRARIES := avcodec-57-prebuilt avdevice-57-prebuilt avfilter-6-prebuilt avformat-57-prebuilt avutil-55-prebuilt include $(BUILD_SHARED_LIBRARY)
FFmpegNative ffmpeg = new FFmpegNative(); int codecID = 28; int res = ffmpeg.avcodec_find_decoder(codecID); if (res == 0) { tv.setText("Success!"); } else { tv.setText("Failed!"); }
28是H264的編解碼ID,能夠在ffmpeg的源代碼中找到,它是枚舉類型定義的。在C語言中,能夠換算爲整型值。這裏測試可否找到H264編解碼,若是能找到,說明調用ffmpeg的庫函數是成功的,這也代表咱們編譯的so文件是基本可用。
2018年8月20號補充說明:目前最新版本的FFmpeg版本號爲4.0.1 ,上面的方法能繼續正常使用