Configure and Build OpenCV to Custom FFMPEG Install

前端 未结 4 1168
梦毁少年i
梦毁少年i 2021-02-05 16:37

I cannot seem to configure OpenCV to link to a non-/usr/lib set of FFMPEG libraries.

My LD_LIBRARY_PATH contains a direct link to the folder for the custom

相关标签:
4条回答
  • 2021-02-05 17:01

    For OpenCV 3.x and ffmpeg 3.x, I have to apply the following patch

    diff --git a/cmake/OpenCVFindLibsVideo.cmake b/cmake/OpenCVFindLibsVideo.cmake
    index 13b62ac..bab9df3 100644
    --- a/cmake/OpenCVFindLibsVideo.cmake
    +++ b/cmake/OpenCVFindLibsVideo.cmake
    @@ -228,6 +228,12 @@ if(WITH_FFMPEG)
         if(FFMPEG_libavresample_FOUND)
           ocv_append_build_options(FFMPEG FFMPEG_libavresample)
         endif()
    +   CHECK_MODULE(libavcodec HAVE_FFMPEG)
    +   CHECK_MODULE(libavformat HAVE_FFMPEG)
    +   CHECK_MODULE(libavutil HAVE_FFMPEG)
    +   CHECK_MODULE(libswscale HAVE_FFMPEG)
    +   CHECK_MODULE(libswresample HAVE_FFMPEG)
    +   CHECK_MODULE(libavresample HAVE_FFMPEG)
         if(HAVE_FFMPEG)
           try_compile(__VALID_FFMPEG
               "${OpenCV_BINARY_DIR}"
    

    And with the following script to build

    #!/bin/bash
    
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/Applications/ffmpeg/lib
    export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$HOME/Applications/ffmpeg/lib/pkgconfig
    export PKG_CONFIG_LIBDIR=$PKG_CONFIG_LIBDIR:$HOME/Applications/ffmpeg/lib
    
    cmake \
        -D BUILD_EXAMPLES=ON \
        -D BUILD_TESTS=OFF \
        -D OPENCV_EXTRA_EXE_LINKER_FLAGS="-Wl,-rpath,$HOME/Applications/ffmpeg/lib" \
        -D CMAKE_BUILD_TYPE=RELEASE \
        -D CMAKE_INSTALL_PREFIX=$HOME/Applications/opencv \
        /path/to/opencv
    

    Also, when build ffmpeg, I have to enable flags --enable-avresample.

    0 讨论(0)
  • 2021-02-05 17:02

    It's years after the question was asked, but I recently ran into the kinds of issues described in this question! I'd like to add my input.

    First: I only built the static ffmpeg libraries (for reasons), which the opencv build process is somewhat hostile towards. There have been posts by opencv developers stating that they don't support an opencv build against static ffmpeg libraries, but my thinking was "what if you're also building static opencv libraries? Certainly that should be supported?"

    And the answer is yes, you can compile static opencv libraries against static ffmpeg libraries! I did this with opencv 4.1.1 against ffmpeg 4.2. I had to use the following cmake options:

    cmake3 \
    -D BUILD_opencv_apps=OFF \
    -D BUILD_opencv_python2=OFF \
    -D BUILD_SHARED_LIBS=OFF \
    -D WITH_FFMPEG=ON \
    -D OPENCV_FFMPEG_SKIP_BUILD_CHECK=ON \
    

    These options result in a set of opencv static libraries that include whatever ffmpeg static libraries that you've also built, assuming your PKG_CONFIG_PATH is setup to correctly find those libraries (or if you've installed them to some default system location).

    Q/A:

    • Why can't we build apps?
      • The opencv build process assumes that you are building and linking against shared libraries, even if you only build static libraries. This seems like a shortcoming in the build process rather than a technical limitation; perhaps some day this will be fixed.
    • What is the OPENCV_FFMPEG_SKIP_BUILD_CHECK flag?
      • There is a step in ffmpeg's cmake process where an application is built and linked using the same faulty build process as opencv's other applications. It's faulty in the sense that the process assumes that you're linking against shared libraries when it doesn't need to; you can perform the same build steps yourself while not making that assumption and create a working application. Thus, this build check needs to be turned off in order for opencv to accept that yes, in fact, any static libraries it finds are okay to use. This is
    • And why do you disable python support?
      • The opencv python build process explicitly produces a shared library loadable by Python.
    • Does this imply that you're still building the tests suite even with the static libraries?
      • Yes. But I haven't tried running them
    0 讨论(0)
  • 2021-02-05 17:02

    In addition to Andrey's answer:

    I found that (for my configuration) the opencv 4.1.1 build-system did not correctly add the ffmpeg library path (though it did set the include directory.) My workaround was to supply these paths with the following cmake build arguments:

    -DCMAKE_SHARED_LINKER_FLAGS=" -Wl,-rpath-link $FFMPEG_LIBDIR -L$FFMPEG_LIBDIR"
    -DCMAKE_MODULE_LINKER_FLAGS=" -Wl,-rpath-link $FFMPEG_LIBDIR -L$FFMPEG_LIBDIR"
    -DCMAKE_EXE_LINKER_FLAGS=" -Wl,-rpath-link $FFMPEG_LIBDIR -L$FFMPEG_LIBDIR"
    

    where $FFMPEG_LIBDIR is the path to your ffmpeg library binaries. It was necessary to use the -rpath-link otpion, because the build system didn't supply all necessary ffmpeg library arguments for my ffmpeg installation.

    Examining modules/videoio/cmake/detect_ffmpeg.cmake, it looks like FFMPEG can also be coerced to accept user supplied library paths by specifying:

    -DHAVE_FFMPEG=ON
    -DFFMPEG_INCLUDE_DIRS=(path to your ffmpeg include directories)
    -DFFMPEG_LIBRARIES=(list of ffmpeg libraries, e.g. -lavcodec)
    

    But if we perform this method, the build system displays "YES" to FFMPEG, but "NO" to the individual modules (didn't debug further.)

    Some users will find that they may need to supply the --sysroot linker argument in the linker flags, or specify -DCMAKE_SYSROOT as a build argument

    Hope this helps someone :)

    0 讨论(0)
  • 2021-02-05 17:03

    Something like

    export LD_LIBRARY_PATH=/ffmpeg_install_path/lib/
    export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/ffmpeg_install_path/lib/pkgconfig
    export PKG_CONFIG_LIBDIR=$PKG_CONFIG_LIBDIR:/ffmpeg_install_path/lib/
    

    should work. At least it works for OpenCV 2.4.x on my Ubuntu.

    0 讨论(0)
提交回复
热议问题