gstreamer appsink无法获取flvmux d

2024-06-12 21:42:03 发布

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

我使用gstreamer和pythonggi来获取编码的视频流数据。我的发布就像gst-launch-1.0 v4l2src device=/dev/video0 ! x264enc bitrate=1000 ! h264parse ! flvmux ! appsink。 现在我用python编写如下代码:

import gi
gi.require_version('Gst', '1.0')
gi.require_version('GstApp', '1.0')
from gi.repository import GObject, Gst, GstApp
GObject.threads_init()
Gst.init(None)
class Example:
    def __init__(self):
        self.mainloop = GObject.MainLoop()
        self.pipeline = Gst.Pipeline()
        self.bus = self.pipeline.get_bus()
        self.bus.add_signal_watch()
        self.bus.connect('message::eos', self.on_eos)
        self.bus.connect('message::error', self.on_error)
        # Create elements
        self.src = Gst.ElementFactory.make('v4l2src', None)
        self.encoder = Gst.ElementFactory.make('x264enc', None)
        self.parse = Gst.ElementFactory.make('h264parse', None)
        self.mux = Gst.ElementFactory.make('flvmux', None)
        self.sink = Gst.ElementFactory.make('appsink', None)
        # Add elements to pipeline
        self.pipeline.add(self.src)
        self.pipeline.add(self.encoder)
        self.pipeline.add(self.parse)
        self.pipeline.add(self.mux)
        self.pipeline.add(self.sink)
        # Set properties
        self.src.set_property('device', "/dev/video0")
        # Link elements
        self.src.link(self.encoder)
        self.encoder.link(self.parse)
        self.parse.link(self.mux)
        self.mux.link(self.sink)
    def run(self):
        self.pipeline.set_state(Gst.State.PLAYING)
        # self.mainloop.run()
        appsink_sample = GstApp.AppSink.pull_sample(self.sink)
        while True:
            buff = appsink_sample.get_buffer()
            size, offset, maxsize = buff.get_sizes()
            frame_data = buff.extract_dup(offset, size)
            print(frame_data)
    def kill(self):
        self.pipeline.set_state(Gst.State.NULL)
        self.mainloop.quit()
    def on_eos(self, bus, msg):
        print('on_eos()')
        self.kill()
    def on_error(self, bus, msg):
        print('on_error():', msg.parse_error())
        self.kill()
example = Example()
example.run()

但我每次都得到相同的数据,就像“FLV\0x01\0x01”。在

而不是用C语言编写函数,但是我得到了同样的结果。为什么?有人能帮我吗?在


Tags: selfnoneaddmakepipelineparseondef
2条回答

问题是gstreamer的过程。我们应该使用signal函数来获取appsink中的流数据。在

print打印我假设的字符串?缓冲区包含二进制数据。它只是从类似字符串的数据开始。所以它可能以FLV\0x01\0x01\0x00开始。。然后是更多的二进制数据。字符串函数将把0x00作为字符串的结束标记,并停止打印(因为print函数不接受大小参数,这是数据结束的协议)。但是,size属性应该更改。。除非所有数据具有相同的数据块大小。。但是你需要找到另一个能打印二进制数据的函数——尽管我不确定这是否真的是你想要的。也许你想把这些数据写到一个文件里?在

相关问题 更多 >