Python之OpenCV读取视频抽帧保存

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zong596568821xp/article/details/83746615
# -*- coding:utf8 -*-
import cv2
import os
import shutil

#视频文件名字
filename = '00156.mp4'

#保存图片的路径
savedpath = filename.split('.')[0] + '/'
isExists = os.path.exists(savedpath)
if not isExists:
    os.makedirs(savedpath)
    print('path of %s is build'%(savedpath))
else:
    shutil.rmtree(savedpath)
    os.makedirs(savedpath)
    print('path of %s already exist and rebuild'%(savedpath))
    

#视频帧率12
fps = 12
#保存图片的帧率间隔
count = 60

#开始读视频
videoCapture = cv2.VideoCapture(filename)
i = 0
j = 0

while True:
    success,frame = videoCapture.read()
    i+=1
    if(i % count ==0):
        #保存图片
        j += 1
        savedname = filename.split('.')[0] + '_' + str(j) + '_' + str(i)+'.jpg'
        cv2.imwrite(savedpath + savedname ,frame)
        print('image of %s is saved'%(savedname))
    if not success:
        print('video is all read')
        break
    

猜你喜欢

转载自blog.csdn.net/zong596568821xp/article/details/83746615