包含模块:cv2,os
# !/usr/bin/env python
# -*-coding:utf-8-*-
import cv2
import os
# 要提取的视频路径
video_path = 'path'
times = 0
name = 1 # 第一张图片的编号
# 提取视频的频率,每x帧提取一个
frameFrequency = 30
# 输出图片到当前目录vedio文件夹下
outPutDirName='D:/Image'+ '/'
if not os.path.exists(outPutDirName):
# 如果文件目录不存在则创建目录
os.makedirs(outPutDirName)
camera = cv2.VideoCapture(video_path)
while True:
times+=1
res, image = camera.read()
if not res:
print('not res , not image')
break
if times%frameFrequency == 0:
cv2.imwrite(outPutDirName + 'img_'+ str(name)+ '.jpg', image)
print(outPutDirName + 'img_' + str(name)+'.jpg')
name += 1
print('图片提取结束')
camera.release()