1+ import cv2
2+ import os
3+ from pathlib import Path
4+ import pandas as pd
5+ import numpy as np
6+ from tqdm import tqdm
7+
8+ def extract_frames_basic (video_path , output_dir , frame_interval = 30 ):
9+ """
10+ Extract frames at regular intervals
11+
12+ Args:
13+ video_path: Path to input video
14+ output_dir: Directory to save frames
15+ frame_interval: Extract every Nth frame (30 = ~1 frame per second at 30fps)
16+ """
17+ # Create output directory
18+ Path (output_dir ).mkdir (parents = True , exist_ok = True )
19+
20+ # Open video
21+ cap = cv2 .VideoCapture (video_path )
22+
23+ # Get video properties
24+ total_frames = int (cap .get (cv2 .CAP_PROP_FRAME_COUNT ))
25+ fps = cap .get (cv2 .CAP_PROP_FPS )
26+
27+ print (f"Video: { video_path } " )
28+ print (f"Total frames: { total_frames } , FPS: { fps } " )
29+
30+ frame_count = 0
31+ saved_count = 0
32+
33+ with tqdm (total = total_frames // frame_interval , desc = "Extracting frames" ) as pbar :
34+ while True :
35+ ret , frame = cap .read ()
36+ if not ret :
37+ break
38+
39+ # Save frame at specified interval
40+ if frame_count % frame_interval == 0 :
41+ frame_filename = f"frame_{ saved_count :06d} .jpg"
42+ frame_path = os .path .join (output_dir , frame_filename )
43+ cv2 .imwrite (frame_path , frame )
44+ saved_count += 1
45+ pbar .update (1 )
46+
47+ frame_count += 1
48+
49+ cap .release ()
50+ print (f"Extracted { saved_count } frames to { output_dir } " )
51+ return saved_count
52+
53+ # Fix 2: Actually call the function at the end of your file
54+ if __name__ == "__main__" :
55+ # Set your output directory
56+ output_dir = "extracted_frames/example"
57+
58+ # Call the function
59+ frame_count = extract_frames_basic (
60+ # input path to video you want to extract frames from
61+ video_path = 'insert path here' ,
62+ output_dir = output_dir ,
63+ frame_interval = 30 # Extract every 30th frame
64+ )
65+
66+ print (f"Done! Extracted { frame_count } frames." )
0 commit comments