"I have a simple mission: To create an open-source, non-linear video editor for Linux. Many have tried and fallen before me, but for some reason I feel compelled to try myself. I am documenting my journey in this blog for all to read. It will be a dangerous journey, and I might not make it back alive. Hold on tight, and enjoy the ride! By the way, I'm calling this project OpenShot Video Editor!"

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.

4 comments

  1. Bertrand Son Kintanar  

    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.

  2. b3rx  

    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.

  3. Jonathan Thomas  

    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).

  4. b3rx  

    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!

Post a Comment

Subscribe to: Post Comments (Atom)