AndroidStudio集成FFMPEG

一、FFMPEG源码编译

1.新建android项目,勾选include C++ support

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

3.Finish后的项目目录

扫描二维码关注公众号,回复: 4981188 查看本文章

4.libs下创建armeabi目录,拷贝ffmpeg编译后的so文件到armeabi下,拷贝include文件到libs目录下

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

6.CMakeLists.txt修改如下:

 
  1. cmake_minimum_required(VERSION 3.4.1)

  2. add_library( native-lib

  3. SHARED

  4. src/main/cpp/native-lib.cpp

  5. )

  6.  
  7. include_directories(libs/include)

  8. set(DIR ../../../../libs)

  9. MESSAGE("打印")

  10. MESSAGE("路径 " ${DIR})

  11. add_library(avcodec-56

  12. SHARED

  13. IMPORTED)

  14. set_target_properties(avcodec-56

  15. PROPERTIES IMPORTED_LOCATION

  16. ${DIR}/armeabi/libavcodec-56.so)

  17. add_library(avdevice-56

  18. SHARED

  19. IMPORTED)

  20. set_target_properties(avdevice-56

  21. PROPERTIES IMPORTED_LOCATION

  22. ${DIR}/armeabi/libavdevice-56.so)

  23. add_library(avformat-56

  24. SHARED

  25. IMPORTED)

  26. set_target_properties(avformat-56

  27. PROPERTIES IMPORTED_LOCATION

  28. ${DIR}/armeabi/libavformat-56.so)

  29. add_library(avutil-54

  30. SHARED

  31. IMPORTED)

  32. set_target_properties(avutil-54

  33. PROPERTIES IMPORTED_LOCATION

  34. ${DIR}/armeabi/libavutil-54.so)

  35. add_library(swresample-1

  36. SHARED

  37. IMPORTED)

  38. set_target_properties(swresample-1

  39. PROPERTIES IMPORTED_LOCATION

  40. ${DIR}/armeabi/libswresample-1.so)

  41. add_library(swscale-3

  42. SHARED

  43. IMPORTED)

  44. set_target_properties(swscale-3

  45. PROPERTIES IMPORTED_LOCATION

  46. ${DIR}/armeabi/libswscale-3.so)

  47. add_library(avfilter-5

  48. SHARED

  49. IMPORTED)

  50. set_target_properties(avfilter-5

  51. PROPERTIES IMPORTED_LOCATION

  52. ${DIR}/armeabi/libavfilter-5.so)

  53.  
  54. find_library( log-lib

  55. log )

  56. target_link_libraries( native-lib

  57. avcodec-56

  58. avdevice-56

  59. avformat-56

  60. avutil-54

  61. swresample-1

  62. swscale-3

  63. -landroid # Add this.

  64. ${log-lib} )


7.cpp目录下新建native-lib.h头文件,native-lib.cpp是默认生成的

native-lib.h

 
  1. //

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

  3. //

  4.  
  5. #ifndef FFMPEG_NATIVE_LIB_H

  6. #define FFMPEG_NATIVE_LIB_H

  7. #include <jni.h>

  8. #include <string>

  9. #include <android/log.h>

  10.  
  11. extern "C" {

  12. //编码

  13. #include "libavcodec/avcodec.h"

  14. //封装格式处理

  15. #include "libavformat/avformat.h"

  16. //像素处理

  17. #include "libswscale/swscale.h"

  18. #include <android/native_window_jni.h>

  19. #include <unistd.h>

  20. JNIEXPORT jstring JNICALL Java_com_test_ffmpeg_MainActivity_stringFromJNI(JNIEnv *env, jobject /* this */);

  21. }

  22. #define LOGI(FORMAT, ...) __android_log_print(ANDROID_LOG_INFO,"jason",FORMAT,##__VA_ARGS__);

  23. #define LOGE(FORMAT, ...) __android_log_print(ANDROID_LOG_ERROR,"jason",FORMAT,##__VA_ARGS__);

  24.  
  25.  
  26. #endif //FFMPEG_NATIVE_LIB_H

native-lib.cpp

 
  1. #include "native-lib.h"

  2. jstring Java_com_test_ffmpeg_MainActivity_stringFromJNI(JNIEnv *env, jobject /* this */) {

  3. std::string hello = "Hello from C++";

  4. av_register_all();

  5. return env->NewStringUTF(hello.c_str());

  6. }


8.MainActivity中配置如下:

9.运行程序,如果可以正常运行,说明ffmpeg的配置已经ok。

猜你喜欢

转载自blog.csdn.net/weixin_37897683/article/details/82586257