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)
posted @ 2022-06-14 10:08  漫漫长夜何时休  阅读(113)  评论(0编辑  收藏  举报