如何通过 OpenCV 处理 VLC UDP 流
问题描述
我能够通过 VLC 命令行接收/查看 UDP h264 数据包(即 VLC --network-caching 0 --demux h264 udp://...)
I was able to receive/view UDP h264 packets through VLC command line (i.e. VLC --network-caching 0 --demux h264 udp://...)
我计划通过 OpenCV 算法处理这些帧.但是,我似乎找不到将 VLC 帧发送到我的 Python OpenCV 脚本的方法.
I am planning on processing those frames through OpenCV algorithms. However, I can't seem to find a way to send the VLC frames over to my Python OpenCV script.
是否可以通过管道在单独的脚本中通过 Numpy 处理 VLC 流输出?
Is it possible to pipe VLC stream output to be processed through Numpy in a separate script?
我之前曾尝试使用其 VideoCapture 功能直接流式传输到 OpenCV,但由于某种原因,视频停顿并停止在扭曲的黑色图像上.目前看来,合并 VLC 似乎是唯一的解决方案(尽管我不完全确定为什么 VLC 优于其他方法).
I have previously tried directly streaming to OpenCV by using its VideoCapture function, but for some reason, the video stalls and stops at a distorted black image. For now, it seems like incorporating VLC is the only solution (even though I am not totally sure why VLC works over other methods).
谢谢.
以下是终端上的错误消息片段.前几帧似乎有问题,但我不知道为什么流在 VLC 上工作.从客户端,我先发送了一个默认的关键帧数据,然后发送了video feed h264数据.
The following is a snippet of the error message on the terminal. It seems like there are problems with the first few frames, but I don't know why the stream works on VLC. From the client, I first sent a default key frame data, and then sent video feed h264 data.
[h264 @ 0x7f9c50020200] top block unavailable for requested intra mode -1
[h264 @ 0x7f9c50020200] error while decoding MB 7 0, bytestream 7208
[h264 @ 0x7f9c50020200] top block unavailable for requested intra mode -1
[h264 @ 0x7f9c50020200] error while decoding MB 8 9, bytestream 7381
解决方案
你可以使用 ffmpeg用于流式传输.
You can use ffmpeg for streaming.
在终端中首次测试 ffmpeg 流.在 linux 中,我们使用 v4l2
从相机中抓取帧.
First test ffmpeg streaming in terminal. In linux, we use v4l2
to grab frames from camera.
服务器:
ffmpeg -f v4l2 -i /dev/video0 -preset ultrafast -vcodec libx264 -tune zerolatency -b 900k -f h264 udp://127.0.0.1:5000
客户:
ffplay udp://127.0.0.1:5000
如果您能够在客户端查看流,那么我们可以使用 OpenCV 进行图像处理.OpenCV 必须有 ffmepg 支持.有关 ffmpeg 支持检查,请参阅 此链接.
If you're able to view the stream on the client side, then we can use OpenCV for image processing. OpenCV must have ffmepg support. See this link for ffmpeg support check.
cap = cv2.VideoCapture('udp://127.0.0.1:5000',cv2.CAP_FFMPEG)
if not cap.isOpened():
print('VideoCapture not opened')
exit(-1)
while True:
ret, frame = cap.read()
if not ret:
print('frame empty')
break
cv2.imshow('image', frame)
if cv2.waitKey(1)&0XFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
相关文章