Adding gst-python package
[platform/upstream/gst-python.git] / old_examples / gst-discover
1 #!/usr/bin/env python
2 # gst-python
3 # Copyright (C) 2006 Andy Wingo <wingo at pobox.com>
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Library General Public
7 # License as published by the Free Software Foundation; either
8 # version 2 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Library General Public License for more details.
14 #
15 # You should have received a copy of the GNU Library General Public
16 # License along with this library; if not, write to the
17 # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 # Boston, MA 02110-1301, USA.
19
20
21 import os
22 import sys
23
24 import pygtk
25 pygtk.require('2.0')
26 import gobject
27 gobject.threads_init()
28 import pygst
29 pygst.require('0.10')
30 import gst
31 from gst.extend import discoverer
32
33 def fail(path):
34     print "error: %r does not appear to be a media file" % path
35     sys.exit(1)
36
37 def succeed(d):
38     def pp(prop, val):
39         print '%s: %s' % (prop, val)
40     pp('media type', d.mimetype)
41
42     pp('has video', d.is_video)
43     if d.is_video:
44         pp('video caps', d.videocaps)
45         pp('video width (pixels)', d.videowidth)
46         pp('video height (pixels)', d.videoheight)
47         pp('video length (hh:mm:ss)', gst.TIME_ARGS(d.videolength))
48         pp('framerate (fps)', '%s/%s' % (d.videorate.num, d.videorate.denom))
49
50     pp('has audio', d.is_audio)
51     if d.is_audio:
52         pp('audio caps', d.audiocaps)
53         pp('audio format', d.audiofloat and 'floating-point' or 'integer')
54         pp('sample rate (Hz)', d.audiorate)
55         pp('sample width (bits)', d.audiowidth)
56         pp('sample depth (bits)', d.audiodepth)
57         pp('audio length (hh:mm:ss)', gst.TIME_ARGS(d.audiolength))
58         pp('audio channels', d.audiochannels)
59
60     sys.exit(0)
61
62 def discover(path):
63     def discovered(d, is_media):
64         if is_media:
65             succeed(d)
66         else:
67             fail(path)
68
69     d = discoverer.Discoverer(path)
70     d.connect('discovered', discovered)
71     d.discover()
72     gobject.MainLoop().run()
73
74 def usage():
75     print >>sys.stderr, "usage: gst-discover PATH-TO-MEDIA-FILE"
76     sys.exit(1)
77
78 def main(argv):
79     if len(argv) != 2:
80         usage()
81     path = argv.pop()
82     if not os.path.isfile(path):
83         print >>sys.stderr, "error: file %r does not exist" % path
84         usage()
85
86     return discover(path)
87
88 if __name__ == '__main__':
89     sys.exit(main(sys.argv))