移动GStreamer DLL至程序目录后OpenCV4出现GStreamer运行时错误
OpenCV + GStreamer 移动DLL后运行异常问题排查与解决建议
环境与版本
Windows 10、Visual Studio 2019、C++、OpenCV 4.8、GStreamer 1.22.5
问题描述
用Dependencies工具查看OpenCV 4.8链接的GStreamer DLL列表,当DLL位于GStreamer安装目录时,OpenCV的GStreamer视频I/O功能运行正常。
但将C:\gstreamer\1.0\msvc_x86_64\bin下的所有文件移动到可执行文件(HILLAB Beam Stabilization System.exe)所在目录后,出现异常。
OpenCV抛出的断言信息如下:
[ WARN:0@6.595] global cap_gstreamer.cpp:2196 cv::CvVideoWriter_GStreamer::close_ OpenCV | GStreamer warning: No source in GStreamer pipeline. Ignore [ERROR:0@6.596] global cap.cpp:645 cv::VideoWriter::open VIDEOIO(GSTREAMER): raised OpenCV exception: OpenCV(4.8.0) C:\Users\woose\Downloads\opencv-4.8.0\opencv-4.8.0\modules\videoio\src\cap_gstreamer.cpp:153: error: (-215:Assertion failed) ptr in function 'cv::`anonymous-namespace'::GSafePtr<struct _GstElement>::get'
这是OpenCV的Bug吗?恳请提供解决建议。
OpenCV构建:GStreamer库路径

