opencv 截取视频

import cv2
start_time, end_time = 0, 10
cap = cv2.VideoCapture(file_dir + name)  # 读取视频
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # 定义mp4编解码器
name_new = 'test.mp4' # 保存新视频名字
cap.set(cv2.CAP_PROP_POS_MSEC, start_time * 1000) # 进行视频定位播放,单位毫秒
out = cv2.VideoWriter(name_new, fourcc, cap.get(5),      # ,创建VideoWriter 对象, get(5)是fps, 3是width, 4是height
                      (int(cap.get(3)), int(cap.get(4))))
if cap.isOpened():
    ret, frame = cap.read()
    while ret:
        if cap.get(cv2.CAP_PROP_POS_MSEC) >= end_time*1000:  # 超出结束时间的时候,结束
            break
        out.write(frame)          # 写入
        ret, frame = cap.read()   # 读取下一帧
cap.release()
out.release()
posted @ 2021-12-11 20:18  一无所知小白龙  阅读(620)  评论(0编辑  收藏  举报