Mac android呼叫ffmpeg 方法

sunxiaopengsun發表於2020-12-20

因為我們只編譯了 arm 版本的 so,所以需要把 so 拷到 armeabi-v7a 目錄下,完整路徑

├── build.gradle
├── libs
│   ├── armeabi-v7a
│   │   ├── libavcodec-57.so
│   │   ├── libavfilter-6.so
│   │   ├── libavformat-57.so
│   │   ├── libavutil-55.so
│   │   ├── libswresample-2.so
│   │   └── libswscale-4.so
│   └── include
│       ├── libavcodec
│       ├── libavfilter
│       ├── libavformat
│       ├── libavutil
│       ├── libswresample
│       └── libswscale
├── proguard-rules.pro
└── src
    └── main
        ├── AndroidManifest.xml
        ├── cpp
        ├── java
        └── res

指定JniLibs庫的地址,這個是在NDK編譯時能夠找到相應的庫路徑或標頭檔案的路徑

sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }

指定abifilter:指定當前軟體支援的CPU

android {
      ...
      defaultConfig {
	 ...
        ndk {
            abiFilters 
  			'armeabi-v7a'
        }
      }
  }

編寫Cmake指令碼

cmake_minimum_required(VERSION 3.4.1)
add_library( # 設定庫的名字
             native-lib
             # 設定為共享庫
             SHARED
             # 編譯下面的問生成的庫檔案時上面的名字
             src/main/cpp/native-lib.cpp )
add_library( avcodec-57  #這是我們編譯的庫檔案
             SHARED  #共享庫
             IMPORTED ) 
set_target_properties( avcodec-57  #編譯的庫檔案
                       PROPERTIES IMPORTED_LOCATION   #定義匯入庫的文章
                       ../../../../libs/armeabi-v7a/libavcodec-57.so )
add_library( avfilter-6
             SHARED
             IMPORTED )
set_target_properties( avfilter-6
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libavfilter-6.so )
add_library( avformat-57
             SHARED
             IMPORTED )
set_target_properties( avformat-57
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libavformat-57.so )
add_library( avutil-55
             SHARED
             IMPORTED )
set_target_properties( avutil-55
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libavutil-55.so )
add_library( swresample-2
             SHARED
             IMPORTED )
set_target_properties( swresample-2
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libswresample-2.so )
add_library( swscale-4
             SHARED
             IMPORTED )
set_target_properties( swscale-4
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libswscale-4.so )

#指定標頭檔案的目錄地址   
include_directories( libs/include )

#日誌檔案檔案庫的這種
find_library(
              log-lib
              log )
#對連線庫的指定
target_link_libraries( # Specifies the target library.
                       native-lib
                       avcodec-57
                       avfilter-6
                       avformat-57
                       avutil-55
                       swresample-2
                       swscale-4
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

在java檔案中引用下列相應的庫檔案:下列的庫要注意相應的引入順序,如果順序不對可能有好多異常出現(後面使用單個so是就不會出現)

static {
    System.loadLibrary("native-lib");
    System.loadLibrary("avcodec-57");
    System.loadLibrary("avfilter-6");
    System.loadLibrary("avformat-57");
    System.loadLibrary("avutil-55");
    System.loadLibrary("swresample-2");
    System.loadLibrary("swscale-4");
}

native-lib.cpp中輸入程式碼

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

extern "C"{
#include <libavcodec/avcodec.h>>
}

extern "C"
jstring
Java_com_example_testffmpeg_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
  //  return env->NewStringUTF(hello.c_str());
    return env->NewStringUTF(av_version_info());
}

相關文章