Skip to content

Instantly share code, notes, and snippets.

@tylercubell
Created February 14, 2019 16:17
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tylercubell/36e86edcba814758969181f478b6006a to your computer and use it in GitHub Desktop.
Save tylercubell/36e86edcba814758969181f478b6006a to your computer and use it in GitHub Desktop.
GStreamer Python YouTube Livestream
import os, gi
gi.require_version("Gst", "1.0")
from gi.repository import GObject, Gst
Gst.init(None)
pipeline = Gst.Pipeline()
# Variables.
youtube_stream_key = "your_key_here"
encoder_speed = "ultrafast"
# Video elements.
videotestsrc = Gst.ElementFactory.make("videotestsrc")
videotestsrc.set_property("is-live", True)
pipeline.add(videotestsrc)
videoconvert = Gst.ElementFactory.make("videoconvert")
pipeline.add(videoconvert)
clockoverlay = Gst.ElementFactory.make("clockoverlay")
clockoverlay.set_property("halignment", "center")
clockoverlay.set_property("valignment", "top")
clockoverlay.set_property("shaded-background", True)
clockoverlay.set_property("font-desc", "Sans, 60")
pipeline.add(clockoverlay)
videorate = Gst.ElementFactory.make("videorate")
videorate.set_property("drop-only", True)
pipeline.add(videorate)
x264enc = Gst.ElementFactory.make("x264enc")
x264enc.set_property("bitrate", 4000)
x264enc.set_property("tune", "zerolatency")
x264enc.set_property("key-int-max", 60)
x264enc.set_property("speed-preset", encoder_speed)
x264enc.set_property("option-string", "keyint=60:min-keyint=60")
pipeline.add(x264enc)
queue = Gst.ElementFactory.make("queue")
pipeline.add(queue)
flvmux = Gst.ElementFactory.make("flvmux")
flvmux.set_property("streamable", True)
pipeline.add(flvmux)
rtmpsink = Gst.ElementFactory.make("rtmpsink")
rtmpsink.set_property("location", "rtmp://a.rtmp.youtube.com/live2/" + youtube_stream_key)
pipeline.add(rtmpsink)
video_caps = Gst.Caps.from_string("video/x-raw,width=1280,height=720,framerate=30/1")
videotestsrc.link_filtered(videoconvert, video_caps)
videoconvert.link(clockoverlay)
clockoverlay.link(videorate)
videorate.link(x264enc)
x264enc.link(queue)
queue.link(flvmux)
flvmux.link(rtmpsink)
# Audio elements.
audiosrc = Gst.ElementFactory.make("audiotestsrc")
audiosrc.set_property("is-live", True)
pipeline.add(audiosrc)
audiorate = Gst.ElementFactory.make("audiorate")
pipeline.add(audiorate)
audioconvert = Gst.ElementFactory.make("audioconvert")
pipeline.add(audioconvert)
audioresample = Gst.ElementFactory.make("audioresample")
pipeline.add(audioresample)
voaacenc = Gst.ElementFactory.make("voaacenc")
voaacenc.set_property("bitrate", 96000)
pipeline.add(voaacenc)
audio_caps = Gst.Caps.from_string("audio/x-raw,rate=48000")
audiosrc.link_filtered(audiorate, audio_caps)
audiorate.link(audioconvert)
audioconvert.link(audioresample)
audioresample.link(voaacenc)
voaacenc.link(flvmux)
# Start pipeline.
pipeline.set_state(Gst.State.PLAYING)
while True:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment