Advertisement
Guest User

Juraj Kubelka

a guest
Jun 16th, 2009
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.21 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 ; tab-width: 4 ; indent-tabs-mode: t -*-
  3.  
  4. # gobject
  5. import gobject
  6. gobject.threads_init()
  7. # /usr/share/gst-python/0.10/examples/decodebin.py
  8.  
  9. # GST
  10. import pygst
  11. pygst.require("0.10")
  12. import gst
  13.  
  14. import time
  15.  
  16.  
  17. class TwoFiles:
  18.  
  19.     def __init__(self):
  20.         self.index = 0
  21.  
  22.     def run(self):
  23.         self.count = 0
  24.         self.createPipeline()
  25.         self.timer = gobject.timeout_add(2000, self.on_timeout)
  26.  
  27.         # The MainLoop
  28.         self.mainloop = gobject.MainLoop()
  29.  
  30.         # And off we go!
  31.         print self.pipeline.set_state(gst.STATE_PLAYING)
  32.         self.mainloop.run()
  33.  
  34.     def createPipeline(self):
  35.         self.pipeline = gst.Pipeline('pipeline')
  36.        
  37.         self.src   = gst.element_factory_make('alsasrc', 'source')
  38.         self.queue = gst.element_factory_make('queue', 'queue')
  39.         self.conv  = gst.element_factory_make('audioconvert', 'converter')
  40.         self.enc   = gst.element_factory_make('vorbisenc', 'encoder')
  41.         self.mux   = gst.element_factory_make('oggmux', 'multiplexer')
  42.         self.file  = gst.element_factory_make('filesink', 'file')
  43.         self.pipeline.add(self.src, self.queue, self.conv, self.enc, self.mux, self.file)
  44.         if not gst.element_link_many(self.src, self.queue, self.conv, self.enc, self.mux, self.file):
  45.             print "Failed to link elements!"
  46.  
  47.         # Set 'location' property on filesrc
  48.         self.file.set_property('location', self.fileName())
  49.  
  50.         self.bus = self.pipeline.get_bus()
  51.         self.bus.add_signal_watch()
  52.         self.bus.connect('message', self.on_message)
  53.         self.bus.connect('message::eos', self.on_eos)
  54.         self.bus.connect('message::tag', self.on_tag)
  55.         self.bus.connect('message::error', self.on_error)
  56.  
  57.     def fileName(self):
  58.         return "./file%02i.ogg"%self.index
  59.  
  60.     def on_message(self, bus, message):
  61.             pass #print "on_message: ", message
  62.  
  63.     def on_timeout(self):
  64.         if self.index < 7:
  65.                 self.index = self.index + 1
  66.                 if self.index % 2:
  67.                         print "  (0) -> in_timeout: self.pipeline.set_state(gst.STATE_NULL) starting:"
  68.                         ret = self.pipeline.set_state(gst.STATE_NULL)
  69.                         print "  (1) -> in_timeout: return = ", ret
  70.                         print "  (2) -> in_timeout: self.pipeline.set_state(gst.STATE_NULL) done."
  71.                         print "  (3) -> on_timeout: state = ",
  72.                         print self.pipeline.get_state()
  73.                 else:
  74.                         print "  (4) -> in_timeout: self.pipeline.set_state(gst.STATE_PLAYING) starting:"
  75.                         ret = self.pipeline.set_state(gst.STATE_PLAYING)
  76.                         print "  (5) -> in_timeout: return = ", ret
  77.                         print "  (6) -> in_timeout: self.pipeline.set_state(gst.STATE_PLAYING) done."
  78.                         print "  (7) -> on_timeout: state = ",
  79.                         print self.pipeline.get_state()
  80.                 return True
  81.         else:
  82.             self.pipeline.post_message(gst.message_new_eos(self.pipeline))
  83.             return False
  84.  
  85.     def on_eos(self, bus, message):
  86.         print "on_eos: ", bus, message
  87.         if self.pipeline.set_state(gst.STATE_NULL) != gst.STATE_CHANGE_SUCCESS:
  88.             print "error: cannot close pipeline properly!!!"
  89.         self.mainloop.quit()
  90.  
  91.     def on_tag(self, bus, message):
  92.         print "on_tag: ", bus, message
  93.  
  94.     def on_error(self, bus, msg):
  95.         error = msg.parse_error()
  96.         print "on_error: ", error[1]
  97.         bus.remove_signal_watch()
  98.         print self.pipeline.set_state(gst.STATE_NULL)
  99.         self.mainloop.quit()
  100.  
  101.  
  102. if __name__ == '__main__':
  103.     i = TwoFiles()
  104.     i.run()
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement