File size: 2,204 Bytes
2c68f56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'''
Date: 2024-06-01 14:09:13
LastEditors: DreamTale
LastEditTime: 2024-06-03 12:12:13
FilePath: /DataCrawler/mt_download.py
'''
import os
import argparse
import subprocess
from concurrent.futures import ThreadPoolExecutor

# global args


import os
import argparse
import subprocess
from concurrent.futures import ThreadPoolExecutor



import os
import cv2

def extract_frames_from_videos(input_dir, out_dir, frame_interval=1):
    if not os.path.isdir(input_dir):
        print(f"Invalid input directory: {input_dir}")
        return

    os.makedirs(out_dir, exist_ok=True)
    video_files = [f for f in os.listdir(input_dir) if f.lower().endswith('.mp4')]

    if not video_files:
        print(f"No .mp4 files found in {input_dir}")
        return

    for video_file in video_files:
        video_path = os.path.join(input_dir, video_file)
        v_name = os.path.splitext(video_file)[0]

        dir_path = os.path.join(input_dir, v_name)

        if os.path.exists(dir_path):
            continue
        os.makedirs(dir_path, exist_ok=True)


        cap = cv2.VideoCapture(video_path)
        if not cap.isOpened():
            print(f"Failed to open video: {video_path}")
            continue

        total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        if total_frames == 0:
            print(f"No frames found in video: {video_file}")
            cap.release()
            continue

        frame_idx = 0
        saved_idx = 0
        while True:
            ret, frame = cap.read()
            if not ret:
                break

            if frame_idx % frame_interval == 0:
                save_name = f"frame_{frame_idx:05d}.jpg" # {v_name}_
                save_path = os.path.join(dir_path, save_name)
                try:
                    cv2.imwrite(save_path, frame)
                    saved_idx += 1
                except Exception as e:
                    print(f"Failed to save frame {frame_idx} of {video_file}: {e}")

            frame_idx += 1

        cap.release()
        print(f"Extracted {saved_idx} frames from {video_file}.")

# 示例调用
extract_frames_from_videos(
    'data_input/driven/target_images/',
    'data_input/driven/target_images/'
)