home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / python-gst0.10 / examples / decodebin.py < prev    next >
Encoding:
Python Source  |  2009-02-21  |  3.0 KB  |  110 lines

  1. #!/usr/bin/env python
  2.  
  3. # decodebin.py - Audio autopluging example using 'decodebin' element
  4. # Copyright (C) 2006 Jason Gerard DeRose <jderose@jasonderose.org>
  5.  
  6. # This library is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU Library General Public
  8. # License as published by the Free Software Foundation; either
  9. # version 2 of the License, or (at your option) any later version.
  10. #
  11. # This library 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 GNU
  14. # Library General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Library General Public
  17. # License along with this library; if not, write to the
  18. # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. # Boston, MA 02111-1307, USA.
  20.  
  21. import sys
  22.  
  23. import gobject
  24. gobject.threads_init()
  25.  
  26. import pygst
  27. pygst.require('0.10')
  28. import gst
  29.  
  30.  
  31. class Decodebin:
  32.     def __init__(self, location):
  33.         # The pipeline
  34.         self.pipeline = gst.Pipeline()
  35.         
  36.         # Create bus and connect several handlers
  37.         self.bus = self.pipeline.get_bus()
  38.         self.bus.add_signal_watch()
  39.         self.bus.connect('message::eos', self.on_eos)
  40.         self.bus.connect('message::tag', self.on_tag)
  41.         self.bus.connect('message::error', self.on_error)
  42.  
  43.         # Create elements
  44.         self.src = gst.element_factory_make('filesrc')
  45.         self.dec = gst.element_factory_make('decodebin')
  46.         self.conv = gst.element_factory_make('audioconvert')
  47.         self.rsmpl = gst.element_factory_make('audioresample')
  48.         self.sink = gst.element_factory_make('alsasink')
  49.         
  50.         # Set 'location' property on filesrc
  51.         self.src.set_property('location', location)
  52.         
  53.         # Connect handler for 'new-decoded-pad' signal 
  54.         self.dec.connect('new-decoded-pad', self.on_new_decoded_pad)
  55.         
  56.         # Add elements to pipeline
  57.         self.pipeline.add(self.src, self.dec, self.conv, self.rsmpl, self.sink)
  58.         
  59.         # Link *some* elements 
  60.         # This is completed in self.on_new_decoded_pad()
  61.         self.src.link(self.dec)
  62.         gst.element_link_many(self.conv, self.rsmpl, self.sink)
  63.         
  64.         # Reference used in self.on_new_decoded_pad()
  65.         self.apad = self.conv.get_pad('sink')
  66.  
  67.         # The MainLoop
  68.         self.mainloop = gobject.MainLoop()
  69.  
  70.         # And off we go!
  71.         self.pipeline.set_state(gst.STATE_PLAYING)
  72.         self.mainloop.run()
  73.         
  74.         
  75.     def on_new_decoded_pad(self, element, pad, last):
  76.         caps = pad.get_caps()
  77.         name = caps[0].get_name()
  78.         print 'on_new_decoded_pad:', name
  79.         if name == 'audio/x-raw-float' or name == 'audio/x-raw-int':
  80.             if not self.apad.is_linked(): # Only link once
  81.                 pad.link(self.apad)
  82.             
  83.             
  84.     def on_eos(self, bus, msg):
  85.         print 'on_eos'
  86.         self.mainloop.quit()
  87.         
  88.         
  89.     def on_tag(self, bus, msg):
  90.         taglist = msg.parse_tag()
  91.         print 'on_tag:'
  92.         for key in taglist.keys():
  93.             print '\t%s = %s' % (key, taglist[key])
  94.             
  95.             
  96.     def on_error(self, bus, msg):
  97.         error = msg.parse_error()
  98.         print 'on_error:', error[1]
  99.         self.mainloop.quit()
  100.  
  101.  
  102.  
  103.  
  104.  
  105. if __name__ == '__main__':
  106.     if len(sys.argv) == 2:
  107.         Decodebin(sys.argv[1])
  108.     else:
  109.         print 'Usage: %s /path/to/media/file' % sys.argv[0]
  110.