Repository: opencv
Issue: cv2.VideoCapture(pipeline, cv2.CAP_GSTREAMER) Error
Labels: bug

### System Information

CMake：3.28.0-rcl
GStreamer：1.22.6 MSVC 64bit
Opencv：4.8.0
Python version: 3.11.4
Operating System: Windows 10

### Detailed description

I used cmake to compile openCV that supports gstreamer. Configuration is：
```General configuration for OpenCV 4.8.0 =====================================
  Version control:               unknown

  Extra modules:
    Location (extra):            C:/Users/admin/Desktop/study/compile-cv2/opencv-source/opencv_contrib-4.8.0/modules
    Version control (extra):     unknown

  Platform:
    Timestamp:                   2024-02-27T02:45:21Z
    Host:                        Windows 10.0.19045 AMD64
    CMake:                       3.28.0-rc1
    CMake generator:             Visual Studio 17 2022
    CMake build tool:            C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/amd64/MSBuild.exe
    MSVC:                        1937
    Configuration:               Debug Release

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3 SSSE3
      requested:                 SSSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_1 (17 files):         + SSE4_1
      SSE4_2 (2 files):          + SSE4_1 POPCNT SSE4_2
      FP16 (1 files):            + SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (8 files):             + SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (37 files):           + SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
      AVX512_SKX (8 files):      + 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/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.37.32822/bin/Hostx64/x64/cl.exe  (ver 19.37.32825.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/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.37.32822/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:         YES
    Extra dependencies:
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dnn_superres dpm face features2d flann fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
    Disabled:                    java_bindings_generator js_bindings_generator world
    Disabled by dependency:      -
    Unavailable:                 alphamat cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv freetype hdf java julia matlab ovis python2 sfm viz
    Applications:                tests perf_tests apps
    Documentation:               NO
    Non-free algorithms:         YES

  Windows RT support:            NO

  GUI:                           WIN32UI
    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)
      avdevice:                  NO
    GStreamer:                   YES (1.22.6)
    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:/Users/admin/Desktop/study/compile-cv2/opencv-source/opencv-4.8.0/build/3rdparty/ippicv/ippicv_win/icv
    Intel IPP IW:                sources (2021.8.0)
              at:                C:/Users/admin/Desktop/study/compile-cv2/opencv-source/opencv-4.8.0/build/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/admin/Desktop/study/compile-cv2/opencv-source/opencv-4.8.0/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python 3:
    Interpreter:                 C:/Users/admin/anaconda3/envs/jdswj/python.exe (ver 3.11.3)
    Libraries:                   C:/Users/admin/anaconda3/libs/python311.lib (ver 3.11.3)
    numpy:                       C:/Users/admin/anaconda3/envs/jdswj/Lib/site-packages/numpy/core/include (ver 1.24.3)
    install path:                C:/Users/admin/anaconda3/envs/jdswj/Lib/site-packages/cv2/python-3.11

  Python (for build):            C:/Users/admin/anaconda3/envs/jdswj/python.exe

  Install to:                    C:/Users/admin/Desktop/study/compile-cv2/opencv-source/opencv-4.8.0/build/install
```

### While using it, I failed。Here is my demo。

```import os
import cv2
import ctypes

## os.add_dll_directory(r"C:\gstreamer\1.0\msvc_x86_64\bin")

## load dll
dll1 = "./gstgio.dll"
dll2 = './gsttcp.dll'
my_dll1 = ctypes.CDLL(dll1)
my_dll2 = ctypes.CDLL(dll2)
## dll3 = './gsttypefindfunctions.dll'
## my_dll3 = ctypes.WinDLL(dll3)

pipeline = "rtspsrc location=rtsp://my-url ! rtph264depay ! h264parse ! avdec_h264 ! videoconvert ! appsink"
cap = cv2.VideoCapture(pipeline, cv2.CAP_GSTREAMER)

while True:
    ret, frame = cap.read()
    if ret:
        frame_new = cv2.resize(frame, (640, 480))
        cv2.imshow('frame', frame_new)
    else:
        print("ret false")
        break
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
```

