PythonからGStreamerを叩く

#!/usr/bin/python

from __future__ import print_function
import sys
from gi.repository import GLib
import pygst
pygst.require("0.10")
import gst

class HelloGst:

    def __init__(self):
        self.loop = GLib.MainLoop(None, False)
        self.pipeline = gst.Pipeline()
        source = gst.element_factory_make("videotestsrc", "source")
        sink = gst.element_factory_make("autovideosink", "output")
        bus = self.pipeline.get_bus()
        bus.connect("message", self.bus_call)
        bus.add_signal_watch()
        self.pipeline.add(source, sink)
        gst.element_link_many(source, sink)
    
    def bus_call(self, bus, message):
        t = message.type
        if t == gst.MESSAGE_EOS:
            self.loop.quit()
        elif t == gst.MESSAGE_ERROR:
            err, debug = message.parse_error()
            print("Error: %s\n%s" % (err, debug))
            self.loop.quit()
    
    def run(self):
        self.pipeline.set_state(gst.STATE_PLAYING)
        self.loop.run()
        self.pipeline.set_state(gst.STATE_NULL)

if __name__ == '__main__':
    HelloGst().run()

現時点ではGObject Introspectionからだとbus.add_signal_watchが上手く行かないので、おとなしく専用のバインディングを使う。