-
Notifications
You must be signed in to change notification settings - Fork 3
/
frameExtractor.py
31 lines (22 loc) · 1012 Bytes
/
frameExtractor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import cv2
import os
def frameExtractor(PATH, VIDEO_PATH, selectedFrames=None):
vid= cv2.VideoCapture(VIDEO_PATH)
if not os.path.exists(PATH):
os.makedirs(PATH)
if selectedFrames==None:
selectedFrames= [i for i in range(0, int(vid.get(cv2.CAP_PROP_FRAME_COUNT)), int(vid.get(cv2.CAP_PROP_FPS)))]
for i, framePos in enumerate(selectedFrames):
vid.set(cv2.CAP_PROP_POS_FRAMES, framePos)
success, frame= vid.read()
if not success:
print('Video END or Video File corrupted.')
break
print('Selected Frame: {} @ {} seconds'.format(i+1, vid.get(cv2.CAP_PROP_POS_MSEC)/1000))
cv2.imwrite(os.path.join(PATH, 'Frame{}.jpg'.format(i+1)), frame)
vid.release()
if __name__ == '__main__':
print('\nFrames at interval of 1 second will be extracted...\n')
path= os.getcwd()
videoPath= input('Enter absolute path of video file with / as separator\n')
frameExtractor(path, videoPath, None)