Linux 下交叉编译opencv3.4.16 aarch64 基于 FFMPEG 的 VideoIO模块

Linux 下交叉编译opencv3.4.16 aarch64 基于 FFMPEG 的 VideoIO模块

  • Prepare:
  • 注:
  • 编译ffmpeg
    • 编译x264
    • 编译x265
    • 编译openh265
    • 编译ffmpeg
      • FAQs
  • 编译opencv
    • 修改CMakeClists.txt
    • 修改cmake/OpenCVFindLibsVideo.cmake

Prepare:

下载opencv、ffmpeg 、交叉编译工具
本文不介绍交叉编译工具环境的设置
opencv3.4.16 对应的ffmpeg版本的是3.4.12
具体不要复制粘贴,按照大概的步骤操作就行了

注:

在CMakeLists.txt中设置CMAKE_C_COMPILERCMAKE_CXX_COMPILER这两个变量的时候会出现无限递归的情况,解决办法是把这两个变量放在project下面

编译ffmpeg

根据需求编译x264 x265 openh264

#指定gcc	也可以指定绝对路径
export CC=aarch64-linux-gnu-gcc
export AR=aarch64-linux-gnu-ar

编译x264

./configure --enable-static --enable-shared --disable-asm --prefix=./build --host=aarch64-linux
make
make 

编译x265

git clone https://github.com/videolan/x265.git
cd x265/build/aarch64-linux #根据平台进入目录
#修改make-Makefiles.bash
cmake -DCMAKE_INSTALL_PREFIX=./build -DCMAKE_TOOLCHAIN_FILE="crosscompile.cmake" -G "Unix Makefiles" ../../source 
./make-Makefiles.bash

编译openh265

git clone https://github.com/cisco/openh264.git
cd openh265
#DESTDIR或者PREFIX 指定install目录
#根据平台指定OS, Android:OS=android
make -B OS=linux CC=aarch64-linux-gnu-gcc CXX=aarch64-linux-gnu-g++ ARCH=aarch64 -j8 DESTDIR=/home/ubuntu/tang/opencv/openh264/out
make install DESTDIR=/home/ubuntu/tang/opencv/openh264/out

编译ffmpeg

./configure \
--prefix=./build \
--target-os=linux \
--enable-shared \
--enable-ffmpeg \
--enable-cross-compile \
--enable-ffplay \
--enable-ffprobe \
--enable-demuxer=rtsp \
--enable-demuxer=rtp \
--enable-protocol=tcp \
--enable-protocol=udp \
--enable-network \
--disable-doc \
--disable-avdevice \
--disable-doc \
--disable-symver \
--disable-asm \
--arch=aarch64 \
--disable-stripping \
--enable-avresample \
--enable-small \
--enable-gpl \
--disable-debug \
--cc=$CC \
--as=$AS \
--extra-cflags="-fpic -I/home/ubuntu/tang/opencv/ffmpeg-3.4.12/x264/include -I/home/ubuntu/tang/opencv/ffmpeg-3.4.12/x265/include -I/home/ubuntu/tang/opencv/ffmpeg-3.4.12/openh264/include" \
--extra-ldflags="-L/opt/gcc-ubuntu-9.3.0-2020.03-x86_64-aarch64-linux-gnu/aarch64-linux-gnu/lib -L/home/ubuntu/tang/opencv/ffmpeg-3.4.12/x264/lib -L/home/ubuntu/tang/opencv/ffmpeg-3.4.12/x265/lib -L/home/ubuntu/tang/opencv/ffmpeg-3.4.12/openh264/lib" 
--enable-libx264 \
--enable-libopenh264 \
--enable-libx265 \
--pkg-config-flags=--static \
--sysroot=/home/ubuntu/channeltools/gcc-ubuntu-9.3.0-2020.03-x86_64-aarch64-linux-gnu/aarch64-linux-gnu/lib

FAQs

