Fang-Pen Lin
Fang-Pen Lin

Reputation: 14416

How to get duration of steaming data with GStreamer

I'm writing a program for converting media file to mp3 file with GStreamer. It works, but I would like to know the duration of audio stream, too. Following is the simplified code.

import logging

import pygst
pygst.require('0.10')
import gst

# this is very important, without this, callbacks from gstreamer thread
# will messed our program up
import gobject
gobject.threads_init()


def on_new_buffer(appsink):
    buf = appsink.emit('pull-buffer')
    print 'new buffer', len(buf)

def on_new_preroll(appsink):
    buf = appsink.emit('pull-preroll')
    print 'new preroll', len(buf)

def on_pad_added(decoder, pad):
    print 'Pad added'
    decoder.link(converter)
    pipeline.set_state(gst.STATE_PLAYING)

def on_msg(msg):
    if msg.type == gst.MESSAGE_ERROR:
        error, debug = msg.parse_error()
        print error, debug
    elif msg.type == gst.MESSAGE_EOS:
        duration = pipeline.query_duration(gst.FORMAT_TIME)
        print 'Duration', duration

pipeline = gst.Pipeline('pipeline')

appsrc = gst.element_factory_make('appsrc', 'src')
decoder = gst.element_factory_make('decodebin2', 'decoder')
converter = gst.element_factory_make('audioconvert', 'converter')
lame = gst.element_factory_make('lamemp3enc', 'lame')
appsink = gst.element_factory_make('appsink', 'sink')

pipeline.add(appsrc, decoder, lame, converter, appsink)
gst.element_link_many(appsrc, decoder)
gst.element_link_many(converter, lame, appsink)

# -- setup appskink --

# -- setup decoder --
decoder.connect('pad-added', on_pad_added)

# -- setup mp3 encoder --
lame.set_property('bitrate', 128)

# -- setup appsink --
# this makes appsink emit singals
appsink.set_property('emit-signals', True)
# turns off sync to make decoding as fast as possible
appsink.set_property('sync', False)
appsink.connect('new-buffer', on_new_buffer)
appsink.connect('new-preroll', on_new_preroll)

pipeline.set_state(gst.STATE_PAUSED)

data = open(r'D:\Musics\Fiona Fung - Proud Of You.mp3', 'rb').read()
buf = gst.Buffer(data)
appsrc.emit('push-buffer', buf)
appsrc.emit('end-of-stream')

bus = pipeline.get_bus()
while True:
    msg = bus.poll(gst.MESSAGE_ANY, -1)
    on_msg(msg)

I didn't use filesrc as source, I use appsrc instead. I would like to read streaming data from Internet rather than a file. Strangely, as result, the output duration is -1

....
new buffer 315
new buffer 320
new buffer 335
new buffer 553
Duration (-1L, <enum GST_FORMAT_TIME of type GstFormat>)

If I switch the appsrc to filesrc, then the duration is correct

import logging

import pygst
pygst.require('0.10')
import gst

# this is very important, without this, callbacks from gstreamer thread
# will messed our program up
import gobject
gobject.threads_init()


def on_new_buffer(appsink):
    buf = appsink.emit('pull-buffer')
    print 'new buffer', len(buf)

def on_new_preroll(appsink):
    buf = appsink.emit('pull-preroll')
    print 'new preroll', len(buf)

def on_pad_added(decoder, pad):
    print 'Pad added'
    decoder.link(converter)
    pipeline.set_state(gst.STATE_PLAYING)

def on_msg(msg):
    if msg.type == gst.MESSAGE_ERROR:
        error, debug = msg.parse_error()
        print error, debug
    elif msg.type == gst.MESSAGE_EOS:
        duration = pipeline.query_duration(gst.FORMAT_TIME)
        print 'Duration', duration

pipeline = gst.Pipeline('pipeline')

filesrc = gst.element_factory_make('filesrc', 'src')
decoder = gst.element_factory_make('decodebin2', 'decoder')
converter = gst.element_factory_make('audioconvert', 'converter')
lame = gst.element_factory_make('lamemp3enc', 'lame')
appsink = gst.element_factory_make('appsink', 'sink')

pipeline.add(filesrc, decoder, lame, converter, appsink)
gst.element_link_many(filesrc, decoder)
gst.element_link_many(converter, lame, appsink)

# -- setup filesrc --
filesrc.set_property('location', r'D:\Musics\Fiona Fung - Proud Of You.mp3')

# -- setup decoder --
decoder.connect('pad-added', on_pad_added)

# -- setup mp3 encoder --
lame.set_property('bitrate', 128)

# -- setup appsink --
# this makes appsink emit singals
appsink.set_property('emit-signals', True)
# turns off sync to make decoding as fast as possible
appsink.set_property('sync', False)
appsink.connect('new-buffer', on_new_buffer)
appsink.connect('new-preroll', on_new_preroll)

pipeline.set_state(gst.STATE_PAUSED)

bus = pipeline.get_bus()
while True:
    msg = bus.poll(gst.MESSAGE_ANY, -1)
    on_msg(msg)

As you can see, the result is correct now.

new buffer 322
new buffer 323
new buffer 315
new buffer 320
new buffer 549
Duration (189459000000L, <enum GST_FORMAT_TIME of type GstFormat>)

So, my question is - how to get correct duration of audio stream data from appsrc?

Thanks.

Upvotes: 1

Views: 4686

Answers (1)

plaes
plaes

Reputation: 32716

Unfortunately with appsrc it is not possible to get the exact duration of the stream, although with some formats that have fixed bitrate it is possible to estimate it based on file length, but other formats that use variable bitrates report an unknown length.

Because appsrc works on the incoming buffers (either push or pull model), by it receiving a chunk of data, consuming it and then either it requests or is supplied next chunk of data and therefore making the estimation of media duration almost impossible. Also, in push model it is not possible to seek media.

Upvotes: 1

Related Questions