android studio configuration ffmpeg

Keywords: Gradle Android cmake C

1) copy the compiled so library to the libs folder, and the header file in include to the libs folder.

2) add the following code to build.gradle

defaultConfig {
        applicationId "com.houde.ffmpeg.test"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-frtti -fexceptions"
                abiFilters 'armeabi-v7a'
            }
        }
        sourceSets{
            main{
                jniLibs.srcDirs = ['libs']
            }
        }
        ndk{
            abiFilters 'armeabi-v7a'
        }
    }

3) add the following code to CMakeList.txt

include_directories(libs/include)
set(DIR ${CMAKE_SOURCE_DIR}/libs)
add_library(avcodec
        SHARED
        IMPORTED)
set_target_properties(avcodec
        PROPERTIES IMPORTED_LOCATION
        ${DIR}/armeabi-v7a/libavcodec.so)

add_library(avdevice
        SHARED
        IMPORTED)
set_target_properties(avdevice
        PROPERTIES IMPORTED_LOCATION
        ${DIR}/armeabi-v7a/libavdevice.so)
add_library(avformat
        SHARED
        IMPORTED)
set_target_properties(avformat
        PROPERTIES IMPORTED_LOCATION
        ${DIR}/armeabi-v7a/libavformat.so)
add_library(avutil
        SHARED
        IMPORTED)
set_target_properties(avutil
        PROPERTIES IMPORTED_LOCATION
        ${DIR}/armeabi-v7a/libavutil.so)
add_library(postproc
        SHARED
        IMPORTED)
set_target_properties(postproc
        PROPERTIES IMPORTED_LOCATION
        ${DIR}/armeabi-v7a/libpostproc.so)
add_library(swresample
        SHARED
        IMPORTED)
set_target_properties(swresample
        PROPERTIES IMPORTED_LOCATION
        ${DIR}/armeabi-v7a/libswresample.so)
add_library(swscale
        SHARED
        IMPORTED)
set_target_properties(swscale
        PROPERTIES IMPORTED_LOCATION
        ${DIR}/armeabi-v7a/libswscale.so)
add_library(avfilter
        SHARED
        IMPORTED)
set_target_properties(avfilter
        PROPERTIES IMPORTED_LOCATION
        ${DIR}/armeabi-v7a/libavfilter.so)

add_library( # Sets the name of the library.
        native-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        src/main/cpp/native-lib.cpp)
target_link_libraries( # Specifies the target library.
        native-lib
        avfilter
        avcodec
        avdevice
        avformat
        avutil
        postproc
        swresample
        swscale
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

4) check whether the configured so is correct, introduce lib avcodec / avcodec. H into the native lib.cpp file, and modify the returned string content:

 std::string hello = avcodec_configuration();
    return env->NewStringUTF(hello.c_str());

Display results:

Error logging:
1) ffmpeg function cannot be recognized
The compilation shows "undefined reference to 'avcodec'register'all()" and "error: (15) undefined reference to' avcodec'configuration()". The error is as follows

Error:(15) undefined reference to 'avcodec_register_all()'
Error:(15) undefined reference to 'avcodec_configuration()'

This is because ffmpeg is a pure C language library. When using C + + to call C code, you need to use "extern" C "to surround the header file" avcodec.h "and related code, as follows:

#include <jni.h>
#include <string>

#ifdef __cplusplus
extern "C" {
#endif

#include "libavcodec/avcodec.h"
#include "libavutil/avutil.h"

JNIEXPORT jstring JNICALL
Java_com_houde_ffmpeg_test_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = avcodec_configuration();
    return env->NewStringUTF(hello.c_str());
}
#ifdef __cplusplus
};
#endif

Reference article:
https://www.jianshu.com/p/435bb46b33a9
https://blog.csdn.net/eieihihi/article/details/74153201

Posted by gozbay.com on Wed, 13 Nov 2019 12:25:44 -0800