哔哔one
首先,ffmpeg 具有将流推送到 rtmp 服务器的功能。您可以尝试为 ffmpeg cammand 创建一个子进程,并通过 PIPE 传递您的帧。这是您可以尝试的简单示例代码import subprocessimport cv2rtmp_url = "rtmp://127.0.0.1:1935/stream/pupils_trace"# In my mac webcamera is 0, also you can set a video file name instead, for example "/home/user/demo.mp4"path = 0cap = cv2.VideoCapture(path)# gather video info to ffmpegfps = int(cap.get(cv2.CAP_PROP_FPS))width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))# command and params for ffmpegcommand = ['ffmpeg', '-y', '-f', 'rawvideo', '-vcodec', 'rawvideo', '-pix_fmt', 'bgr24', '-s', "{}x{}".format(width, height), '-r', str(fps), '-i', '-', '-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-preset', 'ultrafast', '-f', 'flv', rtmp_url]# using subprocess and pipe to fetch frame datap = subprocess.Popen(command, stdin=subprocess.PIPE)while cap.isOpened(): ret, frame = cap.read() if not ret: print("frame read failed") break # YOUR CODE FOR PROCESSING FRAME HERE # write to pipe p.stdin.write(frame.tobytes())