open cv分割视频

import cv2
import glob
import os
from tqdm import tqdm

is_stati = False
time_split = [2, 3, 4, 5, 6, 12]

if __name__ == '__main__':
    if is_stati:
        path = os.path.join(os.getcwd(), 'result')
        path = path+'/*.mp4'
        pic_list = glob.glob(path)
        print("该目录下总共有:", len(pic_list), '个视频')
    else:
        if not os.path.exists('./result'):
            os.mkdir('./result')
        video_list = glob.glob(r'./*.mp4')
        fourcc = cv2.VideoWriter_fourcc(*'mp4v')


        for video in tqdm(video_list):
            name = video.split('/')[-1]
            cap = cv2.VideoCapture(video)
            fps = int(cap.get(cv2.CAP_PROP_FPS))
            width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
            height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

            name_list = name.split('-')
            assert len(name_list) == 4

            for time_stamp in time_split:
                if name_list[1] == '常规场景':
                    temp = name_list[1] + '_' + name_list[0] + '_' + name_list[2] + '_' + str(time_stamp) + 's.mp4'
                else:
                    temp = '复杂场景_' + name_list[0] + '_' + name_list[2] + '_' + str(time_stamp) + 's.mp4'
                video_path = os.path.join(os.getcwd(), 'result', temp)
                video_writer = cv2.VideoWriter(video_path, fourcc, fps, (width, height))

                start_frame = fps * 1
                end_frame = start_frame + fps * time_stamp

                cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)  # 设置开始帧
                current_frame = cap.get(cv2.CAP_PROP_POS_FRAMES)
                while current_frame < end_frame:  # 从start到end之间读取帧数
                    ret, frame = cap.read()  # 从开始帧开始读取,之后会从开始帧依次往后读取,直到退出循环
                    video_writer.write(frame)  # 利用'写入视频对象'写入帧
                    current_frame = cap.get(cv2.CAP_PROP_POS_FRAMES)  # 获取当前帧数
                video_writer.release()
            cap.release()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值