提取视频的每一帧保存到指定文件夹

import cv2
import os
# 要提取视频的文件名,隐藏后缀
sourceFileName = '1-21040914325LK'
# 在这里把后缀接上
video_path = os.path.join(r"F:\image", sourceFileName+'.mp4')
# print(video_path)
times = 0
# 提取视频的频率,每1帧提取一个
frameFrequency = 1
# 输出图片到当前目录vedio文件夹下
outPutDirName = "F:\\image\\{}".format(sourceFileName)
# print(outPutDirName)
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 + '\\' + str(times)+'.jpg', image)
        print(outPutDirName + '\\' + str(times)+'.jpg')
print('图片提取结束')
camera.release()
posted @ 2022-02-08 15:58  长乐未央丫  阅读(158)  评论(0)    收藏  举报