• MacOS(M1)交叉编译安卓版本OpenCV(附带FFmpeg)


    1 编译FFmpeg

    1.1 准备FFmpeg源码

    1. git clone https://gitee.com/mirrors/ffmpeg.git
    2. git checkout -b v4.2 origin/release/4.2 #切换到4.2分之,最新的不能适配opencv

    1.2 构建编译脚本

    在源码目录构建如下脚本文件,并执行

    1. #!/bin/bash
    2. #你的NDK路径
    3. NDK=/Users/gauss/Library/Android/sdk/ndk/21.4.7075529
    4. TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/darwin-x86_64
    5. API=21
    6. echo "进入FFmpeg编译脚本"
    7. function build_android
    8. {
    9. echo "Compiling FFmpeg for $CPU"
    10. ./configure \
    11. --prefix=$PREFIX \
    12. --libdir=$LIB_DIR \
    13. --enable-shared \
    14. --disable-static \
    15. --enable-jni \
    16. --disable-doc \
    17. --disable-symver \
    18. --disable-programs \
    19. --target-os=android \
    20. --arch=$ARCH \
    21. --cpu=$CPU \
    22. --cc=$CC \
    23. --cxx=$CXX \
    24. --enable-cross-compile \
    25. --sysroot=$SYSROOT \
    26. --extra-cflags="-Os -fpic $OPTIMIZE_CFLAGS" \
    27. --extra-ldflags="$ADDI_LDFLAGS" \
    28. --disable-asm \
    29. $COMMON_FF_CFG_FLAGS
    30. make clean
    31. make -j8 # 这里是定义用几个CPU编译
    32. make install
    33. echo "The Compilation of FFmpeg for $CPU is completed"
    34. }
    35. #################### armv7-a ################################
    36. echo "开始编译FFmpeg(armeabi-v7a)"
    37. OUTPUT_FOLDER="armeabi-v7a"
    38. ARCH="arm"
    39. CPU="armv7-a"
    40. TOOL_CPU_NAME=armv7a
    41. TOOL_PREFIX="$TOOLCHAIN/bin/arm-linux-androideabi"
    42. CC="$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang"
    43. CXX="$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang++"
    44. SYSROOT="$NDK/toolchains/llvm/prebuilt/darwin-x86_64/sysroot"
    45. PREFIX="${PWD}/android/$OUTPUT_FOLDER"
    46. LIB_DIR="${PWD}/android/libs/$OUTPUT_FOLDER"
    47. OPTIMIZE_CFLAGS="-march=$CPU"
    48. build_android
    49. #################### armv8-a ################################
    50. echo "开始编译FFmpeg(arm64-v8a)"
    51. OUTPUT_FOLDER="arm64-v8a"
    52. ARCH=arm64
    53. CPU="armv8-a"
    54. TOOL_CPU_NAME=aarch64
    55. TOOL_PREFIX="$TOOLCHAIN/bin/$TOOL_CPU_NAME-linux-android"
    56. CC="$TOOL_PREFIX$API-clang"
    57. CXX="$TOOL_PREFIX$API-clang++"
    58. SYSROOT="$NDK/toolchains/llvm/prebuilt/darwin-x86_64/sysroot"
    59. PREFIX="${PWD}/android/$OUTPUT_FOLDER"
    60. LIB_DIR="${PWD}/android/libs/$OUTPUT_FOLDER"
    61. OPTIMIZE_CFLAGS="-march=$CPU"
    62. build_android
    63. #################### x86 ################################
    64. # echo "开始编译FFmpeg(x86)"
    65. # OUTPUT_FOLDER="x86"
    66. # ARCH="x86"
    67. # CPU="x86"
    68. # TOOL_CPU_NAME="i686"
    69. # TOOL_PREFIX="$TOOLCHAIN/bin/${TOOL_CPU_NAME}-linux-android"
    70. # CC="$TOOL_PREFIX$API-clang"
    71. # CXX="$TOOL_PREFIX$API-clang++"
    72. # SYSROOT="$NDK/toolchains/llvm/prebuilt/darwin-x86_64/sysroot"
    73. # PREFIX="${PWD}/android/$OUTPUT_FOLDER"
    74. # LIB_DIR="${PWD}/android/libs/$OUTPUT_FOLDER"
    75. # OPTIMIZE_CFLAGS="-march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32"
    76. # build_android
    77. #################### x86_64 ################################
    78. # echo "开始编译FFmpeg(x86_64)"
    79. # OUTPUT_FOLDER="x86_64"
    80. # ARCH="x86_64"
    81. # CPU="x86-64"
    82. # TOOL_CPU_NAME="x86_64"
    83. # TOOL_PREFIX="$TOOLCHAIN/bin/${TOOL_CPU_NAME}-linux-android"
    84. # CC="$TOOL_PREFIX$API-clang"
    85. # CXX="$TOOL_PREFIX$API-clang++"
    86. # SYSROOT="$NDK/toolchains/llvm/prebuilt/darwin-x86_64/sysroot"
    87. # PREFIX="${PWD}/android/$OUTPUT_FOLDER"
    88. # LIB_DIR="${PWD}/android/libs/$OUTPUT_FOLDER"
    89. # OPTIMIZE_CFLAGS="-march=$CPU"
    90. # build_android

    编译好之后得到库文件

    添加ffmpeg-config.cmake文件,内容如下(针对armv7)

    1. set(ffmpeg_path "${CMAKE_CURRENT_LIST_DIR}")
    2. message("ffmpeg_path: ${ffmpeg_path}")
    3. set(FFMPEG_LIBDIR "/opt/code/ffmpeg/android/libs/armeabi-v7a")
    4. set(FFMPEG_INCLUDE_DIRS "/opt/code/ffmpeg/android/armeabi-v7a/include")
    5. # library names
    6. set(FFMPEG_LIBRARIES
    7. ${FFMPEG_LIBDIR}/libavformat.so
    8. ${FFMPEG_LIBDIR}/libavdevice.so
    9. ${FFMPEG_LIBDIR}/libavcodec.so
    10. ${FFMPEG_LIBDIR}/libavutil.so
    11. ${FFMPEG_LIBDIR}/libswscale.so
    12. ${FFMPEG_LIBDIR}/libswresample.so
    13. ${FFMPEG_LIBDIR}/libavfilter.so
    14. )
    15. # found status
    16. set(FFMPEG_libavformat_FOUND TRUE)
    17. set(FFMPEG_libavdevice_FOUND TRUE)
    18. set(FFMPEG_libavcodec_FOUND TRUE)
    19. set(FFMPEG_libavutil_FOUND TRUE)
    20. set(FFMPEG_libswscale_FOUND TRUE)
    21. set(FFMPEG_libswresample_FOUND TRUE)
    22. set(FFMPEG_libavfilter_FOUND TRUE)
    23. # library versions, 注意这几个变量,一定要设置为全局CACHE变量
    24. set(FFMPEG_libavutil_VERSION 56.31.100 CACHE INTERNAL "FFMPEG_libavutil_VERSION") # info
    25. set(FFMPEG_libavcodec_VERSION 58.54.100 CACHE INTERNAL "FFMPEG_libavcodec_VERSION") # info
    26. set(FFMPEG_libavformat_VERSION 58.29.100 CACHE INTERNAL "FFMPEG_libavformat_VERSION") # info
    27. set(FFMPEG_libavdevice_VERSION 58.8.100 CACHE INTERNAL "FFMPEG_libavdevice_VERSION") # info
    28. set(FFMPEG_libavfilter_VERSION 7.57.100 CACHE INTERNAL "FFMPEG_libavfilter_VERSION") # info
    29. set(FFMPEG_libswscale_VERSION 5.5.100 CACHE INTERNAL "FFMPEG_libswscale_VERSION") # info
    30. set(FFMPEG_libswresample_VERSION 3.5.100 CACHE INTERNAL "FFMPEG_libswresample_VERSION") # info
    31. set(FFMPEG_FOUND TRUE)
    32. set(FFMPEG_LIBS ${FFMPEG_LIBRARIES})
    33. status(" #################################### FFMPEG:" FFMPEG_FOUND THEN "YES (find_package)" ELSE "NO (find_package)")
    34. status(" avcodec:" FFMPEG_libavcodec_VERSION THEN "YES (${FFMPEG_libavcodec_VERSION})" ELSE NO)
    35. status(" avformat:" FFMPEG_libavformat_VERSION THEN "YES (${FFMPEG_libavformat_VERSION})" ELSE NO)
    36. status(" avutil:" FFMPEG_libavutil_VERSION THEN "YES (${FFMPEG_libavutil_VERSION})" ELSE NO)
    37. status(" swscale:" FFMPEG_libswscale_VERSION THEN "YES (${FFMPEG_libswscale_VERSION})" ELSE NO)
    38. status(" avresample:" FFMPEG_libavresample_VERSION THEN "YES (${FFMPEG_libavresample_VERSION})" ELSE NO)

    2 编译 OpenCV

    2.1 准备OpenCV源码

    1. git clone https://gitee.com/code-github/opencv_opencv.git
    2. * 4.x f7ac724c5d Merge pull request #21531 from fengyuentau:add_3rdparty_mirrors
    3. git branch -r
    4. origin/2.4
    5. origin/3.4
    6. origin/4.x
    7. origin/5.x
    8. origin/HEAD -> origin/4.x #使用的是4.x分支
    9. origin/master
    10. origin/next

    2.2 修改源码根目录下CMakeLists.txt

    需要修改两个地方

     

     2.3 执行cmake

    需要准备SDk和NDK的路径

    1. cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake -DCMAKE_ANDROID_NDK=$ANDROID_NDK \
    2. -DANDROID_NATIVE_API_LEVEL=android-21 \
    3. -DCMAKE_BUILD_TYPE=Release \
    4. -DCMAKE_INSTALL_PREFIX=/opt/code/opencv-4.x/ \
    5. -DBUILD_JAVA=OFF \
    6. -DANDROID_SDK_TOOLS=$ANDROID_SDK/build-tools/29.0.2 \
    7. -DBUILD_ANDROID_EXAMPLES=OFF \
    8. -DCMAKE_EXPORT_COMPILE_COMMANDS=OFF \
    9. -DBUILD_SHARED_LIBS=ON \
    10. -DWITH_FFMPEG=ON \
    11. -DBUILD_FAT_JAVA_LIB=OFF \
    12. -DOPENCV_GENERATE_PKGCONFIG=ON \
    13. -DOPENCV_FFMPEG_USE_FIND_PACKAGE=ON \
    14. -DOPENCV_FFMPEG_SKIP_BUILD_CHECK=ON \
    15. -DFFMPEG_DIR=/opt/code/ffmpeg/android/ ..

    最后可以看到已经关联了FFmpeg

    1. --
    2. -- Video I/O:
    3. -- FFMPEG: YES (find_package)
    4. -- avcodec: YES (58.54.100)
    5. -- avformat: YES (58.29.100)
    6. -- avutil: YES (56.31.100)
    7. -- swscale: YES (5.5.100)
    8. -- avresample: NO
    9. --
    10. -- Parallel framework: pthreads
    11. --
    12. -- Trace: YES (with Intel ITT)
    13. --
    14. -- Other third-party libraries:
    15. -- Custom HAL: YES (carotene (ver 0.0.1))
    16. -- Protobuf: build (3.19.1)
    17. --
    18. -- Python (for build): /opt/homebrew/bin/python3
    19. --
    20. -- Install to: /opt/code/opencv-4.x

    2.4 执行编译

    1. make -j10
    2. make install

    参考:

    opencv+ffmpeg编译打包全解指南 - 知乎 (zhihu.com)  Android最简单的基于FFmpeg的例子(一)---编译FFmpeg类库 | binglingziyu的博客

    Android交叉编译OpenCV+FFmpeg+x264的艰难历程_dreamgyf的博客-CSDN博客

  • 相关阅读:
    计算机毕业设计选什么题目好?springboot 研究生管理系统
    常用redis-lua脚本
    Redis是单线程吗?以及为什么使用单线程(Redis 的网络模型)
    【SLAM】3三维刚体运动
    Tomcat 与 JDK 对应版本关系
    git reset hard,mixed,soft
    微擎模块 抖抖赢口红1.1.5小程序 前端+后端 优化分销升级功能
    【java8】函数式接口
    kali终端查看图形类数据
    Java—Collection
  • 原文地址:https://blog.csdn.net/idwtwt/article/details/127423261