3 # vi:si:et:sw=4:sts=4:ts=4
5 # GStreamer python bindings
6 # Copyright (C) 2002 David I. Lehn <dlehn@users.sourceforge.net>
7 # 2004 Johan Dahlin <johan@gnome.org>
9 # filesrc.py: implements a file source element completely in python
11 # This library is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU Library General Public
13 # License as published by the Free Software Foundation; either
14 # version 2 of the License, or (at your option) any later version.
16 # This library is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 # Library General Public License for more details.
21 # You should have received a copy of the GNU Library General Public
22 # License along with this library; if not, write to the
23 # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 # Boston, MA 02110-1301, USA.
27 import gobject; gobject.threads_init()
32 class FileSource(gst.BaseSrc):
34 gst.PadTemplate("src",
43 def __init__(self, name):
44 self.__gobject_init__()
48 def set_property(self, name, value):
49 if name == 'location':
50 self.fd = open(value, 'r')
52 def do_create(self, offset, size):
53 if offset != self.curoffset:
54 self.fd.seek(offset, 0)
55 data = self.fd.read(self.blocksize)
57 self.curoffset += len(data)
58 return gst.FLOW_OK, gst.Buffer(data)
60 return gst.FLOW_UNEXPECTED, None
61 gobject.type_register(FileSource)
65 print 'Usage: %s input output' % (args[0])
68 bin = gst.Pipeline('pipeline')
70 filesrc = FileSource('filesource')
72 filesrc.set_property('location', args[1])
74 filesink = gst.element_factory_make('filesink', 'sink')
75 filesink.set_property('location', args[2])
77 bin.add(filesrc, filesink)
78 gst.element_link_many(filesrc, filesink)
80 bin.set_state(gst.STATE_PLAYING);
81 mainloop = gobject.MainLoop()
83 def bus_event(bus, message):
85 if t == gst.MESSAGE_EOS:
87 elif t == gst.MESSAGE_ERROR:
88 err, debug = message.parse_error()
89 print "Error: %s" % err, debug
92 bin.get_bus().add_watch(bus_event)
95 bin.set_state(gst.STATE_NULL)
97 if __name__ == '__main__':
98 sys.exit(main(sys.argv))