-
Notifications
You must be signed in to change notification settings - Fork 6
/
video_to_frames.py
48 lines (34 loc) · 1.1 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
46
47
48
import cv2
import os
import shutil
"""
This script converts a video into individual frames and saves them to a specified directory.
Only a subset of frames is saved based on a defined interval to match the desired frame rate for further processing.
"""
# Path to the video file
video_path = 'video.mp4'
# Directory to save the frames
output_folder = 'output_dir'
if os.path.exists(output_folder):
shutil.rmtree(output_folder)
os.makedirs(output_folder, exist_ok=True)
# Load the video
cap = cv2.VideoCapture(video_path)
# Get the frame rate of the video
fps = cap.get(cv2.CAP_PROP_FPS)
frames_per_second = 10
interval = int(fps / frames_per_second)
frame_count = 0
saved_frame_count = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Save frame if it's in the interval
if frame_count % interval == 0:
frame_filename = os.path.join(output_folder, f'frame_{saved_frame_count:04d}.png')
cv2.imwrite(frame_filename, frame)
saved_frame_count += 1
frame_count += 1
cap.release()
print(f'Extracted {saved_frame_count} frames to {output_folder}')