利用cv2提取视频帧,可以逐帧提取,也可以跳跃读取。在读取过程中,需要对文件名进行规范,通过以下命令进行填充:
.zfill(num) # num指的是填充会的位数
import cv2
import os# 要提取视频的文件名,隐藏后缀
sourceFileName = 'Fog20200313000026'
# 在这里把后缀接上
video_path = os.path.join(sourceFileName + '.mp4')
times = 0
# 提取视频的频率,每375帧提取一个
frameFrequency = 375
# 输出图片到当前目录vedio文件夹下
outPutDirName = 'vedio/' + sourceFileName + '/'
if not os.path.exists(outPutDirName):# 如果文件目录不存在则创建目录os.makedirs(outPutDirName)
camera = cv2.VideoCapture(video_path)counter = 0while True:# 读取指定数量的帧if counter <= 1920:times += 1res, image = camera.read()if not res:print('not res , not image')breakif times % frameFrequency == 0:counter += 1cv2.imwrite(outPutDirName + str(counter).zfill(4) + '.jpg', image)print(outPutDirName + str(counter) + '.jpg')else:break
print('图片提取结束')
camera.release()