如何在Python中访问GStreamer缓冲区的数据?

daf*_*daf 2 python gstreamer

在旧的(GObject-introspection前)GStreamer绑定中,可以gst.Buffer通过.data属性或转换来访问数据str.这不再可能:

>>> p buf.data
*** AttributeError: 'Buffer' object has no attribute 'data'
>>> str(buf)
'<GstBuffer at 0x7fca2c7c2950>'
Run Code Online (Sandbox Code Playgroud)

daf*_*daf 5

要访问Gst.Buffer最新版本的内容,首先必须map()使用缓冲区来获取a Gst.MapInfo,它具有data类型的属性bytes(str在Python 2中).

(result, mapinfo) = buf.map(Gst.MapFlags.READ)
assert result

try:
    # use mapinfo.data here
    pass
finally:
    buf.unmap(mapinfo)
Run Code Online (Sandbox Code Playgroud)

您还可以使用缓冲区的组成Gst.Memory元素get_memory(),并单独映射它们.(AFAICT,调用Buffer.map()等同于调用.get_all_memory()和映射结果Memory.)

不幸的是,写入这些缓冲区是不可能的,因为即使Gst.MapFlags.WRITE设置了标志,Python也用不可变类型表示它们.相反,你必须做一些事情,比如Gst.Memory用修改后的数据创建一个新的,然后使用Gst.Buffer.replace_all_memory().