opencv runtime_error ai_generated true

OpenCV: GStreamer: cannot link elements: source -> decodebin

ID: opencv/videocapture-gstreamer-pipeline-failed

Also available as: JSON · Markdown · 中文
82%Fix Rate
83%Confidence
1Evidence
2023-09-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
opencv-4.5.5 active
opencv-4.8.0 active
opencv-4.9.0 active

Root Cause

The GStreamer pipeline string passed to VideoCapture contains incompatible elements or missing plugins, preventing the source element from linking to decodebin.

generic

中文

传递给 VideoCapture 的 GStreamer 管道字符串包含不兼容的元素或缺少插件,导致源元素无法链接到 decodebin。

Official Documentation

https://docs.opencv.org/4.x/d0/da7/videoio_overview.html

Workarounds

  1. 85% success Test the pipeline outside OpenCV first: run 'gst-launch-1.0 filesrc location=video.mp4 ! decodebin ! fakesink'. If it fails, install missing plugins: 'sudo apt-get install gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly'. Then adjust the pipeline string: cv::VideoCapture cap('filesrc location=video.mp4 ! decodebin ! videoconvert ! appsink', cv::CAP_GSTREAMER);
    Test the pipeline outside OpenCV first: run 'gst-launch-1.0 filesrc location=video.mp4 ! decodebin ! fakesink'. If it fails, install missing plugins: 'sudo apt-get install gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly'. Then adjust the pipeline string: cv::VideoCapture cap('filesrc location=video.mp4 ! decodebin ! videoconvert ! appsink', cv::CAP_GSTREAMER);
  2. 75% success Simplify the pipeline to use autovideosrc for live cameras: cv::VideoCapture cap('autovideosrc ! videoconvert ! appsink', cv::CAP_GSTREAMER);
    Simplify the pipeline to use autovideosrc for live cameras: cv::VideoCapture cap('autovideosrc ! videoconvert ! appsink', cv::CAP_GSTREAMER);
  3. 80% success Provide a full GStreamer pipeline with explicit caps: cv::VideoCapture cap('filesrc location=video.mp4 ! qtdemux ! h264parse ! avdec_h264 ! videoconvert ! appsink', cv::CAP_GSTREAMER);
    Provide a full GStreamer pipeline with explicit caps: cv::VideoCapture cap('filesrc location=video.mp4 ! qtdemux ! h264parse ! avdec_h264 ! videoconvert ! appsink', cv::CAP_GSTREAMER);

中文步骤

  1. Test the pipeline outside OpenCV first: run 'gst-launch-1.0 filesrc location=video.mp4 ! decodebin ! fakesink'. If it fails, install missing plugins: 'sudo apt-get install gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly'. Then adjust the pipeline string: cv::VideoCapture cap('filesrc location=video.mp4 ! decodebin ! videoconvert ! appsink', cv::CAP_GSTREAMER);
  2. Simplify the pipeline to use autovideosrc for live cameras: cv::VideoCapture cap('autovideosrc ! videoconvert ! appsink', cv::CAP_GSTREAMER);
  3. Provide a full GStreamer pipeline with explicit caps: cv::VideoCapture cap('filesrc location=video.mp4 ! qtdemux ! h264parse ! avdec_h264 ! videoconvert ! appsink', cv::CAP_GSTREAMER);

Dead Ends

Common approaches that don't work:

  1. Change the video file path to an absolute path 90% fail

    The error is about GStreamer element linking, not file path resolution. Absolute paths don't fix element compatibility.

  2. Reinstall OpenCV with gstreamer support using cmake -DWITH_GSTREAMER=ON 80% fail

    If OpenCV already has GStreamer support (the error message shows GStreamer is running), recompiling won't fix the pipeline syntax.

  3. Use CAP_FFMPEG backend instead by setting cv::CAP_FFMPEG 60% fail

    This workaround avoids GStreamer but may not be available if FFMPEG is not compiled in, or the source requires GStreamer-specific features.