Created
January 19, 2017 07:28
-
-
Save quasoft/6b48dd8f101955b82a55637a6ee8b3db to your computer and use it in GitHub Desktop.
Example for using tee for both playing and saving audio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 tee | |
tee = Gst.ElementFactory.make('tee') | |
audio_sink.add(tee) | |
q1 = Gst.ElementFactory.make('queue') | |
audio_sink.add(q1) | |
tee_q1_pad = tee.get_request_pad("src_%u") | |
q1_pad = q1.get_static_pad('sink') | |
tee_q1_pad.link(q1_pad) | |
q2 = Gst.ElementFactory.make('queue') | |
audio_sink.add(q2) | |
tee_q2_pad = tee.get_request_pad("src_%u") | |
q2_pad = q2.get_static_pad('sink') | |
tee_q2_pad.link(q2_pad) | |
# Queue #1: Encode audio to vorbis and save in ogg file | |
# ----------------------------------------------------- | |
encoder = Gst.ElementFactory.make('vorbisenc') | |
audio_sink.add(encoder) | |
q1.link(encoder) | |
oggmux = Gst.ElementFactory.make('oggmux') | |
audio_sink.add(oggmux) | |
encoder.link(oggmux) | |
filesink = Gst.ElementFactory.make('filesink') | |
filesink.set_property('location', r'/home/user/test.ogg') | |
filesink.set_property('sync', 'true') | |
audio_sink.add(filesink) | |
oggmux.link(filesink) | |
# Queue #2: Play audio with sound card | |
# ----------------------------------------------------- | |
# Create element to play the pipeline to hardware | |
sink = Gst.ElementFactory.make('autoaudiosink') | |
audio_sink.add(sink) | |
q2.link(sink) | |
audio_sink.add_pad(Gst.GhostPad.new('sink', tee.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://stream.radioreklama.bg:80/radio1rock.ogg') | |
# 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 change output file location on the fly...') | |
# TODO: do not interrupt playback | |
player.set_state(Gst.State.READY) | |
filesink.set_property('location', r'/home/user/test2.ogg') | |
player.set_state(Gst.State.PLAYING) | |
# 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