FFmpeg(四):CMake移植FFmpeg的静态库到AndroidStudio中

前言

本文将介绍如何把FFmpeg(二):Windows下编译全平台架构的FFmpeg中编译出的支持全平台的FFmpeg静态so库,采用CMake方式移植到AndroidStudio中,并打印出FFmpeg的信息。

正文

  • 1 相关文件拷贝

    • 1.1 添加FFmpeg编译出的静态库

      在 FFmpeg(二):Windows下编译全平台架构的FFmpeg中我们将FFmpeg编译生成了各个平台适用的静态库,我们将每个CPU下的lib路径下的所有so包提取出来,在Android工程中的libs下建立各个平台对应的文件名称,并拷贝进去,如图所示:

      FFmpeg(四):CMake移植FFmpeg的静态库到AndroidStudio中_第1张图片

    • 1.2 拷贝头文件

      将 FFmpeg(二):Windows下编译全平台架构的FFmpeg中android_more下任意平台下的include文件夹全部拷贝至项目工程中src/main/cpp中,如图所示:

      FFmpeg(四):CMake移植FFmpeg的静态库到AndroidStudio中_第2张图片

    • 1.3 拷贝H264头文件

      在项目工程src/main/cpp/include下新建文件夹libx264,并把FFmpeg-3.2.5/libx264/android/任意CPU/include下编译出的x264.h和x264_config.h拷贝进去

  • 2 编写CMakeList.txt链接添加共享包

    看不懂第2步骤的请回头FFmpeg(三):初识CMakeList和native-lib

    • 2.1 以CMakeList.txt为基准设置相对路径
      set(distribution_DIR ${CMAKE_SOURCE_DIR}/../../../../libs)
      
    • 2.2 加载头文件和设置源码路径
      include_directories(
              # 加载头文件
               src/main/cpp/include
              #源码路径为绝对路径,下载后请记得修改你的源码路径
               E:/Workspace/OtherDemo/FFmpeg4Android-master/ffmpeg-3.2.5  ) 
      
    • 2.3 add_library添加共享库

      我们把libavcodec.so、libavfilter.so、libavformat.so、libavutil.so、libfdk-aac.so、libswresample.so、libswscale.so这七个库导入添加到共享库(这里不关注CPU架构),IMPORTED指2.4中根据CPU架构导入指定的so库。

      add_library(
         avcodec
         SHARED
         IMPORTED   )
      add_library(
         avfilter
         SHARED
         IMPORTED  )
      add_library(
         avformat
         SHARED
         IMPORTED  )
      add_library(
         avutil
         SHARED
         IMPORTED  )
      add_library(
         swresample
         SHARED
         IMPORTED  )
      add_library(
         swscale
         SHARED
         IMPORTED  )
      add_library(
         fdk-aac
         SHARED
         IMPORTED  )
      
      • 2.4 设置目标版本库

      判断当前机型是哪个CPU架构,就加载libs下对应的so包:

       #判断目标版本并设置
       if(${ANDROID_ABI} STREQUAL "armeabi")
       set_target_properties(
         avcodec
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/armeabi/libavcodec.so
       )
       set_target_properties(
         avfilter
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/armeabi/libavfilter.so
       )
       set_target_properties(
         avformat
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/armeabi/libavformat.so
         )
      
       set_target_properties(
         avutil
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/armeabi/libavutil.so
         )
       set_target_properties(
         swresample
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/armeabi/libswresample.so
          )
       set_target_properties(
         swscale
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/armeabi/libswscale.so
          )
       set_target_properties(
         fdk-aac
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/armeabi/libfdk-aac.so
          )
       endif(${ANDROID_ABI} STREQUAL "armeabi")
      
       if(${ANDROID_ABI} STREQUAL "armeabi-v7a")
      
       set_target_properties(
         avcodec
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/armeabi-v7a/libavcodec.so
       )
      
       set_target_properties(
        avfilter
        PROPERTIES IMPORTED_LOCATION
        ../../../../libs/armeabi-v7a/libavfilter.so
        )
      
       set_target_properties(
         avformat
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/armeabi-v7a/libavformat.so
         )
      
       set_target_properties(
         avutil
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/armeabi-v7a/libavutil.so
         )
      
       set_target_properties(
         swresample
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/armeabi-v7a/libswresample.so
          )
      
       set_target_properties(
         swscale
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/armeabi-v7a/libswscale.so
          )
      
       set_target_properties(
         fdk-aac
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/armeabi-v7a/libfdk-aac.so
          )
       endif(${ANDROID_ABI} STREQUAL "armeabi-v7a")
      
       if(${ANDROID_ABI} STREQUAL "arm64-v8a")
       set_target_properties(
       avcodec
       PROPERTIES IMPORTED_LOCATION
       ../../../../libs/arm64-v8a/libavcodec.so
        )
      
       set_target_properties(
       avfilter
       PROPERTIES IMPORTED_LOCATION
       ../../../../libs/arm64-v8a/libavfilter.so
        )
      
       set_target_properties(
         avformat
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/arm64-v8a/libavformat.so
         )
      
       set_target_properties(
         avutil
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/arm64-v8a/libavutil.so
         )
      
       set_target_properties(
         swresample
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/arm64-v8a/libswresample.so
          )
      
       set_target_properties(
         swscale
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/arm64-v8a/libswscale.so
          )
      
      
       set_target_properties(
         fdk-aac
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/arm64-v8a/libfdk-aac.so
          )
       endif(${ANDROID_ABI} STREQUAL "arm64-v8a")
      
       if(${ANDROID_ABI} STREQUAL "x86")
       set_target_properties(
        avcodec
        PROPERTIES IMPORTED_LOCATION
        ../../../../libs/x86/libavcodec.so
       )
      
       set_target_properties(
         avfilter
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/x86/libavfilter.so
       )
      
       set_target_properties(
         avformat
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/x86/libavformat.so
         )
      
       set_target_properties(
         avutil
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/x86/libavutil.so
         )
      
       set_target_properties(
         swresample
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/x86/libswresample.so
          )
      
       set_target_properties(
         swscale
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/x86/libswscale.so
          )
      
       set_target_properties(
         fdk-aac
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/x86/libfdk-aac.so
          )
       endif(${ANDROID_ABI} STREQUAL "x86")
      
       if(${ANDROID_ABI} STREQUAL "x86_64")
       set_target_properties(
         avcodec
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/x86_64/libavcodec.so
       )
      
       set_target_properties(
        avfilter
        PROPERTIES IMPORTED_LOCATION
        ../../../../libs/x86_64/libavfilter.so
       )
      
       set_target_properties(
         avformat
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/x86_64/libavformat.so
         )
      
       set_target_properties(
         avutil
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/x86_64/libavutil.so
         )
      
       set_target_properties(
         swresample
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/x86_64/libswresample.so
          )
      
       set_target_properties(
         swscale
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/x86_64/libswscale.so
          )
      
       set_target_properties(
         fdk-aac
         PROPERTIES IMPORTED_LOCATION
         ../../../../libs/x86_64/libfdk-aac.so
          )
       endif(${ANDROID_ABI} STREQUAL "x86_64")
      
      • 2.5连接共享库

      将所有添加进来的共享库和NDK中查到到已有的库链接起来:

       target_link_libraries(
                    native-lib
                    fdk-aac
                    avcodec
                    avfilter
                    avformat
                    avutil
                    swresample
                    swscale
                    ${log-lib}  )      
      
  • 3 相关代码编写

    • 3.1新建java文件FFmpegKit,内容如下:
    /**
     * Created by MouShao on 2018/2/28.
     */
    public class FFmpegKit {
    static {
      //加载CMakeList中连接的共享库
      System.loadLibrary("native-lib");
      System.loadLibrary("avutil");
      System.loadLibrary("fdk-aac");
      System.loadLibrary("avcodec");
      System.loadLibrary("avformat");
      System.loadLibrary("swscale");
      System.loadLibrary("swresample");
      System.loadLibrary("avfilter");
      }
      public static native String stringFromFFmpeg();
    }
    
  • 3.2 native-lib.cpp编写
    #include 
    #include 
    #include 
    
    extern "C" {
    #include "libavcodec/avcodec.h"
    #include "libavfilter/avfilter.h"
    #include "libavformat/avformat.h"
    #include "libavutil/imgutils.h"
    #include "libswresample/swresample.h"
    #include "libswscale/swscale.h"
    #include "libx264/x264.h"
    
    #define  LOG_TAG    "videoplayer"
    #define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
    
    JNIEXPORT jstring JNICALL Java_com_pvirtech_ffmpeg4android_FFmpegKit_stringFromFFmpeg(
      JNIEnv *env,
      jobject /* this */ obj) {
          char info[10000] = {0};
          sprintf(info, "%s\n", avcodec_configuration());
          return env->NewStringUTF(info);
       }
    };
    
  • 3.3 app下build.gradle的配置
      android {
      ···
      defaultConfig {
      ···
      externalNativeBuild {
          cmake {
              //配置C++11标准
              cppFlags "-std=c++11"
          }
          ndk{
              //abi兼容设置
              abiFilters "armeabi-v7a","armeabi","arm64-v8a","x86","x86_64"
          }
      }
    }
        //导入app/libs下库
        sourceSets.main {
        jniLibs.srcDirs = ['libs']
        jni.srcDirs = []
      }
    }
    
  • 3.4 MainActivity调用
    FFmpeg(四):CMake移植FFmpeg的静态库到AndroidStudio中_第3张图片
  • 4 运行如图所示:

FFmpeg(四):CMake移植FFmpeg的静态库到AndroidStudio中_第4张图片

结语

到此,FFmpeg的首秀就完成了,下一篇我们将如何用JNI动态注册的方式调用FFmpeg播放视频。

下一节讲:
FFmpeg(五):JNI动态注册方法调用FFmpeg播放视频

github源码

半停更说明

碎碎念:如果诸君喜欢,请点个赞
更多问题,欢迎加群:584275290

你可能感兴趣的:(FFmpeg(四):CMake移植FFmpeg的静态库到AndroidStudio中)