【发布时间】:2020-06-20 02:37:55
【问题描述】:
几周以来,我一直在尝试使用 opencv 和 gstreamer 在网络上传输 h264 视频,但我经常遇到问题。我编写了一个简单的服务器,它从网络摄像头或文件中捕获帧并通过网络发送。以及拍摄视频的客户。
服务器.py
import cv2
PIPELINE = 'appsrc ! videoconvert ! x264enc tune=zerolatency speed-preset=fast ! rtph264pay ! udpsink host=192.168.0.171 port=5004'
cap_send = cv2.VideoCapture(0)
frame_size = (1280, 720)
out_send = cv2.VideoWriter(PIPELINE, cv2.CAP_GSTREAMER, 0, 16, frame_size, True)
while True:
ret, frame = cap_send.read()
frame = cv2.resize(frame, frame_size)
out_send.write(frame)
cv2.imshow('send', frame)
if cv2.waitKey(1) == 27:
break
客户端.py
import cv2
PIPELINE = 'udpsrc port=5004 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96" ! rtph264depay ! decodebin ! videoconvert ! appsink'
cap_receive = cv2.VideoCapture(PIPELINE, cv2.CAP_GSTREAMER)
while True:
ret, frame = cap_receive.read()
cv2.imshow('receive', frame)
if cv2.waitKey(1) == 27:
break
在控制台中我也得到一个错误:
cv::GStreamerCapture::open OpenCV | GStreamer warning: Cannot query video position: status=1, value=3, duration=-1
有什么问题?我应该指出一些额外的参数吗?还是我的安装错误? 我尝试设置各种参数 synk、比特率、速度预设等,但总是得到残缺的视频。
我也注意到,如果你先尝试Client.py,然后Server.py我得到一个正常的视频,一段时间后它也会变得失真
【问题讨论】: