使用Python捕获gstreamer网络视频

2024-06-12 23:57:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Python捕捉并显示一个网络视频流。已使用以下命令创建流(在我的笔记本电脑上):

gst-launch-1.0 v4l2src ! videorate ! video/x-raw,framerate=2/1,width=640,height=480 ! x264enc pass=qual quantizer=20 tune=zerolatency ! rtph264pay config-interval=10 pt=96 ! udpsink host=127.0.0.1 port=5000

它接收网络摄像头的输入并通过UDP端口传输。我可以使用以下命令捕获流并显示它:

^{pr2}$

现在,我正尝试用Python脚本执行相同的(捕获),但并不缺乏。这是我的代码:

import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst

udpPipe = Gst.pipeline("player")
source = Gst.ElementFactory.make('udpsrc', None)
source.set_property("port", 5000)
source.set_property("host", "127.0.0.1")

rdepay = Gst.ElementFactory.make('rtph264depay', 'rdepay')
vdecode = Gst.ElementFactory.make('avdec_h264', 'vdecode')
sink = Gst.ElementFactory.make('xvimagesink', None)

udpPipe.add(source, rdepay, vdecode, sink)
gst.element_link_many(source, rdepay, vdecode, sink)
udpPipe.set_state(gst.STATE_PLAYING)

我得到的错误是:

/usr/lib/python2.7/dist-packages/gi/overrides/Gst.py:56: Warning: /build/glib2.0-prJhLS/glib2.0-2.48.2/./gobject/gsignal.c:1674: parameter 1 of type '<invalid>' for signal "GstBus::sync_message" is not a value type
  Gst.Bin.__init__(self, name=name)
/usr/lib/python2.7/dist-packages/gi/overrides/Gst.py:56: Warning: /build/glib2.0-prJhLS/glib2.0-2.48.2/./gobject/gsignal.c:1674: parameter 1 of type '<invalid>' for signal "GstBus::message" is not a value type
  Gst.Bin.__init__(self, name=name)
Traceback (most recent call last):
  File "getUdp.py", line 13, in <module>
    source = Gst.ElementFactory.make('udpsrc', None)
  File "/usr/lib/python2.7/dist-packages/gi/overrides/Gst.py", line 217, in make
    return Gst.ElementFactory.make(factory_name, instance_name)
TypeError: unbound method fake_method() must be called with ElementFactory instance as first argument (got str instance instead) 

有什么想法吗?:-(


Tags: namepynonesourcemaketypesetsink
1条回答
网友
1楼 · 发布于 2024-06-12 23:57:15

今天我在Debian9.3(stretch)上也遇到了同样的错误。 显式调用Gst.init解决了问题。在

下面的代码在我的系统上弹出了一个xvimagesink窗口,其中包括python2.7和3.5。在

#!/usr/bin/python
import sys
import gi
gi.require_version('GLib', '2.0')
gi.require_version('Gst', '1.0')
from gi.repository import GLib, Gst

Gst.init(sys.argv)

udpPipe = Gst.Pipeline("player")
source = Gst.ElementFactory.make('udpsrc', None)
source.set_property("port", 5000)
#source.set_property("host", "127.0.0.1")
caps = Gst.caps_from_string("application/x-rtp, payload=127")
source.set_property("caps", caps)

rdepay = Gst.ElementFactory.make('rtph264depay', 'rdepay')
vdecode = Gst.ElementFactory.make('avdec_h264', 'vdecode')
sink = Gst.ElementFactory.make('xvimagesink', None)
sink.set_property("sync", False)

udpPipe.add(source, rdepay, vdecode, sink)

#Gst.element_link_many(source, rdepay, vdecode, sink)
source.link(rdepay)
rdepay.link(vdecode)
vdecode.link(sink)

udpPipe.set_state(Gst.State.PLAYING)

GLib.MainLoop().run()

我认为有必要打电话消费税初始并使用PyGObject运行mainloop将gst launch命令行转换为python脚本。在

相关问题 更多 >