Adding gst-python package
[platform/upstream/gst-python.git] / old_examples / gstfile.py
1 #!/usr/bin/env python
2 # -*- Mode: Python -*-
3 # vi:si:et:sw=4:sts=4:ts=4
4
5 # gstfile.py
6 # (c) 2005 Edward Hervey <edward at fluendo dot com>
7 # Discovers and prints out multimedia information of files
8
9 # This example shows how to use gst-python:
10 # _ in an object-oriented way (Discoverer class)
11 # _ subclassing a gst.Pipeline
12 # _ and overidding existing methods (do_iterate())
13
14 import os
15 import sys
16
17 import gobject
18 gobject.threads_init()
19
20 import pygst
21 pygst.require('0.10')
22
23 from gst.extend.discoverer import Discoverer
24
25 class GstFile:
26     """
27     Analyses one or more files and prints out the multimedia information of
28     each file.
29     """
30
31     def __init__(self, files):
32         self.files = files
33         self.mainloop = gobject.MainLoop()
34         self.current = None
35
36     def run(self):
37         gobject.idle_add(self._discover_one)
38         self.mainloop.run()
39
40     def _discovered(self, discoverer, ismedia):
41         discoverer.print_info()
42         self.current = None
43         if len(self.files):
44             print "\n"
45         gobject.idle_add(self._discover_one)
46         
47     def _discover_one(self):
48         if not len(self.files):
49             gobject.idle_add(self.mainloop.quit)
50             return False
51         filename = self.files.pop(0)
52         if not os.path.isfile(filename):
53             gobject.idle_add(self._discover_one)
54             return False
55         print "Running on", filename
56         # create a discoverer for that file
57         self.current = Discoverer(filename)
58         # connect a callback on the 'discovered' signal
59         self.current.connect('discovered', self._discovered)
60         # start the discovery
61         self.current.discover()
62         return False
63
64 def main(args):
65     if len(args) < 2:
66         print 'usage: %s files...' % args[0]
67         return 2
68
69     gstfile = GstFile(args[1:])
70     gstfile.run()
71
72 if __name__ == '__main__':
73     sys.exit(main(sys.argv))