-
Notifications
You must be signed in to change notification settings - Fork 1
/
frame_extractor.py
91 lines (69 loc) · 3.53 KB
/
frame_extractor.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# extracts individual frames from a video file to folder
import cv2
import datetime as dt
from pathlib import Path as path
def get_frame(frame_output_path, vidCap, time_counter, count):
vidCap.set(cv2.CAP_PROP_POS_MSEC, time_counter * 1000)
has_frames, image = vidCap.read()
if has_frames:
o = str(dt.timedelta(seconds=time_counter))
cv2.imwrite(frame_output_path + "frame_" + str(count) + "_@_time_" + o.replace(":", "'") + ".png", image)
return has_frames
def extractor(video_filepath, frame_output_path, start_time = 0, fps = 2, frame_limit = -1):
frame_rate = 1 / fps
count = 1
time_counter = start_time
video_filepath = str(path(video_filepath))
frame_output_path = str(path(frame_output_path)) + '/'
if int(frame_limit) > 0:
enable_frame_limit = True
else:
enable_frame_limit = False
vidCap = cv2.VideoCapture(video_filepath)
# I have no idea what this does but it works
# thank you medium.com for letting me steal your code
# def get_frame()...
success = get_frame(frame_output_path, vidCap, time_counter, count)
print("Operation started at " + str(dt.datetime.now().time()))
while success:
if enable_frame_limit == True:
# if count is greater than/equal to $frame_limit
if count >= int(frame_limit):
exit_intent = input("Final frame {0} exported at {1}\npress ENTER to exit or ANY KEY and ENTER to go again...".format(str(count), str(dt.datetime.now().time())))
# if any key was pressed to restart
if exit_intent != "":
extractor() # BROKEN RN THIS WONT WORK LMAO
else:
exit()
# normal operation
elif count < int(frame_limit):
#
count += 1
# go to next time of frame
time_counter += frame_rate
# round time to hundreths place, no weird decimals here
time_counter = round(time_counter, 2)
success = get_frame(frame_output_path, vidCap, time_counter, count)
# normal operation when no frame limit
elif enable_frame_limit == False:
count += 1
# go to next time of frame
time_counter += frame_rate
# round time to hundreths place, no weird decimals here
time_counter = round(time_counter, 2)
success = get_frame(frame_output_path, vidCap, time_counter, count)
exit_intent = input("Final frame {0} exported at {1}\npress ENTER to exit or ANY KEY and ENTER to go again...".format(str(count), str(dt.datetime.now().time())))
# if any key was pressed to restart
if exit_intent != "":
extractor() # BROKEN RN THIS WONT WORK LMAO
else:
exit()
target_video_path = input('Target video path : ')
outpur_frames_path = input('Output frames path : ')
custom_frame_rate = input('Input custom frames/second (empty for 2) : ')
custom_frame_rate = custom_frame_rate if custom_frame_rate != '' else 2
custom_start_time = input('Input custom start time (empty for 0) : ')
custom_start_time = custom_start_time if custom_start_time != '' else 0
custom_frame_limit = input('Input custom frame limit (empty for none): ')
custom_frame_limit = custom_frame_limit if custom_frame_limit != '' else -1
extractor(target_video_path, outpur_frames_path, float(custom_start_time), float(custom_frame_rate), int(custom_frame_limit))