Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the length of media with gstreamer?

How do I find the playback time of media with gstreamer?

like image 541
joeforker Avatar asked Mar 13 '10 23:03

joeforker


2 Answers

Here's a simple Python script to get the duration of anything gstreamer can decode. Note that all times in gstreamer are in nanoseconds.

duration.py:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division

import sys
import gobject
gobject.threads_init()
import pygst
pygst.require("0.10")
import gst
d = gst.parse_launch("filesrc name=source ! decodebin2 ! fakesink")
source = d.get_by_name("source")
source.set_property("location", sys.argv[1])
d.set_state(gst.STATE_PLAYING)
d.get_state()
format = gst.Format(gst.FORMAT_TIME)
duration = d.query_duration(format)[0]
d.set_state(gst.STATE_NULL)

import datetime
delta = datetime.timedelta(seconds=(duration / gst.SECOND))
print delta

Examples:

$ python duration.py VIDEO_TS/VTS_03_1.VOB
0:20:10.528000
$ python duration.py ~/Movies/BigBuckBunny_640x360.m4v
0:09:56.461667
like image 76
joeforker Avatar answered Sep 21 '22 15:09

joeforker


See Section 6.5.

http://majorsilence.com/pygtk_audio_and_video_playback_gstreamer

It avoids need to create a pipeline and run it manually.

like image 35
Hardy Avatar answered Sep 17 '22 15:09

Hardy