–extra-cflags --extra-ldflags 指定编译的时候找到libx264 libopenh264 libx265
报错 x264 x265 openh264 pkg-config not find
修改 变量export PKG_CONFIG_PATH=$PKG_CONFIG_PATH: 编译好的x26*库pkgconfig目录
#查看错误日志ffbuild/config.log ,出现not find libstd++等标准库
#添加** --sysroot=/home/ubuntu/channeltools/gcc-ubuntu-9.3.0-2020.03-x86_64-aarch64-linux-gnu/aarch64-linux-gnu/lib **

编译opencv

修改CMakeClists.txt

在CMakeCLists.txt里面首行添加下面信息
指定一些交叉编译的信息

set(CMAKE_C_COMPILER /opt/gcc-ubuntu-9.3.0-2020.03-x86_64-aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER /opt/gcc-ubuntu-9.3.0-2020.03-x86_64-aarch64-linux-gnu/bin/aarch64-linux-gnu-g++)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)
SET(AARCH64 ON)
set(GCC_COMPILER_VERSION "" CACHE STRING "GCC Compiler version")
set(GNU_MACHINE "aarch64-linux-gnu" CACHE STRING "GNU compiler triple")
set(CMAKE_FIND_ROOT_PATH /opt/gcc-ubuntu-9.3.0-2020.03-x86_64-aarch64-linux-gnu/aarch64-linux-gnu/bin)
#set(CMAKE_SYSROOT /opt/gcc-ubuntu-9.3.0-2020.03-x86_64-aarch64-linux-gnu/aarch64-linux-gnu)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
#

#在这个地方把find_package(PkgConfig QUIET)提到if上
find_package(PkgConfig QUIET)
if(UNIX)
  if(NOT APPLE_FRAMEWORK OR OPENCV_ENABLE_PKG_CONFIG)                                                                                                                                                                           
    if(CMAKE_CROSSCOMPILING AND NOT DEFINED ENV{PKG_CONFIG_LIBDIR} AND NOT DEFINED ENV{PKG_CONFIG_SYSROOT_DIR}
        AND NOT OPENCV_ENABLE_PKG_CONFIG
    )    
      if(NOT PkgConfig_FOUND)
        message(STATUS "OpenCV disables pkg-config to avoid using of host libraries. Consider using PKG_CONFIG_LIBDIR to specify target SYSROOT")
      elseif(OPENCV_SKIP_PKG_CONFIG_WARNING)
        message(WARNING "pkg-config is enabled in cross-compilation mode without defining of PKG_CONFIG_LIBDIR environment variable. This may lead to misconfigured host-based dependencies.")
      endif()
    elseif(OPENCV_DISABLE_PKG_CONFIG)
      if(PkgConfig_FOUND)
        message(WARNING "OPENCV_DISABLE_PKG_CONFIG flag has no effect")
      endif()
    else()
      find_package(PkgConfig QUIET)
    endif()
  endif()
#在这个位置添加link_directories
#因为在编译的时候会链接不发到libx264等库,然后报undefind ****
link_directories(/home/ubuntu/tang/opencv/ffmpeg-3.4.12/build/ffmpeg/lib)
add_subdirectory(include)

修改cmake/OpenCVFindLibsVideo.cmake

需要手动指定ffmpeg 库路径

#注释下面测试编译,测试编译错误后至 HAVE_FFMPEG FALSE
if(HAVE_FFMPEG
    AND NOT HAVE_FFMPEG_WRAPPER
)
  try_compile(__VALID_FFMPEG
      "${OpenCV_BINARY_DIR}"
      "${OpenCV_SOURCE_DIR}/cmake/checks/ffmpeg_test.cpp"
      CMAKE_FLAGS "-DINCLUDE_DIRECTORIES:STRING=${FFMPEG_INCLUDE_DIRS}"
                  "-DLINK_DIRECTORIES:STRING=${FFMPEG_LIBRARY_DIRS}"
                  "-DLINK_LIBRARIES:STRING=${FFMPEG_LIBRARIES}"
      OUTPUT_VARIABLE TRY_OUT
  )
  if(NOT __VALID_FFMPEG)
    message(FATAL_ERROR "FFMPEG: test check build log:\n${TRY_OUT}")
      message(STATUS "WARNING: Can't build ffmpeg test code")
      set(HAVE_FFMPEG FALSE)
    else()
     ocv_append_build_options(VIDEOIO FFMPEG)
  endif()
