- git clone https://gitee.com/mirrors/ffmpeg.git
-
- git checkout -b v4.2 origin/release/4.2 #切换到4.2分之,最新的不能适配opencv
-
-
-
-
在源码目录构建如下脚本文件,并执行
- #!/bin/bash
-
- #你的NDK路径
- NDK=/Users/gauss/Library/Android/sdk/ndk/21.4.7075529
- TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/darwin-x86_64
- API=21
-
- echo "进入FFmpeg编译脚本"
-
- function build_android
- {
- echo "Compiling FFmpeg for $CPU"
- ./configure \
- --prefix=$PREFIX \
- --libdir=$LIB_DIR \
- --enable-shared \
- --disable-static \
- --enable-jni \
- --disable-doc \
- --disable-symver \
- --disable-programs \
- --target-os=android \
- --arch=$ARCH \
- --cpu=$CPU \
- --cc=$CC \
- --cxx=$CXX \
- --enable-cross-compile \
- --sysroot=$SYSROOT \
- --extra-cflags="-Os -fpic $OPTIMIZE_CFLAGS" \
- --extra-ldflags="$ADDI_LDFLAGS" \
- --disable-asm \
- $COMMON_FF_CFG_FLAGS
- make clean
- make -j8 # 这里是定义用几个CPU编译
- make install
- echo "The Compilation of FFmpeg for $CPU is completed"
- }
-
- #################### armv7-a ################################
-
- echo "开始编译FFmpeg(armeabi-v7a)"
- OUTPUT_FOLDER="armeabi-v7a"
- ARCH="arm"
- CPU="armv7-a"
- TOOL_CPU_NAME=armv7a
- TOOL_PREFIX="$TOOLCHAIN/bin/arm-linux-androideabi"
-
- CC="$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang"
- CXX="$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang++"
- SYSROOT="$NDK/toolchains/llvm/prebuilt/darwin-x86_64/sysroot"
- PREFIX="${PWD}/android/$OUTPUT_FOLDER"
- LIB_DIR="${PWD}/android/libs/$OUTPUT_FOLDER"
- OPTIMIZE_CFLAGS="-march=$CPU"
- build_android
-
-
- #################### armv8-a ################################
-
- echo "开始编译FFmpeg(arm64-v8a)"
- OUTPUT_FOLDER="arm64-v8a"
- ARCH=arm64
- CPU="armv8-a"
- TOOL_CPU_NAME=aarch64
- TOOL_PREFIX="$TOOLCHAIN/bin/$TOOL_CPU_NAME-linux-android"
-
- CC="$TOOL_PREFIX$API-clang"
- CXX="$TOOL_PREFIX$API-clang++"
- SYSROOT="$NDK/toolchains/llvm/prebuilt/darwin-x86_64/sysroot"
- PREFIX="${PWD}/android/$OUTPUT_FOLDER"
- LIB_DIR="${PWD}/android/libs/$OUTPUT_FOLDER"
- OPTIMIZE_CFLAGS="-march=$CPU"
- build_android
-
- #################### x86 ################################
-
- # echo "开始编译FFmpeg(x86)"
- # OUTPUT_FOLDER="x86"
- # ARCH="x86"
- # CPU="x86"
- # TOOL_CPU_NAME="i686"
- # TOOL_PREFIX="$TOOLCHAIN/bin/${TOOL_CPU_NAME}-linux-android"
-
- # CC="$TOOL_PREFIX$API-clang"
- # CXX="$TOOL_PREFIX$API-clang++"
- # SYSROOT="$NDK/toolchains/llvm/prebuilt/darwin-x86_64/sysroot"
- # PREFIX="${PWD}/android/$OUTPUT_FOLDER"
- # LIB_DIR="${PWD}/android/libs/$OUTPUT_FOLDER"
- # OPTIMIZE_CFLAGS="-march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32"
- # build_android
-
- #################### x86_64 ################################
-
- # echo "开始编译FFmpeg(x86_64)"
- # OUTPUT_FOLDER="x86_64"
- # ARCH="x86_64"
- # CPU="x86-64"
- # TOOL_CPU_NAME="x86_64"
- # TOOL_PREFIX="$TOOLCHAIN/bin/${TOOL_CPU_NAME}-linux-android"
-
- # CC="$TOOL_PREFIX$API-clang"
- # CXX="$TOOL_PREFIX$API-clang++"
- # SYSROOT="$NDK/toolchains/llvm/prebuilt/darwin-x86_64/sysroot"
- # PREFIX="${PWD}/android/$OUTPUT_FOLDER"
- # LIB_DIR="${PWD}/android/libs/$OUTPUT_FOLDER"
- # OPTIMIZE_CFLAGS="-march=$CPU"
- # build_android
编译好之后得到库文件
添加ffmpeg-config.cmake文件,内容如下(针对armv7)
- set(ffmpeg_path "${CMAKE_CURRENT_LIST_DIR}")
-
- message("ffmpeg_path: ${ffmpeg_path}")
-
- set(FFMPEG_LIBDIR "/opt/code/ffmpeg/android/libs/armeabi-v7a")
- set(FFMPEG_INCLUDE_DIRS "/opt/code/ffmpeg/android/armeabi-v7a/include")
-
- # library names
- set(FFMPEG_LIBRARIES
- ${FFMPEG_LIBDIR}/libavformat.so
- ${FFMPEG_LIBDIR}/libavdevice.so
- ${FFMPEG_LIBDIR}/libavcodec.so
- ${FFMPEG_LIBDIR}/libavutil.so
- ${FFMPEG_LIBDIR}/libswscale.so
- ${FFMPEG_LIBDIR}/libswresample.so
- ${FFMPEG_LIBDIR}/libavfilter.so
- )
-
- # found status
- set(FFMPEG_libavformat_FOUND TRUE)
- set(FFMPEG_libavdevice_FOUND TRUE)
- set(FFMPEG_libavcodec_FOUND TRUE)
- set(FFMPEG_libavutil_FOUND TRUE)
- set(FFMPEG_libswscale_FOUND TRUE)
- set(FFMPEG_libswresample_FOUND TRUE)
- set(FFMPEG_libavfilter_FOUND TRUE)
-
- # library versions, 注意这几个变量,一定要设置为全局CACHE变量
- set(FFMPEG_libavutil_VERSION 56.31.100 CACHE INTERNAL "FFMPEG_libavutil_VERSION") # info
- set(FFMPEG_libavcodec_VERSION 58.54.100 CACHE INTERNAL "FFMPEG_libavcodec_VERSION") # info
- set(FFMPEG_libavformat_VERSION 58.29.100 CACHE INTERNAL "FFMPEG_libavformat_VERSION") # info
- set(FFMPEG_libavdevice_VERSION 58.8.100 CACHE INTERNAL "FFMPEG_libavdevice_VERSION") # info
- set(FFMPEG_libavfilter_VERSION 7.57.100 CACHE INTERNAL "FFMPEG_libavfilter_VERSION") # info
- set(FFMPEG_libswscale_VERSION 5.5.100 CACHE INTERNAL "FFMPEG_libswscale_VERSION") # info
- set(FFMPEG_libswresample_VERSION 3.5.100 CACHE INTERNAL "FFMPEG_libswresample_VERSION") # info
-
- set(FFMPEG_FOUND TRUE)
- set(FFMPEG_LIBS ${FFMPEG_LIBRARIES})
-
- status(" #################################### FFMPEG:" FFMPEG_FOUND THEN "YES (find_package)" ELSE "NO (find_package)")
- status(" avcodec:" FFMPEG_libavcodec_VERSION THEN "YES (${FFMPEG_libavcodec_VERSION})" ELSE NO)
- status(" avformat:" FFMPEG_libavformat_VERSION THEN "YES (${FFMPEG_libavformat_VERSION})" ELSE NO)
- status(" avutil:" FFMPEG_libavutil_VERSION THEN "YES (${FFMPEG_libavutil_VERSION})" ELSE NO)
- status(" swscale:" FFMPEG_libswscale_VERSION THEN "YES (${FFMPEG_libswscale_VERSION})" ELSE NO)
- status(" avresample:" FFMPEG_libavresample_VERSION THEN "YES (${FFMPEG_libavresample_VERSION})" ELSE NO)
- git clone https://gitee.com/code-github/opencv_opencv.git
-
- * 4.x f7ac724c5d Merge pull request #21531 from fengyuentau:add_3rdparty_mirrors
-
- git branch -r
- origin/2.4
- origin/3.4
- origin/4.x
- origin/5.x
- origin/HEAD -> origin/4.x #使用的是4.x分支
- origin/master
- origin/next
-
需要修改两个地方
需要准备SDk和NDK的路径
-
- cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake -DCMAKE_ANDROID_NDK=$ANDROID_NDK \
- -DANDROID_NATIVE_API_LEVEL=android-21 \
- -DCMAKE_BUILD_TYPE=Release \
- -DCMAKE_INSTALL_PREFIX=/opt/code/opencv-4.x/ \
- -DBUILD_JAVA=OFF \
- -DANDROID_SDK_TOOLS=$ANDROID_SDK/build-tools/29.0.2 \
- -DBUILD_ANDROID_EXAMPLES=OFF \
- -DCMAKE_EXPORT_COMPILE_COMMANDS=OFF \
- -DBUILD_SHARED_LIBS=ON \
- -DWITH_FFMPEG=ON \
- -DBUILD_FAT_JAVA_LIB=OFF \
- -DOPENCV_GENERATE_PKGCONFIG=ON \
- -DOPENCV_FFMPEG_USE_FIND_PACKAGE=ON \
- -DOPENCV_FFMPEG_SKIP_BUILD_CHECK=ON \
- -DFFMPEG_DIR=/opt/code/ffmpeg/android/ ..
最后可以看到已经关联了FFmpeg
- --
- -- Video I/O:
- -- FFMPEG: YES (find_package)
- -- avcodec: YES (58.54.100)
- -- avformat: YES (58.29.100)
- -- avutil: YES (56.31.100)
- -- swscale: YES (5.5.100)
- -- avresample: NO
- --
- -- Parallel framework: pthreads
- --
- -- Trace: YES (with Intel ITT)
- --
- -- Other third-party libraries:
- -- Custom HAL: YES (carotene (ver 0.0.1))
- -- Protobuf: build (3.19.1)
- --
- -- Python (for build): /opt/homebrew/bin/python3
- --
- -- Install to: /opt/code/opencv-4.x
- make -j10
- make install
参考:
opencv+ffmpeg编译打包全解指南 - 知乎 (zhihu.com) Android最简单的基于FFmpeg的例子(一)---编译FFmpeg类库 | binglingziyu的博客