视频转图片&图片生成视频

import os
import cv2 as cv


class VideoProcess(object):
    def __init__(self, fps=25, output_path="./output_path/"):
        self.fps = fps
        self.output_path = output_path
        # if not os.path.exists(self.output_path):
        #     os.mkdir(self.output_path)

    def video2frames(self, video_path):
        video_capture = cv.VideoCapture(video_path)  # 读取视频文件
        count = 0
        while True:
            ret, frame = video_capture.read()  # 按帧读取视频,
            if not ret:  # 判断视频解析是否结束
                break
            img_name = self.output_path + f"{count+1:08d}" + ".jpg"
            cv.imwrite(img_name, frame)
            count += 1
            print(f"已经存储{count+1:08d}张图片了...")
            # print("已经存储{%6d}张图片了...".format(count))
        print("convert successfully")

    def frames2video(self, image_dir, video_width, video_height):
        # video_dir = os.path.join(self.output_path)
        fourcc = cv.VideoWriter_fourcc('M', 'J', 'P', 'G')
        frame_size = (video_width, video_height)
        videowriter = cv.VideoWriter(self.output_path, fourcc, self.fps, frame_size)

        frames = sorted(os.listdir(image_dir))
        for frame in frames:
            f_path = os.path.join(image_dir, frame)
            image = cv.imread(f_path)
            image = cv.resize(src=image, dsize=(video_width, video_height))
            videowriter.write(image)
            print(frame + " has been written!")
        videowriter.release()


def demo_frame2video():
    video_path = "./VideoFrame/video-3.avi"
    frame2video_converter = VideoProcess(output_path=video_path)
    image_dir = "./vidao_pics/"
    video_width = 1500
    video_height = 1000
    frame2video_converter.frames2video(image_dir, video_width, video_height)


def demo_video2frame():
    frame_path = "./frames-1/"
    video2frame_converter = VideoProcess(output_path=frame_path)
    video_path = "./1.ts"
    video2frame_converter.video2frames(video_path)

if __name__ == "__main__":
    demo_frame2video()
    demo_video2frame()


评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值