1. 新建项目,导入.so文件
将之前编译生成的多个.so文件及inclulde文件夹下的头文件拷贝到/app/libs文件夹下,拷贝后目录结果如下:
2. 配置/app/src/main目录下新建jni文件夹
3. 创建FFmpegCmd.java类
package com.mazaiting.ffmpegcmdtest;
/**
* FFmpeg命令行操作
* @author mazaiting
* @date 2018/1/12
*/
public class FFmpegCmd {
static {
System.loadLibrary("avcodec-57");
System.loadLibrary("avdevice-57");
System.loadLibrary("avfilter-6");
System.loadLibrary("avformat-57");
System.loadLibrary("avutil-55");
System.loadLibrary("swresample-2");
System.loadLibrary("swscale-4");
System.loadLibrary("FfmpegCmd");
}
public static int execute(String[] commands) {
return run(commands);
}
public native static int run(String[] commands);
}
4. 配置/app/build.gradle文件
1). 在android节点下的defaultConfig目录下新增
ndk {
moduleName "ffmpegcmd"
abiFilters "armeabi"
}
2). 在android节点下新增,如果已有则替换这段
sourceSets {
main {
jniLibs.srcDirs = ['src/main/libs']
jni.srcDirs=[]
}
}
5. 生成jni的头文件
javah命令配置, 运行命令完成后,在jni文件夹下生成com_mazaiting_ffmpegcmdtest_FFmpegCmd.h文件
6. 配置jni目录
将ffmpeg源码中的ffmpeg.h, ffmpeg.c, ffmpeg_opt.c, ffmpeg_filter.c,cmdutils.c, cmdutils.h, cmdutils_common_opts.h,config.h 赋值到jni目录下,并在 jni 目录新建文件 Android.mk Application.mk com_mazaiting_ffmpegcmdtest_FFmpegCmd.c。
7. 修改ffmpeg源码
1). 编辑ffmpeg.c,把
int main(int argc, char **argv)
改名为
int run(int argc, char **argv)
编辑ffmpeg.h, 在文件末尾(在#endif前面)添加函数申明:
int run(int argc, char **argv)
2). 编辑cmdutils.c中的exit_program函数,删掉函数中原来的内容, 添加 return ret;并修改函数的返回类型为int。
长这样:
int exit_program(int ret)
{
return ret;
}
编辑cmdutils.h中exit_program的申明,把返回类型修改为int。
int exit_program(int ret);
8. 其他文件
1). com_mazaiting_ffmpegcmdtest_FFmpegCmd.c
#include "com_mazaiting_ffmpegcmdtest_FFmpegCmd.h"
#include "ffmpeg.h"
#include <android/log.h>
#define LOGI(FORMAT, ...) __android_log_print(ANDROID_LOG_INFO,"ffmpeg",FORMAT,##__VA_ARGS__);
#define LOGE(FORMAT, ...) __android_log_print(ANDROID_LOG_ERROR,"ffmpeg",FORMAT,##__VA_ARGS__);
JNIEXPORT jint JNICALL Java_com_mazaiting_ffmpegcmdtest_FFmpegCmd_run
(JNIEnv *env, jclass cls, jobjectArray commands) {
int argc = (*env)->GetArrayLength(env, commands);
char *argv[argc];
LOGE("Kit argc %d\n", 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);
LOGE("Kit argv %s\n", argv[i]);
}
return run(argc, argv);
}
2). Android.mk
LOCAL_PATH := $(call my-dir)
# FFmpeg library
include $(CLEAR_VARS)
# 打印当前路径
$(warning $(LOCAL_PATH))
# .so文件名称,去除前缀lib和-后的内容
LOCAL_MODULE := avcodec
# .so文件
LOCAL_SRC_FILES := libavcodec-57.so
# 已编译好的库使用PREBUILT_SHARED_LIBRARY
include $(PREBUILT_SHARED_LIBRARY)
# FFmpeg library
include $(CLEAR_VARS)
LOCAL_MODULE := avdevice
LOCAL_SRC_FILES := libavdevice-57.so
include $(PREBUILT_SHARED_LIBRARY)
# FFmpeg library
include $(CLEAR_VARS)
LOCAL_MODULE := avfilter
LOCAL_SRC_FILES := libavfilter-6.so
include $(PREBUILT_SHARED_LIBRARY)
# FFmpeg library
include $(CLEAR_VARS)
LOCAL_MODULE := avformat
LOCAL_SRC_FILES := libavformat-57.so
include $(PREBUILT_SHARED_LIBRARY)
# FFmpeg library
include $(CLEAR_VARS)
LOCAL_MODULE := avutil
LOCAL_SRC_FILES := libavutil-55.so
include $(PREBUILT_SHARED_LIBRARY)
# FFmpeg library
include $(CLEAR_VARS)
LOCAL_MODULE := swresample
LOCAL_SRC_FILES := libswresample-2.so
include $(PREBUILT_SHARED_LIBRARY)
# FFmpeg library
include $(CLEAR_VARS)
LOCAL_MODULE := swscale
LOCAL_SRC_FILES := libswscale-4.so
include $(PREBUILT_SHARED_LIBRARY)
# Program
# module名称
include $(CLEAR_VARS)
LOCAL_MODULE := FfmpegCmd
# module中的源文件
LOCAL_SRC_FILES := com_mazaiting_ffmpegcmdtest_FFmpegCmd.c ffmpeg.c ffmpeg_opt.c cmdutils.c ffmpeg_filter.c
# ffmpeg源码路径
LOCAL_C_INCLUDES += C:/Users/Administrator/Desktop/ffmpeg-3.3.6
LOCAL_LDLIBS := -llog -lz
# 链接的动态库文件
LOCAL_SHARED_LIBRARIES := avcodec avdevice avfilter avformat avutil swresample swscale
# 编译生成的库使用BUILD_SHARED_LIBRARY
include $(BUILD_SHARED_LIBRARY)
3). Application.mk
APP_MODULES := FfmpegCmd
APP_ABI := armeabi
APP_PLATFORM := android-14
# 去除原子性编译
APP_LDFLAGS := -latomic
9. 编译FfmpegCmd.so文件
编译之前先将/app/libs文件夹内的ffmpeg中拷贝的.so文件拷贝到jni文件夹一份,此时jni文件夹内的所有文件如图4,使用ndk-build命令编译(图5)。
10. 编译成功
编译成功后在main目录下生成libs和obj文件夹,将/libs/armeabi/下的libFfmpegcmd.so或者/obj/local/armeabi/objs/下的libFfmpegcmd.so文件复制到/app/libs/armeabi/目录下,此时在java文件中即可成功使用ffmpeg。
11. 使用
1). 在AndroidManifest.xml添加权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2). activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mazaiting.ffmpegcmdtest.MainActivity">
<Button
android:onClick="start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="合成"/>
</RelativeLayout>
3). MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private boolean isRun = false;
/**视频源路径*/
private String videoUrl = "/storage/emulated/0/input.mp4";
/**图片路径*/
private String imageUrl = "/storage/emulated/0/image.png";
/**视频输出路径*/
private String outputUrl = "/storage/emulated/0/output.mp4";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void start(View view){
if (!isRun) {
new Thread(mRunnable).start();
isRun = true;
} else {
Toast.makeText(this, "正在运行", Toast.LENGTH_SHORT).show();
}
}
/**
* 添加水印任务
*/
Runnable mRunnable = new Runnable() {
@Override
public void run() {
String[] commands = new String[10];
commands[0] = "ffmpeg";
commands[1] = "-i";
commands[2] = videoUrl;
commands[3] = "-i";
commands[4] = imageUrl;
commands[5] = "-filter_complex";
commands[6] = "overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2";
commands[7] = "-codec:a";
commands[8] = "copy";
commands[9] = outputUrl;
Log.e(TAG, "run: 开始执行");
FFmpegCmd.execute(commands);
Log.e(TAG, "run: 执行结束");
isRun = false;
}
};
}