Receiving an RTSP stream with a custom codec in Python (on Windows)

I am building a pyside application on windows and I am trying to receive an rtsp stream carrying data encoded in a custom codec. I’ve tried a few things that didn’t work, so I need some help.

I first tried using opencv with a gstreamer backend. I’m already doing this to receive a video stream, but when I try to receive my custom codec, opencv fails to even open the stream. Here’s the code that just tries to open the stream:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import os
import cv2
gst = f"rtspsrc location={rtsp_url} ! appsink"
os.environ["GST_DEBUG"] = "3"
cap = cv2.VideoCapture(gst, cv2.CAP_GSTREAMER)
if cap.isOpened():
while True:
ret, frame = cap.read()
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
break
print(frame)
else:
print("failed to open stream")
</code>
<code>import os import cv2 gst = f"rtspsrc location={rtsp_url} ! appsink" os.environ["GST_DEBUG"] = "3" cap = cv2.VideoCapture(gst, cv2.CAP_GSTREAMER) if cap.isOpened(): while True: ret, frame = cap.read() key = cv2.waitKey(1) if key & 0xFF == ord('q'): break print(frame) else: print("failed to open stream") </code>
import os
import cv2

gst = f"rtspsrc location={rtsp_url} ! appsink"

os.environ["GST_DEBUG"] = "3"
cap = cv2.VideoCapture(gst, cv2.CAP_GSTREAMER)

if cap.isOpened():
    while True:
        ret, frame = cap.read()

        key = cv2.waitKey(1)

        if key & 0xFF == ord('q'):
            break

        print(frame)
else:
    print("failed to open stream")

which produces this output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>0:00:00.156754700 25048 0000020BB33DCBC0 FIXME default gstutils.c:4089:gst_element_decorate_stream_id_internal:<fakesrc0> Creating random stream-id, consider implementing a deterministic way of creating a stream-id
0:00:03.694289100 25048 0000020BB38135C0 WARN default gst/parse/grammar.y:918:gst_parse_no_more_pads:<rtspsrc0> warning: Delayed linking failed.
0:00:03.698133300 25048 0000020BB38135C0 WARN default gst/parse/grammar.y:918:gst_parse_no_more_pads:<rtspsrc0> warning: failed delayed linking some pad of GstRTSPSrc named rtspsrc0 to some pad of GstAppSink named appsink0
0:00:03.705085500 25048 0000020BB33DCC40 WARN basesrc gstbasesrc.c:3177:gst_base_src_loop:<udpsrc1> error: Internal data stream error.
0:00:03.708917300 25048 0000020BB33DCC40 WARN basesrc gstbasesrc.c:3177:gst_base_src_loop:<udpsrc1> error: streaming stopped, reason not-linked (-1)
[ WARN:[email protected]] global cap_gstreamer.cpp:2839 cv::handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module udpsrc1 reported: Internal data stream error.
[ WARN:[email protected]] global cap_gstreamer.cpp:1698 cv::GStreamerCapture::open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:[email protected]] global cap_gstreamer.cpp:1173 cv::GStreamerCapture::isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
[ WARN:[email protected]] global cap.cpp:206 cv::VideoCapture::open VIDEOIO(GSTREAMER): backend is generally available but can't be used to capture by name
failed to open stream
</code>
<code>0:00:00.156754700 25048 0000020BB33DCBC0 FIXME default gstutils.c:4089:gst_element_decorate_stream_id_internal:<fakesrc0> Creating random stream-id, consider implementing a deterministic way of creating a stream-id 0:00:03.694289100 25048 0000020BB38135C0 WARN default gst/parse/grammar.y:918:gst_parse_no_more_pads:<rtspsrc0> warning: Delayed linking failed. 0:00:03.698133300 25048 0000020BB38135C0 WARN default gst/parse/grammar.y:918:gst_parse_no_more_pads:<rtspsrc0> warning: failed delayed linking some pad of GstRTSPSrc named rtspsrc0 to some pad of GstAppSink named appsink0 0:00:03.705085500 25048 0000020BB33DCC40 WARN basesrc gstbasesrc.c:3177:gst_base_src_loop:<udpsrc1> error: Internal data stream error. 0:00:03.708917300 25048 0000020BB33DCC40 WARN basesrc gstbasesrc.c:3177:gst_base_src_loop:<udpsrc1> error: streaming stopped, reason not-linked (-1) [ WARN:[email protected]] global cap_gstreamer.cpp:2839 cv::handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module udpsrc1 reported: Internal data stream error. [ WARN:[email protected]] global cap_gstreamer.cpp:1698 cv::GStreamerCapture::open OpenCV | GStreamer warning: unable to start pipeline [ WARN:[email protected]] global cap_gstreamer.cpp:1173 cv::GStreamerCapture::isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created [ WARN:[email protected]] global cap.cpp:206 cv::VideoCapture::open VIDEOIO(GSTREAMER): backend is generally available but can't be used to capture by name failed to open stream </code>
0:00:00.156754700 25048 0000020BB33DCBC0 FIXME                default gstutils.c:4089:gst_element_decorate_stream_id_internal:<fakesrc0> Creating random stream-id, consider implementing a deterministic way of creating a stream-id
0:00:03.694289100 25048 0000020BB38135C0 WARN                 default gst/parse/grammar.y:918:gst_parse_no_more_pads:<rtspsrc0> warning: Delayed linking failed.
0:00:03.698133300 25048 0000020BB38135C0 WARN                 default gst/parse/grammar.y:918:gst_parse_no_more_pads:<rtspsrc0> warning: failed delayed linking some pad of GstRTSPSrc named rtspsrc0 to some pad of GstAppSink named appsink0
0:00:03.705085500 25048 0000020BB33DCC40 WARN                 basesrc gstbasesrc.c:3177:gst_base_src_loop:<udpsrc1> error: Internal data stream error.
0:00:03.708917300 25048 0000020BB33DCC40 WARN                 basesrc gstbasesrc.c:3177:gst_base_src_loop:<udpsrc1> error: streaming stopped, reason not-linked (-1)
[ WARN:[email protected]] global cap_gstreamer.cpp:2839 cv::handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module udpsrc1 reported: Internal data stream error.
[ WARN:[email protected]] global cap_gstreamer.cpp:1698 cv::GStreamerCapture::open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:[email protected]] global cap_gstreamer.cpp:1173 cv::GStreamerCapture::isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
[ WARN:[email protected]] global cap.cpp:206 cv::VideoCapture::open VIDEOIO(GSTREAMER): backend is generally available but can't be used to capture by name
failed to open stream

