Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 ; tab-width: 4 ; indent-tabs-mode: t -*-
- # gobject
- import gobject
- gobject.threads_init()
- # /usr/share/gst-python/0.10/examples/decodebin.py
- # GST
- import pygst
- pygst.require("0.10")
- import gst
- import time
- class TwoFiles:
- def __init__(self):
- self.index = 0
- def run(self):
- self.count = 0
- self.createPipeline()
- self.timer = gobject.timeout_add(2000, self.on_timeout)
- # The MainLoop
- self.mainloop = gobject.MainLoop()
- # And off we go!
- print self.pipeline.set_state(gst.STATE_PLAYING)
- self.mainloop.run()
- def createPipeline(self):
- self.pipeline = gst.Pipeline('pipeline')
- self.src = gst.element_factory_make('alsasrc', 'source')
- self.queue = gst.element_factory_make('queue', 'queue')
- self.conv = gst.element_factory_make('audioconvert', 'converter')
- self.enc = gst.element_factory_make('vorbisenc', 'encoder')
- self.mux = gst.element_factory_make('oggmux', 'multiplexer')
- self.file = gst.element_factory_make('filesink', 'file')
- self.pipeline.add(self.src, self.queue, self.conv, self.enc, self.mux, self.file)
- if not gst.element_link_many(self.src, self.queue, self.conv, self.enc, self.mux, self.file):
- print "Failed to link elements!"
- # Set 'location' property on filesrc
- self.file.set_property('location', self.fileName())
- self.bus = self.pipeline.get_bus()
- self.bus.add_signal_watch()
- self.bus.connect('message', self.on_message)
- self.bus.connect('message::eos', self.on_eos)
- self.bus.connect('message::tag', self.on_tag)
- self.bus.connect('message::error', self.on_error)
- def fileName(self):
- return "./file%02i.ogg"%self.index
- def on_message(self, bus, message):
- pass #print "on_message: ", message
- def on_timeout(self):
- if self.index < 7:
- self.index = self.index + 1
- if self.index % 2:
- print " (0) -> in_timeout: self.pipeline.set_state(gst.STATE_NULL) starting:"
- ret = self.pipeline.set_state(gst.STATE_NULL)
- print " (1) -> in_timeout: return = ", ret
- print " (2) -> in_timeout: self.pipeline.set_state(gst.STATE_NULL) done."
- print " (3) -> on_timeout: state = ",
- print self.pipeline.get_state()
- else:
- print " (4) -> in_timeout: self.pipeline.set_state(gst.STATE_PLAYING) starting:"
- ret = self.pipeline.set_state(gst.STATE_PLAYING)
- print " (5) -> in_timeout: return = ", ret
- print " (6) -> in_timeout: self.pipeline.set_state(gst.STATE_PLAYING) done."
- print " (7) -> on_timeout: state = ",
- print self.pipeline.get_state()
- return True
- else:
- self.pipeline.post_message(gst.message_new_eos(self.pipeline))
- return False
- def on_eos(self, bus, message):
- print "on_eos: ", bus, message
- if self.pipeline.set_state(gst.STATE_NULL) != gst.STATE_CHANGE_SUCCESS:
- print "error: cannot close pipeline properly!!!"
- self.mainloop.quit()
- def on_tag(self, bus, message):
- print "on_tag: ", bus, message
- def on_error(self, bus, msg):
- error = msg.parse_error()
- print "on_error: ", error[1]
- bus.remove_signal_watch()
- print self.pipeline.set_state(gst.STATE_NULL)
- self.mainloop.quit()
- if __name__ == '__main__':
- i = TwoFiles()
- i.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement