python cv2視頻抽幀保存

 

 

import cv2

import os

import shutil


#input video
filename = '20190327.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))

    

 


fps = 14.91


# the gap of frame
count = 10

 



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

    

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章