ffmpeg系列之移植到安卓中调用

Posted 狂奔的CD

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ffmpeg系列之移植到安卓中调用相关的知识,希望对你有一定的参考价值。

文章目录

基础调用

1)编译安卓需要的so,请参考

编译ffmpeg的安卓版本

2)将so引入安卓中使用

在module的src/main/下创建cpp和jniLibs模块,jniLibs中存放so文件

3)cpp中进行cmake配置

将ffmpeg的头文件拷贝到src/main/cpp下

配置cmakelist

# For more information about using CMake with android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.10.2)

# Declares and names the project.

project("ffmpeg")

#设置FFmpeg头文件的路径,共享库,include里面h文件调用其他h文件用到的
include_directories(
        include#因为和CMakeLists.txt在同一级,所以直接写include
)

add_library(ffmpeg
        SHARED
        ffmpeg_util.cpp)

find_library(
        log-lib
        log)

add_library(avcodec
        SHARED
        IMPORTED)
#给avcodec这个变量赋值
set_target_properties(avcodec
        PROPERTIES IMPORTED_LOCATION
        $CMAKE_SOURCE_DIR/../jniLibs/$ANDROID_ABI/libavcodec.so)


add_library(avdevice
        SHARED
        IMPORTED)
set_target_properties(avdevice
        PROPERTIES IMPORTED_LOCATION
        $CMAKE_SOURCE_DIR/../jniLibs/$ANDROID_ABI/libavdevice.so)

add_library(avformat
        SHARED
        IMPORTED)
set_target_properties(avformat
        PROPERTIES IMPORTED_LOCATION
        $CMAKE_SOURCE_DIR/../jniLibs/$ANDROID_ABI/libavformat.so)

add_library(avutil
        SHARED
        IMPORTED)
set_target_properties(avutil
        PROPERTIES IMPORTED_LOCATION
        $CMAKE_SOURCE_DIR/../jniLibs/$ANDROID_ABI/libavutil.so)


add_library(swresample
        SHARED
        IMPORTED)
set_target_properties(swresample
        PROPERTIES IMPORTED_LOCATION
        $CMAKE_SOURCE_DIR/../jniLibs/$ANDROID_ABI/libswresample.so)

add_library(swscale
        SHARED
        IMPORTED)
set_target_properties(swscale
        PROPERTIES IMPORTED_LOCATION
        $CMAKE_SOURCE_DIR/../jniLibs/$ANDROID_ABI/libswscale.so)

add_library(avfilter
        SHARED
        IMPORTED)
set_target_properties(avfilter
        PROPERTIES IMPORTED_LOCATION
        $CMAKE_SOURCE_DIR/../jniLibs/$ANDROID_ABI/libavfilter.so)



target_link_libraries(ffmpeg
        $log-lib
        avcodec swresample avutil)

4) 编写jni测试代码

#include <android/log.h>

#include <jni.h>
#include <stdlib.h>


#ifdef __cplusplus
extern "C"

#endif
/*Include ffmpeg header file*/
#include "libavcodec/avcodec.h"

#ifdef __cplusplus

#endif

extern "C" JNIEXPORT jstring JNICALL
Java_com_cd_lib_ffmpeg_FfmpegUtil_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) 
    return env->NewStringUTF(avcodec_configuration());


根据jni规则编码java

public class FfmpegUtil 

    // Used to load the 'ffmpeg' library on application startup.
    static 
        System.loadLibrary("ffmpeg");
    

    /**
     * A native method that is implemented by the 'ffmpeg' native library,
     * which is packaged with this application.
     */
    public static native String stringFromJNI();


5) 配置gradle相关位置



android 
    ...

    defaultConfig 
       ...
        ndk 
            abiFilters  'armeabi-v7a', 'arm64-v8a'
        
    
    sourceSets 
        main 
            jniLibs.srcDirs = ['libs']
        
    

    externalNativeBuild 
        cmake 
            path file('src/main/cpp/CMakeLists.txt')
            version '3.10.2'
        
    
    ...


6)build运行即可

以上是关于ffmpeg系列之移植到安卓中调用的主要内容,如果未能解决你的问题,请参考以下文章

ffmpeg系列之编译安卓版ffmpeg库

ffmpeg系列之编译安卓版ffmpeg库

FFmpeg开发笔记:ffmpeg在移植到海思HI35xx平台之将ffmpeg库引入到sample的demo中

如何移植音频驱动到android4.4

opencv系列之ubuntu系统下编译python版本的opencv(指定特定的ffmpeg)

opencv系列之ubuntu系统下编译python版本的opencv(指定特定的ffmpeg)