Skip to content

Instantly share code, notes, and snippets.

@markwingerd
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markwingerd/a3621cf1dc9d379afcb6 to your computer and use it in GitHub Desktop.
Save markwingerd/a3621cf1dc9d379afcb6 to your computer and use it in GitHub Desktop.
GStreamer Tutorial 2: Whole Project - Streams multimedia using Playbin2
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
GStreamer Tutorial 2: Playbin2
In this tutorial we will stream a video from a website using
the playbin2 element, change the video using a solarize element
and output the video. The audio is played by playbin2.
---------------------output_bin--------------------------
playbin2 -> | decodebin -> autoconvert -> solarize -> autovideosink |
---------------------------------------------------------
"""
import gst
# Callback function to link decodebin to autoconvert.
def on_new_decoded_pad(dbin, pad, islast):
decode = pad.get_parent()
pipeline = decode.get_parent()
convert = pipeline.get_by_name('convert')
decode.link(convert)
print 'linked!'
# Create the pipeline and bin
output_bin = gst.Bin('output_bin')
# Create the elements.
media_source = gst.element_factory_make('playbin2', 'media_source')
decode = gst.element_factory_make('decodebin', 'decode')
convert = gst.element_factory_make('autoconvert', 'convert')
solarize = gst.element_factory_make('solarize', 'solarize')
video_sink = gst.element_factory_make("autovideosink", "video_sink")
# Ensure all elements were created successfully.
if (not output_bin or not media_source or not decode or not convert or
not solarize or not video_sink):
print 'Elements could not be linked.'
exit(-1)
# Configure our elements.
#media_source.set_property('uri', 'file:///home/reimus/Public/sintel_trailer-480p.webm')
media_source.set_property('uri', 'http://docs.gstreamer.com/media/sintel_trailer-480p.webm')
# Add elements to our bin
output_bin.add(decode, convert, solarize, video_sink)
# Link decodebin with autoconvert when its source pad has been created.
decode.connect('new-decoded-pad', on_new_decoded_pad)
# Link the rest of our elements together.
if not gst.element_link_many(convert, solarize, video_sink):
print 'Not all elements could link.'
# Create pads for the bin.
decode_pad = decode.get_static_pad('sink')
ghost_pad = gst.GhostPad('sink', decode_pad)
ghost_pad.set_active(True)
output_bin.add_pad(ghost_pad)
# Set media_source's video sink.
media_source.set_property('video-sink', output_bin)
# Set our pipeline state to Playing.
media_source.set_state(gst.STATE_PLAYING)
# Wait until error or EOS.
bus = media_source.get_bus()
msg = bus.timed_pop_filtered(gst.CLOCK_TIME_NONE,
gst.MESSAGE_ERROR | gst.MESSAGE_EOS)
print msg
# Free resources.
media_source.set_state(gst.STATE_NULL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment