如何在PYTHON上用RTSP和GStreamer实现PC网络摄像头的流媒体

How to stream pc webcam with rtsp and GStreamer on python(如何在PYTHON上用RTSP和GStreamer实现PC网络摄像头的流媒体)

本文介绍了如何在PYTHON上用RTSP和GStreamer实现PC网络摄像头的流媒体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有rtsp和python的流媒体传输我的笔记本电脑网络摄像头帧。 为此,我使用了GStreamer。 浏览Web上的一些示例,我得到了以下代码,并使用.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上公开流。 因此,如果我尝试从终端使用以下命令,我可以在窗口中看到视频

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

这里是终端连接到流的命令

gst-launch-1.0 rtsprc location=rtsp://127.0.0.1:8554/test is-live=true ! rtph264depay ! h264parse ! avdec_h264 ! videoconvert ! videoscale ! autovideosink

然而,唯一剩下的问题是显示图像的速度很慢,但应该可以通过对管道进行一些调整来优化它

这篇关于如何在PYTHON上用RTSP和GStreamer实现PC网络摄像头的流媒体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在PYTHON上用RTSP和GStreamer实现PC网络摄像头的流媒体

基础教程推荐