python opencv截取视频

import cv2


video_path = "D:/Data/无人机视频/DCIM/100MEDIA/DJI_0002.MOV"
video = cv2.VideoCapture(video_path)
# 需要明确视频保存的格式
fourcc = cv2.VideoWriter_fourcc(*'XVID')
fps = video.get(cv2.CAP_PROP_FPS)
print('帧率:%d' % fps)
size = (int(video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)))
out = cv2.VideoWriter('out.avi', fourcc, fps, size)
print("视频总帧数", video.get(7))
# 设置视频截取的开始时间
frameToStart = 6200
video.set(cv2.CAP_PROP_POS_FRAMES, frameToStart)
count = 0
while video.isOpened():
    ret, frame = video.read()  # 捕获一帧图像
    if ret:
        out.write(frame)
        count += 1
    else:
        break
    if count % 30 == 0:
        print("have done", 30, "frames...")
    if cv2.waitKey(1) == 27 & 0xFF:
        break
video.release()
out.release()
cv2.destroyAllWindows()

你可能感兴趣的:(Python,深度学习)