使用OpenCV将视频帧添加到列表中会占用大量RAM资源



我使用opencv在磁盘中读取了一个大小为47mb的视频,并将其保存到磁盘上。生成的视频大小为25mb。同时,我创建了一个列表,在其中添加视频中的帧。将此列表转换为数组时,数组的大小为2449.51。为什么会这样呢?可能有一些压缩算法,减少大小,同时保存,我可以应用那些在我的数组?

def read_face(vid_path):
cap = cv2.VideoCapture(vid_path) 
fps = cap.get(cv2.CAP_PROP_FPS)
width  = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH ))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT ))

total_frames=[]
fourcc = cv2.VideoWriter_fourcc(*"XVID")
temp_name='outvide.mp4'
out = cv2.VideoWriter(temp_name,fourcc, fps, (width,height))

while(cap.isOpened()):  # Read until the video is completed
ret, frame = cap.read()    # Capture frame by frame
if ret:
total_frames.append(frame)
out.write(frame)
else:
break
cap.release() 
return np.array(total_frames)

这就是我如何以mb计算帧数组的大小。round(frames.nbytes / 1024 / 1024,2)

我怎样才能减小这个尺寸?

视频以压缩格式保存。MP4, MOV等是表示压缩格式的编解码器,此外还有其他信息,如音频等。

如果你想减少帧列表的大小,你可以在添加帧之前调整帧的大小。

if ret:
cv2.resize(frame, 0.5, 0.5)
total_frames.append(frame)

最新更新