endif()

#然后添加下面
set(FFMPEG_LIBRARY_DIRS /home/ubuntu/tang/opencv/ffmpeg-3.4.12/build/ffmpeg/lib)
set(pkgcfg_lib_FFMPEG_avformat ${FFMPEG_LIBRARY_DIRS}/libavformat.so.57)
set(pkgcfg_lib_FFMPEG_avcodec ${FFMPEG_LIBRARY_DIRS}/libavcodec.so.57)
set(pkgcfg_lib_FFMPEG_avutil ${FFMPEG_LIBRARY_DIRS}/libavutil.so.55)
set(pkgcfg_lib_FFMPEG_swscale ${FFMPEG_LIBRARY_DIRS}/libswscale.so.4)
set(pkgcfg_lib_FFMPEG_swresample ${FFMPEG_LIBRARY_DIRS}/libswresample.so.2.9.100)
set(FFMPEG_LIBRARIES ${pkgcfg_lib_FFMPEG_avformat} ${pkgcfg_lib_FFMPEG_avcodec} ${pkgcfg_lib_FFMPEG_avutil} ${pkgcfg_lib_FFMPEG_swscale} ${pkgcfg_lib_FFMPEG_swresample})
set(FFMPEG_LINK_LIBRARIES ${FFMPEG_LIBRARIES})
set(FFMPEG_LIBS ${FFMPEG_LINK_LIBRARIES})
set(FFMPEG_LIBRARIES ${FFMPEG_LINK_LIBRARIES})
set(pkgcfg_lib_FFMPEG_libavresample_avresample ${FFMPEG_LIBRARY_DIRS}/libavresample.so.3.7.0)
message("pkgcfg_lib_FFMPEG_avformat=======================":${pkgcfg_lib_FFMPEG_avformat})
message("pkgcfg_lib_FFMPEG_avcodec=======================":${pkgcfg_lib_FFMPEG_avcodec})
message("pkgcfg_lib_FFMPEG_avutil=======================":${pkgcfg_lib_FFMPEG_avutil})
message("pkgcfg_lib_FFMPEG_swscale=======================":${pkgcfg_lib_FFMPEG_swresample})
message("pkgcfg_lib_FFMPEG_libavresample_avresample=======================":${pkgcfg_lib_FFMPEG_libavresample_avresample})

# BUILD_opencv_world这个是编译生成一个大的集合so库
#WITH_1394=OFF 关掉,这个是一个camera的支持
#WITH_GTK=OFF 关掉,交叉编译,一般arm开发板没有图形库
mkdir build 
cd build
cmake -DCMAKE_INSTALL_PREFIX=./mm_install \
-D CMAKE_BUILD_TYPE=RELEASE \
-D WITH_FFMPEG=ON \
-D BUILD_FAT_JAVA_LIB=OFF \
-D BUILD_ANDROID_PROJECTS=OFF \
-D WITH_OPENCL=OFF \
-D BUILD_PNG=ON \
-D BUILD_JPEG=ON \
-D WITH_CUDA=OFF \
-D WITH_GSTREAMER=OFF \
-D WITH_1394=OFF \
-D WITH_GTK=OFF \
-D BUILD_SHARED_LIBS=ON  \
-D BUILD_opencv_world=ON ..

你可能感兴趣的:(openCV学习,C++笔记,ffmpeg,linux)