简体   繁体   English

将 gstreamer 管道转换为 python 代码

[英]convert gstreamer pipeline to python code

Im trying to convert gstreamer pipeline to python code using gi library.我正在尝试使用 gi 库将 gstreamer 管道转换为 python 代码。

This is the pipeline which is running successfully in terminal:这是在终端中成功运行的管道:

gst-launch-1.0 rtspsrc location="rtsp://admin:123456@192.168.0.150:554/H264?ch=1&subtype=0&proto=Onvif" latency=300 ! rtph264depay ! h264parse ! nvv4l2decoder drop-frame-interval=1 ! nvvideoconvert ! video/x-raw,width=1920,height=1080,formate=I420 ! queue !  nveglglessink window-x=0 window-y=0 window-width=1080 window-height=720

but while running the same pipeline using python code, there is no output window displaying rtsp stream and also no error on the terminal.但是在使用 python 代码运行相同的管道时,没有显示 rtsp 流的输出窗口,终端上也没有错误。 The terminal simply stuck until i press ctrl+c.终端只是卡住,直到我按 ctrl+c。

This is the code that im using to run the gstreamer command:这是我用来运行 gstreamer 命令的代码:

import gi

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

from gi.repository import Gst, GObject


def main(device):
    GObject.threads_init()
    Gst.init(None)

    pipeline = Gst.Pipeline()

    source = Gst.ElementFactory.make("rtspsrc", "video-source")
    source.set_property("location", device)
    source.set_property("latency", 300)
    pipeline.add(source)

    depay = Gst.ElementFactory.make("rtph264depay", "depay")
    pipeline.add(depay)
    source.link(depay)

    parse = Gst.ElementFactory.make("h264parse", "parse")
    pipeline.add(parse)
    depay.link(parse)

    decoder = Gst.ElementFactory.make("nvv4l2decoder", "decoder")
    decoder.set_property("drop-frame-interval", 2)
    pipeline.add(decoder)
    parse.link(decoder)

    convert = Gst.ElementFactory.make("nvvideoconvert", "convert")
    pipeline.add(convert)
    decoder.link(convert)

    caps = Gst.Caps.from_string("video/x-raw,width=1920,height=1080,formate=I420")
    filter = Gst.ElementFactory.make("capsfilter", "filter")
    filter.set_property("caps", caps)
    pipeline.add(filter)
    convert.link(filter)

    queue = Gst.ElementFactory.make("queue", "queue")
    pipeline.add(queue)
    filter.link(queue)

    sink = Gst.ElementFactory.make("nveglglessink", "video-sink")
    sink.set_property("window-x", 0)
    sink.set_property("window-y", 0)
    sink.set_property("window-width", 1280)
    sink.set_property("window-height", 720)

    pipeline.add(sink)

    queue.link(sink)

    loop = GObject.MainLoop()

    pipeline.set_state(Gst.State.PLAYING)

    try:
        loop.run()
    except:
        pass

    pipeline.set_state(Gst.State.NULL)

if __name__ == "__main__":
    main("rtsp://admin:123456@192.168.0.150:554/H264?ch=1&subtype=0&proto=Onvif")

Does anyone know what is the mistake?有谁知道是什么错误? Thank you!谢谢!

The reason it doesn't work is because rtspsrc 's source pad is a so-called "Sometimes pad" .它不起作用的原因是因为rtspsrc的源垫是所谓的“有时垫” The link here explains it quite well, but basically you cannot know upfront how many pads will become available on the rtspsrc , since this depends on the SDP provided by the RTSP server.此处的链接很好地解释了它,但基本上您无法预先知道rtspsrc多少垫可用,因为这取决于 RTSP 服务器提供的 SDP。

As such, you should listen to the "pad-added" signal of the rtspsrc , where you can link the rest of your pipeline to the source pad that just showed up in the callback.因此,您应该收听rtspsrc的“ rtspsrc "pad-added"信号,您可以在其中将管道的其余部分链接到刚刚出现在回调中的源焊盘。

So summarised:所以总结如下:

def main(device):
    GObject.threads_init()
    Gst.init(None)

    pipeline = Gst.Pipeline()
    source = Gst.ElementFactory.make("rtspsrc", "video-source")
    source.set_property("location", device)
    source.set_property("latency", 300)
    source.connect("pad-added", on_rtspsrc_pad_added)
    pipeline.add(source)

    # We will add/link the rest of the pipeline later
    loop = GObject.MainLoop()

    pipeline.set_state(Gst.State.PLAYING)

    try:
        loop.run()
    except:
        pass

    pipeline.set_state(Gst.State.NULL)

def on_rtspsrc_pad_added(rtspsrc, pad, *user_data):
    # Create the rest of your pipeline here and link it 
    depay = Gst.ElementFactory.make("rtph264depay", "depay")
    pipeline.add(depay)
    rtspsrc.link(depay)

    # and so on ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM