1锤窑、編譯環(huán)境
windows 7 栓霜、 ndk-r14b淤年、MinGW瘟栖、ffmpeg-3.3.7
????? 編譯的時(shí)候最好用我這個(gè)版本的ffmpeg-3.3.7和ndk-r14b 葵擎,之前編譯時(shí)有遇到各種問題,其中一種就是ffmpeg-3.3.7和ndk不匹配什么的半哟。怎么弄都弄不好酬滤,最后換他們的版本才解決的。如果大家想用最新的寓涨,也可以盯串,不過遇到問題時(shí),可以嘗試換ffmpeg或ndk來解決戒良。MinGW大家自行下載安裝即可体捏,網(wǎng)上有很多教程,這里不再贅述了。
2几缭、修改ffmpeg 配置
??? 解壓下載好的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)'
3奏司、精簡(jiǎn)腳本編寫
配置修改完之后乔询,就要寫一個(gè)編譯腳本了,在ffmpeg-3.3.7目錄下韵洋,新建一個(gè)ffmpeg_android_build.sh文件。
編譯腳本決定了最終編譯出來的so的大小黄锤,可以將你們需要的功能disable掉搪缨,我這個(gè)腳本最終編譯出來的so 有5.4M。編譯出來的ffmpeg 文件夾中會(huì)有一個(gè)config.log 文件鸵熟,里面也記錄了編譯后支持的功能副编。可以把不需要的再寫入腳本流强,進(jìn)一步精簡(jiǎn)痹届。config.log 文件截圖如下:
4、使用MinGW編譯so庫
??? 安裝好MinGW 后打月,進(jìn)入MinGW 文件夾队腐,找到msys.bat ,運(yùn)行。進(jìn)入ffmpeg-3.3.7目錄
在編譯腳本所在的目錄下奏篙,也就是ffmpeg-3.3.7目錄柴淘,執(zhí)行如下命令:
一般6、7分鐘就能編譯完成秘通。在fmpeg-3.3.7目錄會(huì)生成一個(gè)Android文件夾为严。里面就包含我們需要的so,這個(gè)腳本編譯出來的差不多5.4M,還算比較精簡(jiǎn)的了肺稀。如何應(yīng)用在android 下呢第股。下面我會(huì)講到 cmake編譯FFmpeg。
5.創(chuàng)建NDK項(xiàng)目并引入ffmpeg動(dòng)態(tài)庫
新建這兩個(gè)文件夾话原,把剛編譯出來的so 庫復(fù)制到j(luò)niLibs下夕吻。include 可以不復(fù)制。
配置build.gradle
創(chuàng)建java 類FFmpegCmd
編寫CMakeLists.txt文件
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library(# Sets the name of the library.
? ? ? ? ? ? ffmpegrun
? ? ? ? ? ? # Sets the library as a shared library.
? ? ? ? ? ? SHARED
? ? ? ? ? ? # Provides a relative path to your source file(s).
? ? ? ? ? ? ? src/main/cpp/cmdutils.c
src/main/cpp/ffmpeg.c
src/main/cpp/ffmpeg_filter.c
src/main/cpp/ffmpeg_opt.c
src/main/cpp/ffmpeg_cmd_run.c
? ? ? ? ? ? )
add_library(
avcodec
SHARED
IMPORTED
? ? ? ? ? ? )
add_library(
avdevice
SHARED
IMPORTED
? ? ? ? ? ? )
add_library(
avfilter
SHARED
IMPORTED
? ? ? ? ? ? )
add_library(
avformat
SHARED
IMPORTED
? ? ? ? ? ? )
add_library(
avutil
SHARED
IMPORTED
? ? ? ? ? ? )
add_library(
swresample
SHARED
IMPORTED
? ? ? ? ? ? )
add_library(
swscale
SHARED
IMPORTED
? ? ? ? ? ? )
if(${ANDROID_ABI} STREQUAL "armeabi-v7a")
set_target_properties(
avcodec
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libavcodec.so
? ? )
set_target_properties(
avdevice
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libavdevice.so
? ? )
set_target_properties(
avfilter
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libavfilter.so
? ? ? ? )
set_target_properties(
avformat
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libavformat.so
? ? ? ? ? ? )
set_target_properties(
avutil
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libavutil.so
? ? ? ? ? ? )
set_target_properties(
swresample
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libswresample.so
? ? ? ? ? ? )
set_target_properties(
swscale
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libswscale.so
? ? ? ? ? ? )
endif(${ANDROID_ABI} STREQUAL "armeabi-v7a")
include_directories(
D:/ffmpeg/ffmpeg-3.3.7/
)
add_definitions("-D__ARM_NEON__=1")
set_property(SOURCE ${SRC_FILE}? APPEND_STRING PROPERTY COMPILE_FLAGS " -mfpu=neon")
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library(# Sets the name of the path variable.
? ? ? ? ? ? ? log-lib
? ? ? ? ? ? ? # Specifies the name of the NDK library that
# you want CMake to locate.
? ? ? ? ? ? ? log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries(# Specifies the target library.
? ? ? ? ? ? ? ? ? ? ? ffmpegrun
avcodec
avdevice
avfilter
avformat
avutil
swresample
swscale
? ? ? ? ? ? ? ? ? ? ? # Links the target library to the log library
# included in the NDK.
? ? ? ? ? ? ? ? ? ? ? ${log-lib} )
注意CMakeLists.txt文件中有這樣一行稿静,要換成你的ffmpeg 的路徑梭冠。
然后我們需要將ffmpeg-3.3.7目錄下的一下幾個(gè)文件復(fù)制到cpp下,ffmpeg_cmd_run 這個(gè)c文件是后面需要我們寫的改备,這里暫時(shí)不管他控漠。
找到cmdutils.c中的exit_program函數(shù),把exit_program函數(shù)修改成:
cmdutils.h中找到exit_program修改為:
在ffmpeg.c末尾加上如下代碼:
創(chuàng)建ffmpeg_cmd_run.c文件
#include "ffmpeg.h"
#include "include/libavcodec/avcodec.h"
#include
JNIEXPORT jint JNICALL
Java_com_multimedia_processing_ffmpeg_ffmpeg_FFmpegCmd_ffmpegRun(JNIEnv *env, jclass clazz,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? jobjectArray commands){
int argc = (*env)->GetArrayLength(env,commands);
? ? char *argv[argc];
? ? int i;
? ? for (i =0; i < argc; i++) {
jstring js = (jstring) (*env)->GetObjectArrayElement(env,commands, i);
? ? ? ? argv[i] = (char *) (*env)->GetStringUTFChars(env,js, 0);
? ? }
return jxRun(argc,argv);
}
至此,所有步驟都已完成? 盐捷,接下來就是使用
demo 就不出傳了,需要的可以聯(lián)系我:QQ 603306391