在工业项目的背景下,我使用AI算法开发了一个实时应用程序来检测人。在本地,我使用OPENCV操作帧以获取和显示视频。
目标是将Opencv的帧流式传输到rtmp服务器
FFmpeg似乎是一个不错的选择。但通常从.mp4或几个.jpg开始流媒体以发布流式视频到rtmp服务器。
谢谢。
首先,FFmpeg 可以将流推送到 RTMP 服务器。
你可以尝试为 FFmpeg 命令创建一个子进程,并通过管道传递帧数据。
这是一个简单的示例代码,你可以尝试一下:
import subprocess
import cv2
rtmp_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 = 0
cap = cv2.VideoCapture(path)
# gather video info to ffmpeg
fps = 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 ffmpeg
command = ['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 data
p = 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())
[rtmp @ 000002649f951c40] Cannot open connection tcp://127.0.0.1:1935
我正在Windows上工作,应该怎么办?@admingyu,请您给个答案。 - Gogo-the-CatStream #0:0: Video: rawvideo (BGR[24] / 0x18524742), bgr24, 640x480, 221184 kb/s, 30 tbr, 30 tbn, 30 tbc
rtmp://f7a22cf.channel.media.azure.net:1935/live/c9c612df8: I/O error Traceback (most recent call last): File "C:\Users\singh\source\try.py", line 41, in <module> p.stdin.write(frame.tobytes()) - Brij Raj Singh - MSFT