GStreamer-based episode length detection (bug 882)
[gpodder.git] / src / gpodder / gstreamer.py
blob1adfa11082126c500ce4b2dd509ac6aa869a3e40
1 # -*- coding: utf-8 -*-
3 # gPodder - A media aggregator and podcast client
4 # Copyright (c) 2005-2010 Thomas Perl and the gPodder Team
6 # gPodder is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # gPodder is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 try:
21 import gobject
22 import pygst
23 pygst.require('0.10')
24 import gst
25 from gst.extend.discoverer import Discoverer
26 have_gst = True
27 except:
28 have_gst = False
30 class GstFile:
31 def __init__(self):
32 self.mainloop = gobject.MainLoop()
33 self.result = None
35 def run(self, filename):
36 gobject.idle_add(self.on_idle, filename)
37 self.mainloop.run()
38 return self.result / gst.MSECOND
40 def on_idle(self, filename):
41 d = Discoverer(filename)
42 d.connect('discovered', self.on_data)
43 d.discover()
44 return False
46 def on_data(self, discoverer, ismedia):
47 if discoverer.is_video:
48 self.result = discoverer.videolength
49 elif discoverer.is_audio:
50 self.result = discoverer.audiolength
51 gobject.idle_add(self.mainloop.quit)
53 def get_track_length(filename):
54 """
55 Returns track length in microseconds.
57 Prefers video streams to audio streams. If no supported streams were found,
58 returns None.
59 """
60 if not have_gst:
61 return None
62 return GstFile().run(filename)
64 __all__ = ['get_track_length']
66 # vim: set ts=4 sts=4 sw=4 et: