Python视频抽帧成图片

可以在ubuntu使用,生成的图像名称是6位按顺序的数字

# encoding:utf-8
'''
读取文件列表,将视频文件单独拿出来,隔帧抽图并保存
'''
import cv2
import os
import glob
mp4s = glob.glob('*.mp4')
for one_mp4 in mp4s:
    print(one_mp4)
    # 要提取视频的文件名,隐藏后缀
    sourceFileName = one_mp4
    # 在这里把后缀接上
    # video_path = os.path.join("", "", sourceFileName + '.mp4')
    video_path = os.path.join("", "", sourceFileName)
    times = 0
    namenumber = 0
    # 提取视频的频率,每1帧提取一个
    frameFrequency = 1
    # 输出图片到当前目录vedio文件夹下
    outPutDirName = 'VedioPicture/' + sourceFileName + '/'
    if not os.path.exists(outPutDirName):
        # 如果文件目录不存在则创建目录
        os.makedirs(outPutDirName)
    camera = cv2.VideoCapture(video_path)
    while True:
        times += 1
        namenumber += 1
        res, image = camera.read()
        if not res:
            print('not res , not image')
            break
        if times % frameFrequency == 0:
            if (namenumber < 10):
                namenumber2str = '00000' + str(namenumber)
            elif (namenumber < 100):
                namenumber2str = '0000' + str(namenumber)
            elif (namenumber < 1000):
                namenumber2str = '000' + str(namenumber)
            elif (namenumber < 10000):
                namenumber2str = '00' + str(namenumber)
            elif (namenumber < 100000):
                namenumber2str = '0' + str(namenumber)
            else:
                namenumber2str = str(namenumber)
            cv2.imwrite(outPutDirName + namenumber2str + '.jpg', image)  # sourceFileName + '_' +
            print outPutDirName + namenumber2str + '.jpg'
    print one_mp4 + '图片提取结束'
    camera.release()

posted on 2020-05-10 15:58  丁错儿  阅读(38)  评论(0编辑  收藏  举报

导航