0

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 ?

  • 1
    Please elaborate more to get better answers. You can explore more on this library: https://github.com/scivision/PyLivestream – Love Sharma Apr 01 '20 at 03:10

2 Answers2

2

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

amirothman
  • 157
  • 1
  • 11
1

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

abhiTronix
  • 1,248
  • 13
  • 17
  • Can this be also used to stream live video? I have a use case, where at a client site, I am using live video from the camera installed there, performing object detection on it, and then continuously uploading it on youtube as well? – S Andrew Jul 17 '22 at 19:46
  • @SAndrew Absolutely yes. – abhiTronix Jul 20 '22 at 03:38