我想读取rtsp视频源,添加覆盖文本并将其推送到RTMP端点。我使用Videocapture读取视频源,并使用python子流程将帧写回到RTMP端点。我提到这个FFmpeg stream video to rtmp from frames OpenCV python
import sys
import subprocess
import cv2
import ffmpeg
rtmp_url = "rtmp://127.0.0.1:1935/live/test"
path = 0
cap = cv2.VideoCapture("rtsp://10.0.1.7/media.sdp")
# 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 = ['ffmpeg', '-i', '-', "-c", "copy", '-f', 'flv', rtmp_url]
p = subprocess.Popen(command, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
font = cv2.FONT_HERSHEY_SIMPLEX
while cap.isOpened():
ret, frame = cap.read()
cv2.putText(frame, 'TEXT ON VIDEO', (50, 50), font, 1, (0, 255, 255), 2, cv2.LINE_4)
cv2.imshow('video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if not ret:
print("frame read failed")
break
try:
p.stdin.write(frame.tobytes())
except Exception as e:
print (e)
cap.release()
p.stdin.close()
p.stderr.close()
p.wait()
python脚本返回“[Errno 32]断开的管道”。在终端中运行ffmpeg命令可以正常工作
ffmpeg -i rtsp://10.0.1.7/media.sdp -c copy -f flv rtmp://127.0.0.1:1935/live/test
上面的命令运行良好,我可以将输入流推送到RTMP端点。但我无法将处理后的帧写入运行ffmpeg的子进程
如果我错过了什么,请告诉我
将原始帧写入stdin管道时不能使用
"-c", "copy"
由
ret, frame = cap.read()
返回的frame
是BGR颜色格式的uint8
NumPy数组(cap.read()
解码视频并转换颜色格式)。在
FFmpeg
术语中frame
格式为“rawvideo
”command
应该告诉FFmpeg希望原始视频作为输入,具体大小和像素格式:command = ['ffmpeg', '-f', 'rawvideo', '-s', f'{width}x{height}', '-pixel_format', 'bgr24', ...
因为输入是原始视频,我们必须对其重新编码。
我们可以指定编码像素格式和视频编解码器:
'-pix_fmt', 'yuv420p', '-c:v', 'libx264' ...
备注:
很少使用FFmpeg参数而不作解释(如
'-bufsize', '64M'
)李>执行侦听器应用程序:
如果没有接收视频的“侦听器”,RTMP流媒体将无法工作。
应在启动RTMP流之前启动侦听器(由于TCP使用)
我们可以使用FFplay子进程作为“侦听器”应用程序:
流式合成视频帧:
从一个简单的代码示例开始,它流式传输合成帧(不捕获RTSP视频)
以下“自包含”代码示例在灰色背景上写入黄色文本,并将帧传递给FFmpeg以进行RTMP流传输:
输出示例:
从RTSP流捕获视频帧。
以下代码示例从public RTSP stream捕获视频帧,写入文本,并将帧传递给FFmpeg进行RTMP流传输:
输出示例:
相关问题 更多 >
编程相关推荐