Want to know how many nanoseconds, seconds, or minutes are in your audio or video file? Want to know your current time position while listening to your audio or video file? There are two super useful methods provided in the Gstreamer framework to answer these two questions (examples are using the Python language):
position, format = pipeline.query_position(gst.FORMAT_TIME)
duration, format = pipeline.query_duration(gst.FORMAT_TIME)
Remember, these queries will only work once a media file has been pre-rolled (i.e. the state must be changed to PAUSED or PLAY). A common way to continuously query a media file is to create a timer which fires a method every 500 milliseconds (or whatever time interval makes sense for your app). That timer method can call these 2 methods, and update a progress bar... or any number of widgets.
The next important thing to note: these methods return the time in nanoseconds, which is not the most obvious thing in the world. The return values will be quite large. So divide them by 1,000,000,000 to convert them into seconds. For example:
position_seconds = position / 1000000000
duration_seconds = duration / 1000000000
Don't forget to put a Try / Except around your queries. If the media is not pre-rolled, or if for some reason the media file does not support those queries, it will return a null value... and thus break your nanoseconds to seconds conversion.
OpenShot Video Editor(TM) is an open-source program that creates, modifies, and edits video files. Copyright (C) 2008 Jonathan Thomas.
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.





May 29, 2008 9:43 AM
Thanks for a nice post. I've been looking for such an information. I was wondering is there a way to query the duration of a certain file without showing the video widget? even if i pause it the video widget still pops up.
May 29, 2008 10:43 AM
I'm currently using the playbin element. do you know how to get the duration using filesrc? because when i try to use it, it gives me -1.
please advice.
May 29, 2008 1:41 PM
To query duration without showing a video widget, I believe you need to use a fakesink (http://gstreamer.freedesktop.org/data/doc/gstreamer/0.10.1/gstreamer-plugins/html/gstreamer-plugins-fakesink.html). I'll give this a try tonight and see if it works.
If you are getting a -1 as the duration, it sounds like the stream was not set to PAUSED or PLAY. You might try putting that query in a timer, and continuously querying the pipeline (just to see if it's always -1).
May 29, 2008 1:47 PM
Thanks for the info jonathan. Actually I don't need to play the video itself. All I need is to find out the duration of the video file. and reading about the gst-discover module, does this exactly. I'm actually creating a video converter for my psp. im currently using python, pygtk, gst, and ffmpeg for it.
thanks again. I'll be your newest blog reader.
cheers!