python-----opencv截取按帧截取视频
最近有需求把一个视频从指定帧截取一部分,demo代码如下:
import cv2 video_path = r'F:\temp\temp_0806\1\video\test.dat' videoCapture = cv2.VideoCapture(video_path) fps = 16 # 保存视频的帧率 size = (1280, 720) # 保存视频的大小 videoWriter = cv2.VideoWriter('video4.avi', cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'), fps, size) i = 0 while True: success, frame = videoCapture.read() if success: i += 1 print('i = ', i) if (i >= 1 and i <= 960): videoWriter.write(frame) else: print('end') break