Search code examples
pythonaudiogstreamerpygobjectgobject-introspection

How to read a stream size in Gstreamer?


1. Context

I have a very simple pipeline:

filesrc location=foo.wav ! decodebin ! ENCODER ! MUXER ! filesink location=bar.whatever

with ENCODER being any gstreamer encoder and MUXER, whatever suitable muxer. The pipeline is working. For the sake of simplicity, assume there is only one audio stream coming from decodebin.

2. Problem

Now,

  • How can I query the total audio stream size in bytes, right after decodebin (i.e. the raw PCM size)?
  • How can I query the total audio stream size in bytes, right after ENCODER (i.e. the raw compressed size)?

3. What I tried

I am using Python with GI. I've tried with the appsink element, to no avail, with the following pipeline:

filesrc location=foo.wav ! decodebin ! ENCODER ! tee name=tee \
      tee. ! queue ! MUXER ! filesink location=bar.whatever \
      tee. ! queue ! appsink

The relevant part with appsink is the following:

counter = 0
appsink = Gst.ElementFactory.make('appsink', None)
appsink.set_property('emit-signals', True)
appsink.set_property('sync', False)
appsink.connect('new-sample', on_new_buffer)
appsink.connect('new-preroll', on_new_preroll)

def on_new_buffer(sample):
    counter += sample.emit('pull-sample').get_buffer().get_size() 

def on_new_preroll(sample):
    counter += sample.emit('pull-preroll').get_buffer().get_size()

However this is really really slow (20x slower than just using filesink).


Solution

  • You can try using pad probes.

    http://gstreamer.freedesktop.org/data/doc/gstreamer/head/gstreamer/html/GstPad.html#gst-pad-add-probe

    Add a pad probe for buffers and buffer lists on the pads where you want to count the number of bytes.