用cv包的VideoCapture提取视频帧

import cv2
import os

#6s的视频生成15帧图片

def extract_frames_and_save_img(video_path):

    videos = os.listdir(video_path)
    for video_name in videos:
        print('视频名称:',video_name)
        file_name = video_name.split('.')[0]
        folder_name = file_name
        os.makedirs(folder_name, exist_ok=True)
        vc = cv2.VideoCapture(video_path + video_name)  # 读入视频文件
        print('视频文件路径:',video_path + video_name)
        c = 1
        if vc.isOpened():  # 判断是否正常打开
            rval, frame = vc.read()
        else:
            rval = False
            print('视频不能正常打开,请检查路径')

        timeF =10  # 视频帧计数间隔频率
        print('每隔2帧存储一张图片......')
        i=0
        while rval:  # 循环读取视频帧
            rval, frame = vc.read()

            pic_path = folder_name + '/'
            if (c % timeF == 0):  # 每隔timeF帧进行存储操作
                cv2.imwrite(pic_path + file_name + '_' + str(c) + '.jpg', frame)  # 存储为图像,保存名为 文件夹名_数字(第几个文件).jpg
                i+=1
                print(pic_path + file_name + '_' + str(c) + '.jpg')
            c = c + 1
            cv2.waitKey(1)
        print('图片存储路径:D:\\python_project\\',file_name)
        print('该视频生成%d张图片'%i)

        vc.release()

if __name__ == '__main__':
    video_path = 'D:/python_project/testVideo/icebear/'
    extract_frames_and_save_img(video_path)


运行结果如下:
在这里插入图片描述

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页