import subprocess import cv2# YouTube streaming settingsYOUTUBE_URL = "rtmp://a.rtmp.youtube.com/live2/"KEY = "..."# OpenCV camera setupcap = cv2.VideoCapture(0)cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)# FFmpeg command for streamingcommand = [r"C:\utility\ffmpeg\ffmpeg-2024-02-22-git-76b2bb96b4-full_build\ffmpeg-2024-02-22-git-76b2bb96b4-full_build\bin\ffmpeg.exe",'-f', 'rawvideo','-pix_fmt', 'bgr24','-s', '640x480','-i', '-','-ar', '44100','-ac', '2','-acodec', 'pcm_s16le','-f', 's16le','-ac', '2','-i', 'NUL', '-acodec', 'aac','-ab', '128k','-strict', 'experimental','-vcodec', 'h264','-pix_fmt', 'yuv420p','-g', '50','-vb', '1000k','-profile:v', 'baseline','-preset', 'ultrafast','-r', '30','-f', 'flv', f"{YOUTUBE_URL}/{KEY}",]# Open a subprocess with FFmpegpipe = subprocess.Popen(command, stdin=subprocess.PIPE)while True: # Read a frame from the camera ret, frame = cap.read() if not ret: break # Display the frame cv2.imshow('Frame', frame) cv2.waitKey(1) # Wait for 1ms # Send the frame through the pipe for streaming pipe.stdin.write(frame.tobytes()) # Check for 'q' key press to stop streaming if cv2.waitKey(1) & 0xFF == ord('q'): break# Release resourcescap.release()cv2.destroyAllWindows()I'm trying to implement capturing the camera screen using opencv and transmitting this frame to the YouTube streaming broadcast via ffmpeg. YouTube streaming does start when I run this code. However, it appears to be a black screen, not a camera screen. I don't see what the problem is.
I didn't even start streaming at first, but I changed the command option to various things, and when I ran the code, I succeeded in starting streaming. There are many references to transmitting mp4, but there are not many references to transmitting real-time capture. I'm going to process the camera screen using opencv and then send it to streaming. I don't know what the problem is. Please help me.