Stream video frames directly to youtube in python

Hey guys I programmatically created a video using Moviepy and Gizeh. Right now it saves the final video as a mp4 file. Is it possible to upload that video frame by frame to youtube (livestream) without saving it on my computer ?

Upvotes: 0

Views: 8719

Answers (2)

abhiTronix
abhiTronix

Reputation: 1422

You can use VidGear Library's WriteGear API with its FFmpeg backend which can easily upload any live video frame by frame to YouTube (livestream) over RTMP protocol in few lines of python code as follows:

Without Audio

# import required libraries
from vidgear.gears import CamGear
from vidgear.gears import WriteGear
import cv2

# define and open video source
stream = CamGear(source="/home/foo/foo.mp4", logging=True).start()

# define required FFmpeg parameters for your writer
output_params = {
    "-clones": ["-f", "lavfi", "-i", "anullsrc"],
    "-vcodec": "libx264",
    "-preset": "medium",
    "-b:v": "4500k",
    "-bufsize": "512k",
    "-pix_fmt": "yuv420p",
    "-f": "flv",
}

# [WARNING] Change your YouTube-Live Stream Key here:
YOUTUBE_STREAM_KEY = "xxxx-xxxx-xxxx-xxxx-xxxx"

# Define writer with defined parameters
writer = WriteGear(
    output_filename="rtmp://a.rtmp.youtube.com/live2/{}".format(YOUTUBE_STREAM_KEY),
    logging=True,
    **output_params
)

# loop over
while True:

    # read frames from stream
    frame = stream.read()

    # check for frame if Nonetype
    if frame is None:
        break

    # {do something with the frame here}

    # write frame to writer
    writer.write(frame)

# safely close video stream
stream.stop()

# safely close writer
writer.close()

With Audio

# import required libraries
from vidgear.gears import CamGear
from vidgear.gears import WriteGear
import cv2

# define video source
VIDEO_SOURCE = "/home/foo/foo.mp4"

# Open stream
stream = CamGear(source=VIDEO_SOURCE, logging=True).start()

# define required FFmpeg optimizing parameters for your writer
# [NOTE]: Added VIDEO_SOURCE as audio-source
# [WARNING]: VIDEO_SOURCE must contain audio
output_params = {
    "-i": VIDEO_SOURCE,
    "-acodec": "aac",
    "-ar": 44100,
    "-b:a": 712000,
    "-vcodec": "libx264",
    "-preset": "medium",
    "-b:v": "4500k",
    "-bufsize": "512k",
    "-pix_fmt": "yuv420p",
    "-f": "flv",
}

# [WARNING]: Change your YouTube-Live Stream Key here:
YOUTUBE_STREAM_KEY = "xxxx-xxxx-xxxx-xxxx-xxxx"

# Define writer with defined parameters and
writer = WriteGear(
    output_filename="rtmp://a.rtmp.youtube.com/live2/{}".format(YOUTUBE_STREAM_KEY),
    logging=True,
    **output_params
)

# loop over
while True:

    # read frames from stream
    frame = stream.read()

    # check for frame if Nonetype
    if frame is None:
        break

    # {do something with the frame here}

    # write frame to writer
    writer.write(frame)

# safely close video stream
stream.stop()

# safely close writer
writer.close()

Reference: https://abhitronix.github.io/vidgear/latest/help/writegear_ex/#using-writegears-compression-mode-for-youtube-live-streaming

Upvotes: 3

amirothman
amirothman

Reputation: 157

Hi thanks for posting the question. Yes, it is possible! Since you specifically mentioned Youtube I would refer to these two references:

I wish all the best. Do write if you have more questions

Upvotes: 2

Related Questions