在工作中刚好用到,按自己的需求写了一段

import cv2
cap = cv2.VideoCapture('video.mp4')
cap.isOpened()
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)

print(width,height)

if cap.isOpened():  # 当成功打开视频时cap.isOpened()返回True,否则返回False
    # get方法参数按顺序对应下表(从0开始编号)
    rate = cap.get(5)  # 帧速率
    FrameNumber = int(cap.get(7))  # 视频文件的帧数
    duration = FrameNumber / rate  # 帧速率/视频总帧数 是时间,除以60之后单位是分钟
    fps = int(rate)  #每一段小视频帧数

    i = 0
    while (True):
        success, frame = cap.read()
        if success:
            i += 1
            if (i % fps == 1):
                videoWriter = cv2.VideoWriter(str(i) + '.mp4',
                                              cv2.VideoWriter_fourcc('D', 'I', 'V', 'X'), fps,
                                              (int(width), int(height)))
                videoWriter.write(frame)
            else:
                videoWriter.write(frame)
        else:
            print('end')
            break

cap.release()

 

posted on 2019-11-28 18:55  tianyun620520  阅读(2856)  评论(1编辑  收藏  举报