I'm trying to capture a video stream using OpenCV with an RTSP feed but I'm running into an issue related to GStreamer. Here's the relevant code:
import cv2
IP = 'XXX.XXX.X.XX'
LOGIN = 'username'
PASSWORD = 'password'
FEED_URL = f'rtsp://{LOGIN}:{PASSWORD}@{IP}:554/cam/realmonitor?channel=1&subtype=0'
def test_get_feed():
feed = cv2.VideoCapture(FEED_URL)
test_get_feed()
When I run the script, I get the following error:
[ WARN:[email protected]] global .../cap_gstreamer.cpp (2386) handleMessage OpenCV | GStreamer warning: your GStreamer installation is missing a required plugin
[ WARN:[email protected]] global .../cap_gstreamer.cpp (2402) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module uridecodebin0 reported: No URI handler implemented for "rtsp".
[ WARN:[email protected]] global .../cap_gstreamer.cpp (1356) open OpenCV | GStreamer warning: unable to start pipeline
OpenCV: Couldn't read video stream from file "rtsp://username:[email protected]:554/cam/realmonitor?channel=1&subtype=0"
I have already:
Uninstalled and reinstalled Gstreamer via:
brew install gstreamer
Attempted to install gst-rtsp-server and gstreamer-rtsp-plugin but neither of those were found in brew.
tried several versions of opencv-python ranging from 4.6-latest
None of those things helped. I am using an m1 mac with macOS Ventura 13.4.1.
The string you have to pass OpenCV to launch the gstreamer pipeline is the same you would use in the command prompt to see the stream. Additionally the last element of the pipeline, since you want to use OpenCV as a "sink", has to be appsink. Therefore, the string you provide to cv2.VideoCapture should be something like:
#use h265 or h264 according to your stream type
gstreamer_str = "gst-launch-1.0 rtspsrc location=rtsp://your_rtsp_src ! rtph265depay ! h265parse ! videoconvert ! appsink"
And in your function open the video capture with the following:
def test_get_feed():
feed = cv2.VideoCapture(gstreamer_str, cv2.CAP_GSTREAMER)
If this does not work just try removing "gst-launch-1.0" from gstreamer_str. If also this does not work, try launching the pipeline from the terminal (with "gst-launch-1.0" as written above) and see what prints.