# -*- coding: utf-8 -*-
import cv2
import numpy as np
from matplotlib import pyplot as plt
import os
from PIL import Image
print(os.getcwd())
#파이썬 경로를 비디오 경로와 동일하게 변경
os.chdir('D:/my/record_3')
print(os.getcwd())
videopath = 'D:/my/record_3/'
savepath = 'D:/my/video frame/rec3/'
for i in range(9,901):
vname = 'v_3_'+str(i)
#print(vname)
cap = cv2.VideoCapture(videopath+vname+'.mp4')
fps = cap.get(cv2.CAP_PROP_FPS)
length = cap.get(cv2.CAP_PROP_FRAME_COUNT)
duration = length / fps
print(vname,', ',duration)
second = 0 # 초
num = 0 # 캡쳐 횟수 카운터
increase_width = 3 # 몇 초마다 캡쳐를 하고 싶은지 (증가폭)
cap.set(cv2.CAP_PROP_POS_MSEC, second * 1000) # openCV가 마이크로 세컨드 (ms)를 쓰기 때문에 1초는 1000을 입력해야함
success, image = cap.read()
while success and second <= duration:
num += 1
second += increase_width # 증가폭
#print("이번 캡쳐할 시간대는", second, "초")
cap.set(cv2.CAP_PROP_POS_MSEC, second * 1000)
success, image = cap.read()
#second가 높으면저장안됨
if second >= duration:
break
cv2.imwrite(savepath+"f_"+vname+"_%d.jpg" % num, image)
print('ok ',vname)
#plt.imshow(image)
#plt.show()
반응형