I need to process live streams from twitch and other sites and the default ffmpeg backend has a bug which leads to stuttering so the recommendation is to use gstreamer instead.
I was trying for almost two weeks but cant get the gstreamer backend working.
I built OpenCV with gstreamer. Every DLL path is correct in cmake-gui, gstreamer directory is added to the path. It builds successfully then when I do import cv2 it cant find the DLLs. I tried everything I found about the topic but nothing worked so I finally copied the gstreamer DLLs to “site-packages\cv2\python-3.9” which is a dirty hack but at least now I can import cv2.
Then the problems continue… I can successfully show my webcam image with:
gst-launch-1.0 camerabin ! autovideosink
or show a twitch stream with:
gst-launch-1.0 [stream url] ! decodebin ! autovideosink
but nothing works with OpenCV. I tried ‘appsink’, BGR convert and every example I found but nothing works.
import cv2
import streamlink
import os
stream = 'https://www.twitch.tv/nokokopuffs'
url = streamlink.streams(stream)['480p'].to_url()
gs = f'{url} ! decodebin ! autovideosink'
os.system(f'gst-launch-1.0 {gs}')
print('CLOSING GST-LAUNCH\n')
cap = cv2.VideoCapture(gs, cv2.CAP_GSTREAMER)
while(True):
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) != -1: break
cap.release()
cv2.destroyAllWindows()
This code first shows the stream with gst-launch fine then crashes at the OpenCV part with the following output:
Setting pipeline to PAUSED ...
Pipeline is PREROLLING ...
Got context from element 'souphttpsrc0': gst.soup.session=context, session=(SoupSession)NULL, force=(boolean)false;
Redistribute latency...
Redistribute latency...
Pipeline is PREROLLED ...
Setting pipeline to PLAYING ...
New clock: GstSystemClock
ERROR: from element /GstPipeline:pipeline0/GstAutoVideoSink:autovideosink0/GstD3DVideoSink:autovideosink0-actual-sink-d3dvideo: Output window was closed
Additional debug info:
../sys/d3dvideosink/d3dhelpers.c(1910): d3d_render_buffer (): /GstPipeline:pipeline0/GstAutoVideoSink:autovideosink0/GstD3DVideoSink:autovideosink0-actual-sink-d3dvideo
Execution ended after 0:00:01.722096000
Setting pipeline to PAUSED ...
Setting pipeline to READY ...
Setting pipeline to NULL ...
Freeing pipeline ...
CLOSING GST-LAUNCH
[ WARN:0] global C:\opencv\opencv\modules\videoio\src\cap_gstreamer.cpp (480) cv::GStreamerCapture::isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
[ERROR:0] global C:\opencv\opencv\modules\videoio\src\cap.cpp (116) cv::VideoCapture::open VIDEOIO(GSTREAMER): raised OpenCV exception:
OpenCV(4.2.0) C:\opencv\opencv\modules\videoio\src\cap_gstreamer.cpp:743: error: (-215:Assertion failed) uridecodebin in function 'cv::GStreamerCapture::open'
Traceback (most recent call last):
File "d:\Work\2110 Imperfect Copies\vidgear_test.py", line 19, in <module>
cv2.imshow('frame', frame)
cv2.error: OpenCV(4.2.0) C:\opencv\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
Some help would be greatly appreciated as I don’t know what else to try. I built it many times with different options, tried many gstreamer pipeline options. Tried it on windows 10, 11, fedora, ubuntu… Nothing works…