[英]How to stream pc webcam with rtsp and GStreamer on python
我正在尝试 stream 我的笔记本电脑网络摄像头框架与 rtsp 和 python。 为此,我使用 GStreamer。 浏览 web 上的一些示例,我得到以下代码,并使用本地视频 in.mp4 格式对其进行了测试
#!/usr/bin/env python
import sysimport gi
gi.require_version('Gst', '1.0')
gi.require_version('GstRtspServer', '1.0')
from gi.repository import Gst, GstRtspServer, GObject, GLib
loop = GLib.MainLoop()
Gst.init(None)
class TestRtspMediaFactory(GstRtspServer.RTSPMediaFactory):
def __init__(self):
GstRtspServer.RTSPMediaFactory.__init__(self)
def do_create_element(self, url):
#set mp4 file path to filesrc's location property
src_demux = "filesrc location=/home/user1/Videos/720.mp4 ! qtdemux name=demux"
h264_transcode = "demux.video_0"
#uncomment following line if video transcoding is necessary
#h264_transcode = "demux.video_0 ! decodebin ! queue ! x264enc"
pipeline = "{0} {1} ! queue ! rtph264pay name=pay0 config-interval=1 pt=96".format(src_demux, h264_transcode)
print ("Element created: " + pipeline)
return Gst.parse_launch(pipeline)
class GstreamerRtspServer():
def __init__(self):
self.rtspServer = GstRtspServer.RTSPServer()
self.rtspServer.set_service("7000")
factory = TestRtspMediaFactory()
factory.set_shared(True)
mountPoints = self.rtspServer.get_mount_points()
mountPoints.add_factory("/stream1", factory)
self.rtspServer.attach(None)
if __name__ == '__main__':
s = GstreamerRtspServer()
loop.run()
它公开了地址 127.0.0.1:7000/stream1 上的 stream。 因此,如果我从终端尝试使用以下命令,我可以在 window 中看到视频
gst-launch-1.0 rtspsrc location=rtsp://127.0.0.1:7000/stream1 ! queue ! rtph264depay ! avdec_h264 ! videoconvert ! videoscale ! video/x-raw,width=640,height=480 ! autovideosink
现在,我想更改管道以将源从 mp4 本地视频更改为我的笔记本电脑网络摄像头。 我尝试了以下方法:
vl42src device=/dev/video0 ! decodebin ! x264enc ! rtph264pay name=pay0 config-interval=1 pt=96
而且,在终端方面,我尝试连接相同的管道。 它不起作用。 这是我得到的错误:
gst-launch-1.0 rtspsrc location=rtsp://127.0.0.1:7000/stream1 ! queue ! rtph264depay ! avdec_h264 ! videoconvert ! videoscale ! video/x-raw,width=640,height=480 ! autovideosink --gst-debug=3 -v
Setting pipeline to PAUSED ...
Pipeline is live and does not need PREROLL ...
Got context from element 'autovideosink0-actual-sink-vaapi': gst.vaapi.Display=context, gst.vaapi.Display=(GstVaapiDisplay)"\(GstVaapiDisplayGLX\)\ vaapidisplayglx1";
Progress: (open) Opening Stream
Progress: (connect) Connecting to rtsp://127.0.0.1:7000/stream1
Progress: (open) Retrieving server options
Progress: (open) Retrieving media info
0:00:00.113988576 11172 0x5589f65698f0 WARN rtspsrc gstrtspsrc.c:6563:gst_rtspsrc_send:<rtspsrc0> error: Unhandled error
0:00:00.114040409 11172 0x5589f65698f0 WARN rtspsrc gstrtspsrc.c:6563:gst_rtspsrc_send:<rtspsrc0> error: Service Unavailable (503)
ERROR: from element /GstPipeline:pipeline0/GstRTSPSrc:rtspsrc0: Unhandled error
0:00:00.114134907 11172 0x5589f65698f0 WARN rtspsrc gstrtspsrc.c:7951:gst_rtspsrc_open:<rtspsrc0> can't get sdp
0:00:00.114188780 11172 0x5589f65698f0 WARN rtspsrc gstrtspsrc.c:6031:gst_rtspsrc_loop:<rtspsrc0> we are not connected
Additional debug info:
gstrtspsrc.c(6563): gst_rtspsrc_send (): /GstPipeline:pipeline0/GstRTSPSrc:rtspsrc0:
Service Unavailable (503)
ERROR: pipeline doesn't want to preroll.
Setting pipeline to PAUSED ...
Setting pipeline to READY ...
Setting pipeline to NULL ...
Freeing pipeline ...
我有什么问题? 有谁知道可以用来使我的网络摄像头工作的管道?
提前致谢
最后,我找到了这个例子,并相应地更改了我的代码
https://github.com/superdump/pyrtsp
这里使用的管道代替了代码中描述的那些
v4l2src device=/dev/video0 ! videoconvert ! x264enc speed-preset=ultrafast tune=zerolatency ! rtph264pay name=pay0 pt=96
这里来自终端的命令连接到 stream
gst-launch-1.0 rtsprc location=rtsp://127.0.0.1:8554/test is-live=true ! rtph264depay ! h264parse ! avdec_h264 ! videoconvert ! videoscale ! autovideosink
然而,唯一剩下的问题是显示图像的速度很慢,但它应该可以通过对管道进行一些调整来优化
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.