Skip to content

Instantly share code, notes, and snippets.

@quasoft
Created October 8, 2016 19:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quasoft/a83d359e28ab776bf1a21cc85c4bdd01 to your computer and use it in GitHub Desktop.
Save quasoft/a83d359e28ab776bf1a21cc85c4bdd01 to your computer and use it in GitHub Desktop.
Using GStreamer with playbin and custom sink as radio player
#!/usr/bin/env python3
import gi
import threading
gi.require_version('Gst', '1.0')
gi.require_version('GstBase', '1.0')
gi.require_version('Gtk', '3.0')
from gi.repository import GObject, Gst
# Initialize threads
GObject.threads_init()
# Initialize GStreamer
Gst.init(None)
# Handle song metadata
def on_tag(bus, msg):
taglist = msg.parse_tag()
print('%s: %s' % (taglist.nth_tag_name(0), taglist.get_string(taglist.nth_tag_name(0)).value))
# Create a custom bin element, that will serve as audio sink to
# player bin. Audio filters will be added to this sink.
audio_sink = Gst.Bin.new('audiosink')
# Create element to attenuate/amplify the signal
amplify = Gst.ElementFactory.make('audioamplify')
amplify.set_property('amplification', 0.1)
audio_sink.add(amplify)
# Create element to play the pipeline to hardware
sink = Gst.ElementFactory.make('autoaudiosink')
audio_sink.add(sink)
amplify.link(sink)
audio_sink.add_pad(Gst.GhostPad.new('sink', amplify.get_static_pad('sink')))
# Create playbin and add the custom audio sink to it
player = Gst.ElementFactory.make("playbin", "player")
player.props.audio_sink = audio_sink
# Set URI to online radio
player.set_property('uri', 'http://...')
# Start playing
player.set_state(Gst.State.PLAYING)
# Listen for metadata
bus = player.get_bus()
bus.enable_sync_message_emission()
bus.add_signal_watch()
bus.connect('message::tag', on_tag)
loop = GObject.MainLoop()
threading.Thread(target=loop.run).start()
# Let user stop player gracefully
input('Press enter to stop playing...')
# Stop loop
player.set_state(Gst.State.NULL)
loop.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment