forked from aaf6aa/SCUNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVidToFrames.py
68 lines (41 loc) · 1.88 KB
/
VidToFrames.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
import cv2
import os
import time
from concurrent.futures import ThreadPoolExecutor
show_number = input("Enter the show number: ")
num_frames = int(input("Enter the number of frames to extract from each video: "))
start_time_minutes = int(input("Enter the starting time in minutes to begin capturing frames: "))
start_time_seconds = start_time_minutes * 60
input_folder_path = os.path.dirname(os.path.abspath(__file__))
output_folder_path = 'output'
if not os.path.exists(output_folder_path):
os.makedirs(output_folder_path)
video_filenames = [f for f in os.listdir(input_folder_path) if f.endswith('.mkv')]
def extractFrames(filename, show_number, output_folder_path, num_frames, start_time_seconds):
vidcap = cv2.VideoCapture(filename)
fps = vidcap.get(cv2.CAP_PROP_FPS)
start_frame = int(fps * start_time_seconds)
vidcap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
count = 0
while count < num_frames:
success, image = vidcap.read()
if not success:
break
cv2.imwrite(os.path.join(output_folder_path, f"show{show_number}_Frame{count + 1}.png"), image)
count += 1
vidcap.release()
start_time = time.perf_counter()
with ThreadPoolExecutor() as executor:
futures = []
for video_filename in video_filenames:
future = executor.submit(extractFrames,
os.path.join(input_folder_path, video_filename),
show_number,
output_folder_path,
num_frames,
start_time_seconds)
futures.append(future)
for future in futures:
future.result()
end_time = time.perf_counter()
print("Elapsed time:", end_time - start_time)