File size: 1,588 Bytes
857bc90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import cv2
from tqdm import tqdm

def save_video_frames(input_dir, output_dir):

    os.makedirs(output_dir, exist_ok=True)
    
    dir_frame_idx = None
    for filename in tqdm(os.listdir(input_dir)):
        if filename.lower().endswith('.mp4'):
            video_path = os.path.join(input_dir, filename)
            
            base_name = os.path.splitext(filename)[0]
            
            cap = cv2.VideoCapture(video_path)
            if not cap.isOpened():
                print(f"Warning: Cannot open {video_path}")
                continue

            frame_idx = 0
            while True:
                ret, frame = cap.read()
                
                if not ret:
                    break
                
                output_filename = f"{base_name}_frame_{frame_idx}.png"
                output_path = os.path.join(output_dir, output_filename)
                
                if not cv2.imwrite(output_path, frame):
                    print(f"Warnning: Cannot write {output_path}")
                
                frame_idx += 1

            cap.release()
            
            if dir_frame_idx == None:
                dir_frame_idx = frame_idx
            else:
                if dir_frame_idx != frame_idx:
                    print(f"Warning: {video_path} has {frame_idx} frames, but {dir_frame_idx} frames in {input_dir}")
                dir_frame_idx = min(dir_frame_idx, frame_idx)
                
    return dir_frame_idx

if __name__ == "__main__":
    save_video_frames(
        input_dir="aaa",
        output_dir="bbb"
    )