编译ffmpeg

首先,我们需要编译ffmpeg,具体可以看 Ubuntu ffmpeg编译

在AS中使用ffmpeg

新建一个带ndk的Android项目,复制选中的如下文件到项目的jniLibs目录的armeabi-v7a文件夹下
基于Android Studio3.2实现ffmpeg最简单的例子
接着,复制include文件夹(头文件)到src/main/cpp文件夹下

把整个include文件夹都复制到cpp文件夹下,里面的依赖关系无需改动,ffmpeg内部已依赖好

基于Android Studio3.2实现ffmpeg最简单的例子
修改cpp文件夹下的native-lib.cpp为native-lib.c
然后,编写CMakeLists

cmake_minimum_required(VERSION 3.4.1)

#set(distribution_DIR ${CMAKE_SOURCE_DIR}/libs)            # 文件夹为libs的情况下
set(distribution_DIR ${CMAKE_SOURCE_DIR}/src/main/jniLibs) # 文件夹为jniLibs的情况下
message("distribution_DIR:" ${distribution_DIR})

add_library( libavcodec-56
        SHARED
        IMPORTED )

add_library( libavdevice-56
        SHARED
        IMPORTED )

add_library( libavfilter-5
        SHARED
        IMPORTED )

add_library( libavformat-56
        SHARED
        IMPORTED )

add_library( libavutil-54
        SHARED
        IMPORTED )

add_library( libpostproc-53
        SHARED
        IMPORTED )

add_library( libswresample-1
        SHARED
        IMPORTED )

add_library( libswscale-3
        SHARED
        IMPORTED )

set_target_properties( libavcodec-56
        PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/${ANDROID_ABI}/libavcodec-56.so)

set_target_properties( libavdevice-56
        PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/${ANDROID_ABI}/libavdevice-56.so)

set_target_properties( libavfilter-5
        PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/${ANDROID_ABI}/libavfilter-5.so)

set_target_properties( libavformat-56
        PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/${ANDROID_ABI}/libavformat-56.so)

set_target_properties( libavutil-54
        PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/${ANDROID_ABI}/libavutil-54.so)

set_target_properties( libpostproc-53
        PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/${ANDROID_ABI}/libpostproc-53.so)

set_target_properties( libswresample-1
        PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/${ANDROID_ABI}/libswresample-1.so)

set_target_properties( libswscale-3
        PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/${ANDROID_ABI}/libswscale-3.so)

add_library(
        native-lib
        SHARED
        ${CMAKE_SOURCE_DIR}/src/main/cpp/native-lib.c) #如果是C++则为native-lib.cpp

find_library(
        log-lib
        log)

include_directories(src/main/cpp/include)

target_link_libraries( # Specifies the target library.
        native-lib
        libavcodec-56
        libavdevice-56
        libavfilter-5
        libavformat-56
        libavutil-54
        libpostproc-53
        libswresample-1
        libswscale-3
        android #如果我们在编写代码时使用了ANativeWindow相关的方法的话,需要在链接库文件的时候加入android这个库文件,不然会报undefined reference to的错
        ${log-lib} )

然后,新建一个Java类VideoUtils

	public class VideoUtils {
	    static {
	        //顺序不能乱
	        System.loadLibrary("avutil-54");
	        System.loadLibrary("swresample-1");
	        System.loadLibrary("avcodec-56");
	        System.loadLibrary("avformat-56");
	        System.loadLibrary("swscale-3");
	        System.loadLibrary("postproc-53");
	        System.loadLibrary("avfilter-5");
	        System.loadLibrary("avdevice-56");
	        System.loadLibrary("native-lib");
	    }
	
	    public native static String ffmpegInfo();
	}

接着,在native-lib.c中实现ffmpegInfo方法

#include <jni.h>
#include <android/log.h>
//编码
#include "include/libavcodec/avcodec.h"
//封装格式处理
#include "include/libavformat/avformat.h"
//像素处理
#include "include/libswscale/swscale.h"

#define LOGI(FORMAT,...) __android_log_print(ANDROID_LOG_INFO,"heiko",FORMAT,__VA_ARGS__);
#define LOGE(FORMAT,...) __android_log_print(ANDROID_LOG_ERROR,"heiko",FORMAT,__VA_ARGS__);

JNIEXPORT jstring JNICALL
Java_com_ethanco_ffmpegtest_VideoUtils_ffmpegInfo(JNIEnv *env, jclass type) {
    char info[10000] = { 0 };
    sprintf(info, "%s\n", avcodec_configuration());
    return (*env)->NewStringUTF(env, info);
}  

接着,我们调用ffmpeginfo方法

TextView tv = findViewById(R.id.sample_text);
tv.setText(VideoUtils.ffmpegInfo());  

最后,运行项目,如下图所示。
基于Android Studio3.2实现ffmpeg最简单的例子
可以查看到FFmpeg类库相关的信息,即表示ffmpeg配置成功。

其他

相关Demo

相关文章: