通过python-opencv对视频进行逐帧提取,仅需修改两个参数。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import cv2 import os
video_path = 'video_path' timeF = 1
images_path = video_path.split('.', 1)[0] if not os.path.exists(images_path): os.mkdir(images_path)
vc = cv2.VideoCapture(video_path) c = 1 rat = 1 if vc.isOpened(): print('视频读取成功,正在逐帧截取...') while rat: rat, frame = vc.read() if(c%timeF == 0 and rat == True): cv2.imwrite(images_path + '/' + str(c) + '.jpg',frame) c = c + 1 vc.release() print('截取完成,图像保存在:%s' %images_path) else: print('视频读取失败,请检查文件地址')
|
截取失败尝试以下方法: