天天看點

二、AndroidStudio內建FFMPEG

本文基于以下連結

一、FFMPEG源碼編譯

1.建立android項目,勾選include C++ support

二、AndroidStudio內建FFMPEG

2.勾選Exceptions Support (-fexceptions)、Runtime Type Information Support (-frtti)

二、AndroidStudio內建FFMPEG

3.Finish後的項目目錄

二、AndroidStudio內建FFMPEG

4.libs下建立armeabi目錄,拷貝ffmpeg編譯後的so檔案到armeabi下,拷貝include檔案到libs目錄下

二、AndroidStudio內建FFMPEG

5.app下的build.gradle修改如下:

二、AndroidStudio內建FFMPEG

6.CMakeLists.txt修改如下:

cmake_minimum_required(VERSION 3.4.1)
add_library( native-lib
             SHARED
             src/main/cpp/native-lib.cpp
             )

include_directories(libs/include)
set(DIR ../../../../libs)
MESSAGE("列印")
MESSAGE("路徑 " ${DIR})
add_library(avcodec-56
            SHARED
            IMPORTED)
set_target_properties(avcodec-56
                      PROPERTIES IMPORTED_LOCATION
                      ${DIR}/armeabi/libavcodec-56.so)
add_library(avdevice-56
            SHARED
            IMPORTED)
set_target_properties(avdevice-56
                      PROPERTIES IMPORTED_LOCATION
                      ${DIR}/armeabi/libavdevice-56.so)
add_library(avformat-56
            SHARED
            IMPORTED)
set_target_properties(avformat-56
                      PROPERTIES IMPORTED_LOCATION
                      ${DIR}/armeabi/libavformat-56.so)
add_library(avutil-54
            SHARED
            IMPORTED)
set_target_properties(avutil-54
                      PROPERTIES IMPORTED_LOCATION
                      ${DIR}/armeabi/libavutil-54.so)
 add_library(swresample-1
             SHARED
             IMPORTED)
 set_target_properties(swresample-1
                       PROPERTIES IMPORTED_LOCATION
                       ${DIR}/armeabi/libswresample-1.so)
  add_library(swscale-3
              SHARED
              IMPORTED)
  set_target_properties(swscale-3
                        PROPERTIES IMPORTED_LOCATION
                        ${DIR}/armeabi/libswscale-3.so)
  add_library(avfilter-5
              SHARED
              IMPORTED)
  set_target_properties(avfilter-5
                        PROPERTIES IMPORTED_LOCATION
                        ${DIR}/armeabi/libavfilter-5.so)

find_library( log-lib
              log )
target_link_libraries( native-lib
                       avcodec-56
                       avdevice-56
                       avformat-56
                       avutil-54
                       swresample-1
                       swscale-3
                       -landroid # Add this.
                       ${log-lib} )
           

7.cpp目錄下建立native-lib.h頭檔案,native-lib.cpp是預設生成的

二、AndroidStudio內建FFMPEG

native-lib.h

//
// Created by ygdx_lk on 17/11/1.
//

#ifndef FFMPEG_NATIVE_LIB_H
#define FFMPEG_NATIVE_LIB_H
#include <jni.h>
#include <string>
#include <android/log.h>

extern "C" {
//編碼
#include "libavcodec/avcodec.h"
//封裝格式處理
#include "libavformat/avformat.h"
//像素處理
#include "libswscale/swscale.h"
#include <android/native_window_jni.h>
#include <unistd.h>
JNIEXPORT jstring JNICALL Java_com_test_ffmpeg_MainActivity_stringFromJNI(JNIEnv *env, jobject /* this */);
}
#define LOGI(FORMAT, ...) __android_log_print(ANDROID_LOG_INFO,"jason",FORMAT,##__VA_ARGS__);
#define LOGE(FORMAT, ...) __android_log_print(ANDROID_LOG_ERROR,"jason",FORMAT,##__VA_ARGS__);


#endif //FFMPEG_NATIVE_LIB_H
           

native-lib.cpp

#include "native-lib.h"
jstring Java_com_test_ffmpeg_MainActivity_stringFromJNI(JNIEnv *env, jobject /* this */) {
    std::string hello = "Hello from C++";
    av_register_all();
    return env->NewStringUTF(hello.c_str());
}
           

8.MainActivity中配置如下:

二、AndroidStudio內建FFMPEG
二、AndroidStudio內建FFMPEG

9.運作程式,如果可以正常運作,說明ffmpeg的配置已經ok。