cv2.VideoCapture(udp_str,cv2.CAP_GSTREAMER) is not working

test_src is working ,but other str is not working,and have the same error log
the udp pipeline can recive by use commandline and softwore QGC

import cv2
# GStreamer pipeline string for UDP video stream

test_str = "videotestsrc  ! videoconvert ! appsink"
camera_str = "ksvideosrc ! video/x-raw,format=BGR ! videoconvert ! appsink"
udp_str = " udpsrc port=5600 " \
          "! application/x-rtp ! rtph264depay ! h264parse ! avdec_h264 " \
          "! decodebin ! videoconvert ! video/x-raw,format=(string)BGR ! videoconvert " \
          "! appsink sync=false"
udp_qt_str = " udpsrc port=5600 "\
             "! application/x-rtp,payload=96 ! rtph264depay ! h264parse ! avdec_h264 "\
             "! decodebin ! videoconvert ! video/x-raw,format=(string)BGR ! videoconvert "\
             "! appsink emit-signals=true sync=false max-buffers=1 drop=true"

print("ready capture")
# Create VideoCapture object with GStreamer pipeline string camera_str,cv2.CAP_GSTREAMER
cap = cv2.VideoCapture(test_str,cv2.CAP_GSTREAMER)
print("finish capture")
# Check if the camera opened successfully
if not cap.isOpened():
    print("Cannot open video stream")

while True:
    # Capture frame-by-frame
    ret, frame =

    # Check if frame is read correctly
    if not ret:
        print("Error reading frame")
    # Display the frame
    cv2.imshow("GStreamer UDP Video Stream", frame)

    # Check for 'q' key press
    if cv2.waitKey(1) & 0xFF == ord('q'):

# Release the VideoCapture object and close all windows

getBuildInformation log

General configuration for OpenCV 4.9.0 =====================================
  Version control:               unknown

  Extra modules:
    Location (extra):            E:/lctest/opencv/sources/opencv_contrib-4.9.0/modules
    Version control (extra):     unknown

    Timestamp:                   2024-03-06T08:29:39Z
    Host:                        Windows 10.0.22621 AMD64
    CMake:                       3.20.21032501-MSVC_2
    CMake generator:             Visual Studio 16 2019
    CMake build tool:            E:/Visual Studio2019/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 (18 files):         + SSSE3 SSE4_1
      SSE4_2 (2 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (9 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (38 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
      AVX512_SKX (8 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX

    Built as dynamic libs?:      YES
    C++ standard:                11
    C++ Compiler:                E:/Visual Studio2019/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe  (ver 19.29.30151.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:fast     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /wd4819 /MP  /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:fast     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /wd4819 /MP  /Zi /Ob0 /Od /RTC1 
    C Compiler:                  E:/Visual Studio2019/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:fast     /MP   /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:fast     /MP /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:          cudart_static.lib nppc.lib nppial.lib nppicc.lib nppidei.lib nppif.lib nppig.lib nppim.lib nppist.lib nppisu.lib nppitc.lib npps.lib cublas.lib cudnn.lib cufft.lib -LIBPATH:C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.3/lib/x64
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann fuzzy gapi hdf 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 world xfeatures2d ximgproc xobjdetect xphoto
    Disabled:                    -
    Disabled by dependency:      -
    Unavailable:                 alphamat cannops cvv freetype java julia matlab ovis python2 sfm viz
    Applications:                tests perf_tests apps
    Documentation:               NO
    Non-free algorithms:         NO

  Windows RT support:            NO

    Win32 UI:                    YES
    VTK support:                 NO

  Media I/O: 
    ZLib:                        build (ver 1.3)
    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.18.1)
    DirectShow:                  YES
    Media Foundation:            YES
      DXVA:                      YES

  Parallel framework:            Concurrency

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Intel IPP:                   2021.11.0 [2021.11.0]
           at:                   E:/lctest/opencv/sources/build/3rdparty/ippicv/ippicv_win/icv
    Intel IPP IW:                sources (2021.11.0)
              at:                E:/lctest/opencv/sources/build/3rdparty/ippicv/ippicv_win/iw
    Lapack:                      NO
    Eigen:                       NO
    Custom HAL:                  NO
    Protobuf:                    build (3.19.1)
    Flatbuffers:                 builtin/3rdparty (23.5.9)

  NVIDIA CUDA:                   YES (ver 11.3, CUFFT CUBLAS FAST_MATH)
    NVIDIA GPU arch:             61 70 75 80 86
    NVIDIA PTX archs:

  cuDNN:                         YES (ver 8.9.1)

  OpenCL:                        YES (NVD3D11)
    Include path:                E:/lctest/opencv/sources/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python 3:
    Interpreter:                 E:/conda_env/RT-DETR/python.exe (ver 3.9.17)
    Libraries:                   E:/conda_env/RT-DETR/libs/python39.lib (ver 3.9.17)
    numpy:                       E:/conda_env/RT-DETR/Lib/site-packages/numpy/core/include (ver 1.26.1)
    install path:                E:/conda_env/RT-DETR/Lib/site-packages/cv2/python-3.9

  Python (for build):            E:\conda_env\RT-DETR\python.exe

    ant:                         NO
    Java:                        NO
    JNI:                         NO
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    E:/lctest/opencv/buildpy


ready capture

(python.exe:24800): GStreamer-CRITICAL **: 11:52:08.851: gst_caps_get_structure: assertion 'GST_IS_CAPS (caps)' failed

(python.exe:24800): GStreamer-CRITICAL **: 11:52:08.851: gst_structure_get_int: assertion 'structure != NULL' failed
[ WARN:0@30.035] global cap_gstreamer.cpp:1714 cv::GStreamerCapture::open OpenCV | GStreamer warning: cannot query video width/height

(python.exe:24800): GStreamer-CRITICAL **: 11:52:08.851: gst_structure_get_fraction: assertion 'structure != NULL' failed
[ WARN:0@30.035] global cap_gstreamer.cpp:1722 cv::GStreamerCapture::open OpenCV | GStreamer warning: cannot query video fps
[ WARN:0@30.035] global cap_gstreamer.cpp:1777 cv::GStreamerCapture::open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
finish capture

MY UDP Source is ued to send my camera to udp stream

gst-launch-1.0 -v ksvideosrc ! videoconvert ! x264enc tune=zerolatency ! video/x-h264,framerate=30/1 ! h264parse ! rtph264pay pt=96 ! udpsink host= port=5600 

New to this myself. Does this help

import cv2

def list_available_cameras():
    num_cameras = 10  # Maximum number of cameras to check
    available_cameras = []
    for i in range(num_cameras):
        cap = cv2.VideoCapture(i)
        if not cap.isOpened():
    return available_cameras

if __name__ == "__main__":
    cameras = list_available_cameras()
    print("Available cameras:", cameras)

maybe not,but thank you relply。
I would capture cv2.CAP_GSTREAMER API Types
direct to capture camera by index is working in my code

I HAVE SUCCESS to get udp stream
this is solve ways
add some code in cv2/ like this:
import os

PATHS=[#activate gstreamer runtime
    os.path.join('E:/lctest/opencv/buildpy', 'x64/vc16/bin'),
    os.path.join(os.getenv('CUDA_PATH', 'C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.3'), 'bin')