Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access the data of a GStreamer buffer in Python?

In the old (pre-GObject-introspection) GStreamer bindings, it was possible to access gst.Buffer data via the .data attribute or by casting to str. This is no longer possible:

>>> p buf.data
*** AttributeError: 'Buffer' object has no attribute 'data'
>>> str(buf)
'<GstBuffer at 0x7fca2c7c2950>'
like image 944
daf Avatar asked Sep 14 '25 08:09

daf


1 Answers

To access the contents of a Gst.Buffer in recent versions, you must first map() the buffer to get a Gst.MapInfo, which has a data attribute of type bytes (str in Python 2).

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

try:
    # use mapinfo.data here
    pass
finally:
    buf.unmap(mapinfo)

You can also access the buffer's constituent Gst.Memory elements with get_memory(), and map them individually. (AFAICT, calling Buffer.map() is equivalent to calling .get_all_memory() and mapping the resulting Memory.)

Unfortunately, writing to these buffers is not possible since Python represents them with immutable types even when the Gst.MapFlags.WRITE flag is set. Instead, you'd have to do something like create a new Gst.Memory with the modified data, and use Gst.Buffer.replace_all_memory().

like image 148
daf Avatar answered Sep 15 '25 21:09

daf