I have a Python script running on a Raspberry Pi that currently shows live feeds of multiple 3D printers arranged in a 4 by 4 grid. The script uses OpenCV to capture and display the video streams. However, I'm struggling to figure out how to livestream this window's content to YouTube.
Here's a simplified version of my current code:
import numpy as npimport requestsfrom threading import Threadimport cv2# Dictionary of printer URLsprinter_urls = { # [URLs of various printers IP addresses]}# Function to fetch print job data from each printerdef fetch_print_job_data(url): try: response = requests.get(f"{url}/api/v1/print_job") # [Handle response and error] except Exception as e: return None# [Additional utility functions, e.g., calculate_remaining_time()]# Function to stream video from each printerdef stream_video(url, printer, output): stream_url = f"{url}:8080?action=stream" cap = cv2.VideoCapture(stream_url) while True: ret, frame = cap.read() if not ret: break remaining_seconds = fetch_print_job_data(url) remaining_time_str = calculate_remaining_time(remaining_seconds) output[printer] = (frame, remaining_time_str)# Function to update and display streams in a grid layoutdef update_streams(): stream_threads = [] outputs = {} for printer, url in printer_urls.items(): outputs[printer] = None t = Thread(target=stream_video, args=(url, printer, outputs)) t.daemon = True t.start() stream_threads.append(t) while True: # [Code to display streams in a 4x4 grid window] # [Using OpenCV functions to handle window display and layout]update_streams()My goal is to livestream this video to YouTube directly from the Raspberry Pi. While I've had some success using vidgear.gears and FFmpeg for streaming my webcam, I am yet to get a window to stream online.