import cv2
import os
def videoToImage(sourceVideoPath, outputImagePath):
times = 0 # 初始化times
frameFrequency = 20 # 提取视频的频率,每25帧提取一个
id = 0 # 迭代控制保存的图片文件名,从355开始计数
camera = cv2.VideoCapture(sourceVideoPath)# 读取时视频文件流
while True:
times += 1
res, image = camera.read()
if not res:
print('not res , not image')
break
# 判断间隔多少帧
if times % frameFrequency == 0:
id += 1
cv2.imwrite(os.path.join(outputImagePath, str(id)) + '.jpg', image)
print('图片提取结束')
camera.release()
if __name__ == '__main__':
# 要提取视频的文件名
sourceVideoPath = r'E:\1\1\1.mp4'
# 输出图片到指定目录文件夹下
outputImagePath = r'E:\1\1'
videoToImage(sourceVideoPath, outputImagePath)