Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from gi.repository import GLib
- from gi.repository import GObject
- from gi.repository import Gst
- from gi.repository import Gtk
- # Needed for window.get_xid(), xvimagesink.set_window_handle(), respectively:
- from gi.repository import GdkX11, GstVideo
- GObject.threads_init()
- Gst.init(None)
- class TestFilter(Gst.Element):
- __gstmetadata__ = (
- "TestFilter plugin",
- "test_filter.py",
- "Description",
- "Contact"
- )
- # src pad template: used to forward buffers
- _srctemplate = Gst.PadTemplate.new('video_src',
- Gst.PadDirection.SRC,
- Gst.PadPresence.ALWAYS,
- Gst.caps_from_string('video/x-raw, format=(string){ YV12, AYUV, YUY2, UYVY }')
- )
- # sink pad template: used to receive buffers
- _sinktemplate = Gst.PadTemplate.new('video_sink',
- Gst.PadDirection.SINK,
- Gst.PadPresence.ALWAYS,
- Gst.caps_from_string('video/x-raw, format=(string){ YV12, AYUV, YUY2, UYVY }')
- )
- # register pad templates
- __gsttemplates__ = (_srctemplate, _sinktemplate)
- def __init__(self):
- Gst.Element.__init__(self)
- # src pad
- self.srcpad = Gst.Pad.new_from_template(self._srctemplate, 'video_src')
- # sink pad
- self.sinkpad = Gst.Pad.new_from_template(self._sinktemplate, 'video_sink')
- self.sinkpad.set_chain_function_full(self._sink_chain)
- # add pads
- self.add_pad(self.srcpad)
- self.add_pad(self.sinkpad)
- def _sink_chain(self, pad, parent, buf):
- # ... do analyzing stuff ...
- # forward buffer
- return self.srcpad.push(buf)
- if __name__=='__main__':
- def on_message(bus, message):
- if message.type == Gst.MessageType.EOS:
- player.set_state(Gst.State.NULL)
- elif message.type == Gst.MessageType.ERROR:
- player.set_state(Gst.State.NULL)
- (err, debug) = message.parse_error()
- print "Error: %s" % err, debug
- def on_sync_message(bus, message):
- if message.get_structure().get_name() == "prepare-window-handle":
- win_id = videowidget.get_property('window').get_xid()
- imagesink = message.src
- imagesink.set_property("force-aspect-ratio", True)
- imagesink.set_window_handle(win_id)
- def on_delete(*args):
- player.set_state(Gst.State.NULL)
- Gtk.main_quit()
- win = Gtk.Window()
- win.connect("delete_event", on_delete)
- videowidget = Gtk.DrawingArea()
- videowidget.set_size_request(400, 250)
- vbox = Gtk.VBox(False, 0)
- vbox.pack_start(videowidget, True, True, 0)
- win.add(vbox)
- win.show_all()
- # Setup GStreamer
- player = Gst.ElementFactory.make("playbin", "MultimediaPlayer")
- bus = player.get_bus()
- bus.add_signal_watch()
- bus.enable_sync_message_emission()
- bus.connect("message", on_message)
- bus.connect("sync-message::element", on_sync_message)
- # create a Bin containing my own TestFilter, textoverlay and autovideosink
- testfilter = TestFilter()
- textoverlay = Gst.ElementFactory.make('textoverlay', 'textoverlay')
- autovideosink = Gst.ElementFactory.make('autovideosink')
- video_bin = Gst.Bin.new('video_bin')
- video_bin.add(testfilter)
- video_bin.add(textoverlay)
- video_bin.add(autovideosink)
- sink_pad = Gst.GhostPad.new('sink', testfilter.get_static_pad('video_sink'))
- video_bin.add_pad(sink_pad)
- testfilter.link(textoverlay)
- textoverlay.link(autovideosink)
- player.set_property('video-sink', video_bin)
- textoverlay.set_property('text', 'ABC')
- player.set_property('uri', "file:///.../somefile.avi")
- player.set_state(Gst.State.PLAYING)
- Gtk.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement