opencv-python保存视频
import cv2
class WVideoManager:
def __init__(self, write_path: str, width: int, height: int, FPS: int = 5, WinName: str = 'WVideoManager'):
fourcc = cv2.VideoWriter_fourcc(*'DIVX') # 视频编解码器
fps = FPS # 帧数
self.width, self.height = width, height # 宽高
assert write_path.endswith('.avi')
self.cap = cv2.VideoWriter(write_path, fourcc, fps, (width, height)) # 写入视频
self.WinName = WinName
self.show = False
def write_one_frame(self, frame, show=False):
if show:
self.show = True
cv2.imshow(self.WinName, frame)
self.cap.write(frame) # 写入帧
def close(self):
self.cap.release()
if self.show:
cv2.destroyWindow(self.WinName)
其中 VideoWriter_fourcc 的值对应不同操作系统会有所区别,对应的值对应相应的视频格式
在Windows中: DIVX(.avi) 在OS中 : MJPG(.mp4),DIVX(.avi),X264(.mkv)
本文来自博客园,作者:漫漫长夜何时休,转载请注明原文链接:https://www.cnblogs.com/ag-chen/p/16373572.html