The thing is, the pipeline runs fine without errors in the command line using gst-launch-1.0. I assume cv2.videoCapture can only be given video frame formats? I really don’t know.

I then tried using python bindings for gstreamer, but there doesn’t seem to be any such bindings that work on Windows. I know I can use WSL, but I’m too deep into my development to now set everything up again in WSL. That said, I did a short test in WSL just to see if it would work, and it does. Here’s the code I used:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import gi
from threading import Thread
import struct
gi.require_version('Gst', '1.0')
gi.require_version("GstApp", "1.0")
from gi.repository import Gst, GstApp, GLib
_ = GstApp
Gst.init()
main_loop = GLib.MainLoop()
main_loop_thread = Thread(target=main_loop.run)
main_loop_thread.start()
pipeline = Gst.parse_launch("rtspsrc location=rtsp_url ! appsink name=sink")
appsink = pipeline.get_by_name("sink")
pipeline.set_state(Gst.State.PLAYING)
try:
while True:
sample = appsink.try_pull_sample(Gst.SECOND)
if sample is None:
print("no sample")
continue
buf = sample.get_buffer()
result, mapinfo = buf.map(Gst.MapFlags.READ)
dat = mapinfo.data
datum = dat[12:]
x, y, worn = struct.unpack("!ffB", datum)
print(x, y)
except KeyboardInterrupt:
print("stopping")
pass
pipeline.set_state(Gst.State.NULL)
main_loop.quit()
main_loop_thread.join()
</code>
<code>import gi from threading import Thread import struct gi.require_version('Gst', '1.0') gi.require_version("GstApp", "1.0") from gi.repository import Gst, GstApp, GLib _ = GstApp Gst.init() main_loop = GLib.MainLoop() main_loop_thread = Thread(target=main_loop.run) main_loop_thread.start() pipeline = Gst.parse_launch("rtspsrc location=rtsp_url ! appsink name=sink") appsink = pipeline.get_by_name("sink") pipeline.set_state(Gst.State.PLAYING) try: while True: sample = appsink.try_pull_sample(Gst.SECOND) if sample is None: print("no sample") continue buf = sample.get_buffer() result, mapinfo = buf.map(Gst.MapFlags.READ) dat = mapinfo.data datum = dat[12:] x, y, worn = struct.unpack("!ffB", datum) print(x, y) except KeyboardInterrupt: print("stopping") pass pipeline.set_state(Gst.State.NULL) main_loop.quit() main_loop_thread.join() </code>
import gi
from threading import Thread
import struct

gi.require_version('Gst', '1.0')
gi.require_version("GstApp", "1.0")

from gi.repository import Gst, GstApp, GLib

_ = GstApp

Gst.init()

main_loop = GLib.MainLoop()
main_loop_thread = Thread(target=main_loop.run)
main_loop_thread.start()

pipeline = Gst.parse_launch("rtspsrc location=rtsp_url ! appsink name=sink")

appsink = pipeline.get_by_name("sink")

pipeline.set_state(Gst.State.PLAYING)

try:
    while True:
        sample = appsink.try_pull_sample(Gst.SECOND)
        if sample is None:
            print("no sample")
            continue
        
        buf = sample.get_buffer()
        result, mapinfo = buf.map(Gst.MapFlags.READ)
        dat = mapinfo.data

        datum = dat[12:]

        x, y, worn = struct.unpack("!ffB", datum)
        print(x, y)

except KeyboardInterrupt:
    print("stopping")
    pass


pipeline.set_state(Gst.State.NULL)
main_loop.quit()
main_loop_thread.join()

Does anyone know if I can get this working with opencv? Or if there is any way to run gstreamer code in python on windows? I’d appreciate any help I can get, I’ve been pulling my hair out for days. Thanks.

New contributor

ismail hosny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật