27 Svi.

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.