forked from JingyunLiang/VRT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_to_frames.py
45 lines (30 loc) · 1.09 KB
/
video_to_frames.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
import sys
import os
import cv2
# Run program with arguments: "Video_filepath" "Frame directory"
def main():
if len(sys.argv) < 3:
print("NEED AT LEAST TWO ADDITIONAL ARGUMENTS: video_filepath | frame_dir")
exit(-1)
video_filepath = sys.argv[1]
frame_dir = sys.argv[2]
video_filename = os.path.splitext(os.path.basename(video_filepath))[0]
os.makedirs(frame_dir)
print("\nInfo: ")
print("Video path = ", video_filepath)
print("Video name = ", video_filename)
print("Frame directory = ", frame_dir)
capture = cv2.VideoCapture(video_filepath)
frame_index = 0
while True:
ret, frame = capture.read()
if not ret:
print("Total frames:", frame_index+1)
break
output_frame_path = f"{frame_dir}/{frame_index:05d}.png"
print(output_frame_path)
if not cv2.imwrite(output_frame_path, frame):
print("ERROR: Failed to write frame", frame_index)
frame_index += 1
if __name__ == "__main__":
main()