【发布时间】:2019-08-04 02:18:34
【问题描述】:
我正在使用 OpenCV 开发一个 Python 模块,该模块连接到 RTSP 流以对视频执行一些预处理(主要是降低 fps 和分辨率),然后将其存储在文件系统中。
但是,即使在尝试了几个编解码器之后,寻找类似的发展......我总是以一个空的视频结束。我见过这个其他线程 (cv::VideoWriter yields unreadable video),它可能类似,但是是在 C++ 上开发的。
有人做过这方面的工作吗?我通常使用示例 RTSP 流作为参考,例如 rtsp://freja.hiof.no:1935/rtplive/definst/hessdalen03.stream,并且可以正确接收甚至观看来自 VLC 的流.
我看到很多讨论如何从 RTSP 流中捕获视频,或如何使用 VideoWriters 和 VideoReaders 类和视频文件的线程,但几乎没有将两者结合起来。
任何帮助将不胜感激 :) 谢谢!!
编辑 1:用于存储帧的示例代码。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import numpy
# Test frame.
width, height = 400, 300
width_2, height_2 = int(width / 2), int(height / 2)
frame = numpy.zeros((height, width, 3), numpy.uint8)
cv2.rectangle(frame, (0, 0), (width_2, height_2), (255, 0, 0), cv2.FILLED)
cv2.rectangle(frame, (width_2, height_2), (width, height), (0, 255, 0), cv2.FILLED)
frames = [frame for _ in range(100)]
fps = 25
# Define the codec.
#fourcc = cv2.VideoWriter_fourcc(*'X264')
#fourcc = cv2.VideoWriter_fourcc(*'XVID')
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
# Create VideoWriter object
out = cv2.VideoWriter(filename='video.avi',
fourcc=fourcc,
apiPreference=cv2.CAP_FFMPEG,
fps=float(fps),
frameSize=(width, height),
isColor=True)
result = 0
for frame in frames:
result += 0 if out.write(frame) is None else 1
print(result)
out.release()
编辑 2:解决方案
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import numpy
# Test frame.
width, height = 400, 300
width_2, height_2 = int(width / 2), int(height / 2)
frame1 = numpy.zeros((height, width, 3), numpy.uint8)
cv2.rectangle(frame1, (0, 0), (width_2, height_2), (255, 0, 0), cv2.FILLED)
cv2.rectangle(frame1, (width_2, height_2), (width, height), (0, 255, 0), cv2.FILLED)
cv2.imwrite('frame1.jpg', frame1)
frame2 = numpy.zeros((height, width, 3), numpy.uint8)
cv2.rectangle(frame2, (width_2, 0), (width, height_2), (255, 0, 0), cv2.FILLED)
cv2.rectangle(frame2, (0, height_2), (width_2, height), (0, 255, 0), cv2.FILLED)
cv2.imwrite('frame2.jpg', frame2)
range1 = [frame1 for _ in range(10)]
range2 = [frame2 for _ in range(10)]
frames = range1 + range2 + range1 + range2 + range1
fps = 2
# Define the codec.
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
# Create VideoWriter object
out = cv2.VideoWriter('video.avi', fourcc, float(fps), (width, height))
for frame in frames:
out.write(frame)
out.release()
【问题讨论】:
-
你能看到直播吗?我的意思是使用 imshow 而不是将其保存到磁盘?您是否可以使用虚拟图像创建视频,例如全红色的图像?您应该逐步进行,首先确保可以连接和检索图像,然后专注于保存....您提供的链接看起来rtmp 链接多于 rtsp,通常端口 554 用于 rtsp,1935 用于 rtmp...
-
嗨@api55!是的,我可以看到从 RTSP 流中检索到的帧。事实上,现在我将它们存储为 jpeg 图像。但是将它们存储为视频应该更有效。但是,正如您所说,这很可能是为了解决问题。我用 numpy 准备了一个虚拟帧,视频仍然出错。请参阅随附的代码,导致 VLC 无法显示 240.924 字节的视频。
-
太好了,现在您的问题有一个 MCVE。我知道获得fourcc +扩展在大多数情况下是一种可怕的体验......首先尝试查看您的机器中安装了哪些编解码器,而不是FOURCC pass -1,并且应该出现一个菜单,您可以选择一个编解码器
-
它返回(在 Ubuntu 上)消息“OpenCV: FFMPEG: format avi / AVI (Audio Video Interleaved)”,以及一个包含 489 个标签的列表,例如“fourcc tag 0x34363248/'H264' codec_id 001B '。例如,它在尝试将其用作fourcc标签时回复“找不到编解码器ID 27的编码器:找不到编码器”,即使它在列表中。
标签: python opencv image-processing video rtsp