OpenCV构建:CMake配置结果
Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19045. ocv_init_download: OpenCV source tree is not fetched as git repository. 3rdparty resources will be downloaded from github.com by default. Detected processor: AMD64 Could NOT find PythonInterp (missing: PYTHON_EXECUTABLE) (Required is at least version "2.7") libjpeg-turbo: VERSION = 2.1.3, BUILD = opencv-4.8.0-libjpeg-turbo libjpeg-turbo(SIMD): SIMD extensions disabled: could not find NASM compiler. Performance will suffer. Could NOT find OpenJPEG (minimal suitable version: 2.0, recommended version >= 2.3.1). OpenJPEG will be built from sources OpenJPEG: VERSION = 2.5.0, BUILD = opencv-4.8.0-openjp2-2.5.0 OpenJPEG libraries will be built from sources: libopenjp2 (version "2.5.0") found Intel IPP (ICV version): 2021.8.0 [2021.8] at: C:/opencv/4.8.0/3rdparty/ippicv/ippicv_win/icv found Intel IPP Integration Wrappers sources: 2021.8.0 at: C:/opencv/4.8.0/3rdparty/ippicv/ippicv_win/iw Could not find OpenBLAS include. Turning OpenBLAS_FOUND off Could not find OpenBLAS lib. Turning OpenBLAS_FOUND off Could NOT find BLAS (missing: BLAS_LIBRARIES) Could NOT find LAPACK (missing: LAPACK_LIBRARIES) Reason given by package: LAPACK could not be found because dependency BLAS could not be found. Could NOT find Java (missing: Java_JAR_EXECUTABLE Java_JAVAC_EXECUTABLE Java_JAVAH_EXECUTABLE Java_JAVADOC_EXECUTABLE) (found version "1.8.0_251") Could NOT find JNI (missing: JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH) VTK is not found. Please set -DVTK_DIR in CMake to VTK build directory, or to VTK install subdirectory with VTKConfig.cmake file Processing WORLD modules... module opencv_core... Allocator metrics storage type: 'long long' module opencv_imgproc... module opencv_imgcodecs... imgcodecs: OpenEXR codec is disabled in runtime. Details: https://github.com/opencv/opencv/issues/21326 module opencv_videoio... module opencv_highgui... highgui: using builtin backend: WIN32UI Processing WORLD modules... DONE Excluding from source files list: modules/imgproc/src/imgwarp.lasx.cpp Excluding from source files list: modules/imgproc/src/resize.lasx.cpp General configuration for OpenCV 4.8.0 ===================================== Version control: unknown Platform: Timestamp: 2023-09-20T03:55:22Z Host: Windows 10.0.19045 AMD64 CMake: 3.22.0 CMake generator: Visual Studio 16 2019 CMake build tool: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/MSBuild/Current/Bin/MSBuild.exe MSVC: 1929 Configuration: Debug Release CPU/HW features: Baseline: SSE SSE2 SSE3 requested: SSE3 Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX SSE4_1 (13 files): + SSSE3 SSE4_1 SSE4_2 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 (0 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX AVX (3 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX AVX2 (25 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX512_SKX (2 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX C/C++: Built as dynamic libs?: YES C++ standard: 11 C++ Compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe (ver 19.29.30147.0) C++ flags (Release): /DWIN32 /D_WINDOWS /W4 /GR /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:precise /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /wd4819 /MP /MD /O2 /Ob2 /DNDEBUG C++ flags (Debug): /DWIN32 /D_WINDOWS /W4 /GR /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:precise /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /wd4819 /MP /MDd /Zi /Ob0 /Od /RTC1 C Compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe C flags (Release): /DWIN32 /D_WINDOWS /W3 /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:precise /MP /MD /O2 /Ob2 /DNDEBUG C flags (Debug): /DWIN32 /D_WINDOWS /W3 /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:precise /MP /MDd /Zi /Ob0 /Od /RTC1 Linker flags (Release): /machine:x64 /INCREMENTAL:NO Linker flags (Debug): /machine:x64 /debug /INCREMENTAL ccache: NO Precompiled headers: NO Extra dependencies: 3rdparty dependencies: OpenCV modules: To be built: core highgui imgcodecs imgproc videoio world Disabled: calib3d dnn features2d flann gapi java_bindings_generator js_bindings_generator ml objc_bindings_generator objdetect photo python_bindings_generator python_tests stitching ts video Disabled by dependency: - Unavailable: java python2 python3 Applications: apps Documentation: NO Non-free algorithms: NO Windows RT support: NO GUI: Win32 UI: YES VTK support: NO Media I/O: ZLib: build (ver 1.2.13) JPEG: build-libjpeg-turbo (ver 2.1.3-62) SIMD Support Request: YES SIMD Support: NO WEBP: build (ver encoder: 0x020f) PNG: build (ver 1.6.37) TIFF: build (ver 42 - 4.2.0) JPEG 2000: build (ver 2.5.0) OpenEXR: build (ver 2.3.0) HDR: YES SUNRASTER: YES PXM: YES PFM: YES Video I/O: DC1394: NO FFMPEG: YES (prebuilt binaries) avcodec: YES (58.134.100) avformat: YES (58.76.100) avutil: YES (56.70.100) swscale: YES (5.9.100) avresample: YES (4.0.0) GStreamer: YES (1.22.5) DirectShow: YES Media Foundation: YES DXVA: YES Parallel framework: Concurrency Trace: YES (with Intel ITT) Other third-party libraries: Intel IPP: 2021.8 [2021.8.0] at: C:/opencv/4.8.0/3rdparty/ippicv/ippicv_win/icv Intel IPP IW: sources (2021.8.0) at: C:/opencv/4.8.0/3rdparty/ippicv/ippicv_win/iw Lapack: NO Eigen: NO Custom HAL: NO Protobuf: build (3.19.1) Flatbuffers: builtin/3rdparty (23.5.9) OpenCL: YES (NVD3D11) Include path: C:/Users/woose/Downloads/opencv-4.8.0/opencv-4.8.0/3rdparty/include/opencl/1.2 Link libraries: Dynamic load Python (for build): C:/Users/woose/AppData/Local/Programs/Python/Python310/python.exe Java: ant: NO Java: NO JNI: NO Java wrappers: NO Java tests: NO Install to: C:/opencv/4.8.0/install ----------------------------------------------------------------- Configuring done
解决建议
- 不要仅复制bin目录文件:GStreamer运行不仅依赖bin下的DLL,还需要
C:\gstreamer\1.0\msvc_x86_64\lib目录下的库文件,以及share目录下的插件元数据、配置文件等。直接移动bin文件会导致GStreamer无法加载必要的插件或读取配置。 - 保留GStreamer目录结构:建议将整个
C:\gstreamer\1.0\msvc_x86_64目录复制到可执行文件所在路径的子目录(比如./gstreamer),然后在程序启动时设置环境变量GST_PLUGIN_PATH指向该子目录的lib\gstreamer-1.0路径,同时将./gstreamer/bin加入系统PATH。 - 检查插件依赖:用Dependencies工具检查GStreamer插件(比如
libgstcoreelements.dll等)是否能正确加载,确保所有依赖的DLL都存在于可执行文件路径或PATH环境变量中。 - 指定GStreamer运行路径:在代码中通过
putenv或SetEnvironmentVariable设置GST_PLUGIN_PATH和PATH,确保GStreamer能找到插件和依赖库。示例代码:#include <cstdlib> #ifdef _WIN32 #include <windows.h> #endif int main() { // 设置GStreamer插件路径 #ifdef _WIN32 SetEnvironmentVariableA("GST_PLUGIN_PATH", "./gstreamer/lib/gstreamer-1.0"); // 将GStreamer的bin目录加入PATH char path[1024]; GetEnvironmentVariableA("PATH", path, sizeof(path)); strcat(path, ";./gstreamer/bin"); SetEnvironmentVariableA("PATH", path); #else putenv("GST_PLUGIN_PATH=./gstreamer/lib/gstreamer-1.0"); putenv("PATH=$PATH:./gstreamer/bin"); #endif // 后续OpenCV代码 return 0; } - 排查OpenCV编译配置:确认CMake构建OpenCV时,GStreamer的
GSTREAMER_INCLUDE_DIR和GSTREAMER_LIBRARY路径正确指向完整的GStreamer安装目录,而非仅bin目录。
内容的提问来源于stack exchange,提问作者wsys
相关产品推荐
相关产品推荐

