File size: 997 Bytes
cb91e74 |
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 |
import os
import cv2
from pathlib import Path
def extract_frames(video_path, output_dir, fps=2):
os.makedirs(output_dir, exist_ok=True)
vidcap = cv2.VideoCapture(video_path)
video_name = Path(video_path).stem
count = 0
frame_id = 1
success, image = vidcap.read()
while success:
if int(vidcap.get(cv2.CAP_PROP_POS_FRAMES)) % int(vidcap.get(cv2.CAP_PROP_FPS) // fps) == 0:
frame_name = f"{video_name}_{frame_id:04d}.png"
cv2.imwrite(os.path.join(output_dir, frame_name), image)
frame_id += 1
success, image = vidcap.read()
count += 1
vidcap.release()
print(f"Extracted {frame_id-1} frames from {video_path}")
if __name__ == "__main__":
video_dir = "videos"
frames_dir = "frames"
for video_file in os.listdir(video_dir):
if video_file.lower().endswith(('.mp4', '.avi', '.mov')):
extract_frames(os.path.join(video_dir, video_file), frames_dir)
|