1、编译环境
windows 7 、 ndk-r14b、MinGW、ffmpeg-3.3.7
编译的时候最好用我这个版本的ffmpeg-3.3.7和ndk-r14b ,之前编译时有遇到各种问题,其中一种就是ffmpeg-3.3.7和ndk不匹配什么的。怎么弄都弄不好,最后换他们的版本才解决的。如果大家想用最新的,也可以,不过遇到问题时,可以尝试换ffmpeg或ndk来解决。MinGW大家自行下载安装即可,网上有很多教程,这里不再赘述了。
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、精简脚本编写
配置修改完之后,就要写一个编译脚本了,在ffmpeg-3.3.7目录下,新建一个ffmpeg_android_build.sh文件。

编译脚本决定了最终编译出来的so的大小,可以将你们需要的功能disable掉,我这个脚本最终编译出来的so 有5.4M。编译出来的ffmpeg 文件夹中会有一个config.log 文件,里面也记录了编译后支持的功能。可以把不需要的再写入脚本,进一步精简。config.log 文件截图如下:

4、使用MinGW编译so库
安装好MinGW 后,进入MinGW 文件夹,找到msys.bat ,运行。进入ffmpeg-3.3.7目录

在编译脚本所在的目录下,也就是ffmpeg-3.3.7目录,执行如下命令:

一般6、7分钟就能编译完成。在fmpeg-3.3.7目录会生成一个Android文件夹。里面就包含我们需要的so,这个脚本编译出来的差不多5.4M,还算比较精简的了。如何应用在android 下呢。下面我会讲到 cmake编译FFmpeg。

5.创建NDK项目并引入ffmpeg动态库
新建这两个文件夹,把刚编译出来的so 库复制到jniLibs下。include 可以不复制。

配置build.gradle

创建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目录下的一下几个文件复制到cpp下,ffmpeg_cmd_run 这个c文件是后面需要我们写的,这里暂时不管他。

找到cmdutils.c中的exit_program函数,把exit_program函数修改成:

cmdutils.h中找到exit_program修改为:

在ffmpeg.c末尾加上如下代码:

创建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 就不出传了,需要的可以联系我:QQ 603306391
网友评论