If not load dll，the error is：  and block。
```(python.exe:19968): GStreamer-WARNING **: 10:02:15.827: Failed to load plugin 'C:\Users\admin\Anaconda3\Library\lib\gstreamer-1.0\gstgio.dll': 'C:\Users\admin\Anaconda3\Library\lib\gstreamer-1.0\gstgio.dll': �Ҳ���ָ���ĳ���

(python.exe:19968): GStreamer-WARNING **: 10:02:15.829: Failed to load plugin 'C:\Users\admin\Anaconda3\Library\lib\gstreamer-1.0\gsttcp.dll': 'C:\Users\admin\Anaconda3\Library\lib\gstreamer-1.0\gsttcp.dll': �Ҳ���ָ���ĳ���
(python.exe:35280): GStreamer-WARNING **: 10:05:26.387: Failed to load plugin 'C:\Users\admin\Anaconda3\Library\lib\gstreamer-1.0\gsttypefindfunctions.dll': 'C:\Users\admin\Anaconda3\Library\lib\gstreamer-1.0\gsttypefindfunctions.dll': �Ҳ���ָ���ĳ���
```

But  I can find these dll with path：C:\Users\admin\Anaconda3\Library\lib\gstreamer-1.0。And anyother dlls in this path could be loaded successfully。Then，I find these dlls and copy them to my code‘s dictionary，and add the code to load dll。dll1 & dll2 are working，but dll3 is still wrong：
```my_dll3 = ctypes.WinDLL(dll3)
              ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\admin\anaconda3\envs\jdswj\Lib\ctypes\__init__.py", line 376, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [WinError 127] 找不到指定的程序。
```

I deleted C:\Users\admin\Anaconda3\Library\lib\gstreamer-1.0\gsttypefindfunctions.dll，and use the code above，error is：
```[ WARN:0@30.032] global cap_gstreamer.cpp:1697 cv::GStreamerCapture::open OpenCV | GStreamer warning: unable to query duration of stream

(python.exe:32464): GStreamer-CRITICAL **: 10:11:58.583: gst_caps_get_structure: assertion 'GST_IS_CAPS (caps)' failed

(python.exe:32464): GStreamer-CRITICAL **: 10:11:58.583: gst_structure_get_int: assertion 'structure != NULL' failed
[ WARN:0@30.032] global cap_gstreamer.cpp:1707 cv::GStreamerCapture::open OpenCV | GStreamer warning: cannot query video width/height

(python.exe:32464): GStreamer-CRITICAL **: 10:11:58.583: gst_structure_get_fraction: assertion 'structure != NULL' failed
[ WARN:0@30.032] global cap_gstreamer.cpp:1713 cv::GStreamerCapture::open OpenCV | GStreamer warning: cannot query video fps
[ WARN:0@30.032] global cap_gstreamer.cpp:1728 cv::GStreamerCapture::open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1

(python.exe:32464): GStreamer-CRITICAL **: 10:13:03.603: gst_sample_get_caps: assertion 'GST_IS_SAMPLE (sample)' failed
[ERROR:0@95.052] global cap_gstreamer.cpp:925 cv::GStreamerCapture::retrieveVideoFrame GStreamer: gst_sample_get_caps() returns NULL
```


I tried
 ```gst-launch-1.0 rtspsrc location=rtsp://my-url latency=20 ! rtph264depay ! h264parse ! avdec_h264 ! autovideosink
```
 in terminal and cmd。Both worked well。

Now， I have no idea to solve this problem。Could anyone help me？

### Steps to reproduce

```import cv2
import ctypes

dll1 = "./gstgio.dll"
dll2 = './gsttcp.dll'
my_dll1 = ctypes.CDLL(dll1)
my_dll2 = ctypes.CDLL(dll2)
# dll3 = './gsttypefindfunctions.dll'
# my_dll3 = ctypes.WinDLL(dll3)

pipeline = "rtspsrc location=rtsp://my-url ! rtph264depay ! h264parse ! avdec_h264 ! videoconvert  appsink"
cap = cv2.VideoCapture(pipeline, cv2.CAP_GSTREAMER)

while True:
    ret, frame = cap.read()
    if ret:
        frame_new = cv2.resize(frame, (640, 480))
        cv2.imshow('frame', frame_new)
    else:
        print("ret false")
        break
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
```

### Issue submission checklist

- [X] I report the issue, it's not a question
- [X] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [ ] I updated to the latest OpenCV version and the issue is still there
- [X] There is reproducer code and related data files (videos, images, onnx, etc)