GStreamer: accurate duration

When decoding, for instance, a variable-bitrate MP3, gstreamer reported durations are, to say the least, estimates. I’ve tried to get a better result in a few ways. First off, some files yield a duration tag, but even if you’re lucky and it is there, there are no guaranties about precision. After that I tried seeking to the end (GST_SEEK_END) of the stream and querying the position, which gstreamer didn’t like. Finally, routing the audio into a fakesink, waiting for the end of stream and then querying for the position gives the right result. It’s not the prettiest method, but it works.

This is a Python script that prints the duration of a media to stdout.

3 thoughts on “GStreamer: accurate duration”

  1. Inaccurate durations are really a bummer when using GStreamer, especially when making some sort of seeker. Does the solution presented here take time equal to the duration of the MP3 file to run? (I’m using Ruby, and I don’t want to install the dependencies for your Python script).

Leave a Reply

Your email address will not be published. Required fields are marked *