File size: 6,546 Bytes
b0b0867
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import os
import tempfile
from typing import List, Union, Tuple, Optional
from modules.video_processor import is_video_file, process_video_upload, SUPPORTED_VIDEO_FORMATS

# Supported image formats
SUPPORTED_IMAGE_FORMATS = ['.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.webp', '.gif']

def get_media_type(file_path: str) -> str:
    """

    Determine if a file is an image, video, or unsupported.

    

    Args:

        file_path: Path to the file

        

    Returns:

        'image', 'video', or 'unsupported'

    """
    if not file_path:
        return 'unsupported'
    
    _, ext = os.path.splitext(file_path.lower())
    
    if ext in SUPPORTED_IMAGE_FORMATS:
        return 'image'
    elif ext in SUPPORTED_VIDEO_FORMATS:
        return 'video'
    else:
        return 'unsupported'

def is_supported_media(file_path: str) -> bool:
    """Check if the file is a supported image or video format."""
    return get_media_type(file_path) in ['image', 'video']

def create_gallery_item(file_path: str) -> Optional[Tuple[str, str]]:
    """

    Create a gallery-compatible item from a media file path.

    

    Args:

        file_path: Path to the media file

        

    Returns:

        Tuple suitable for gallery (file_path, filename) or None if unsupported

    """
    if not os.path.exists(file_path):
        return None
    
    if not is_supported_media(file_path):
        return None
    
    filename = os.path.basename(file_path)
    return (file_path, filename)

def process_single_media_upload(file_path: str, max_video_duration: int = 30, frame_interval: int = 1) -> List[str]:
    """

    Process a single media file upload (image or video).

    

    Args:

        file_path: Path to the uploaded file

        max_video_duration: Maximum duration to process for videos (seconds)

        frame_interval: Interval between frames for videos (seconds)

        

    Returns:

        List of file paths to be added to gallery (images or extracted frames)

    """
    if not file_path or not os.path.exists(file_path):
        return []
    
    media_type = get_media_type(file_path)
    
    if media_type == 'image':
        # For images, just return the original path
        return [file_path]
    elif media_type == 'video':
        # For videos, extract frames
        frame_paths, _ = process_video_upload(file_path, max_video_duration, frame_interval)
        return frame_paths
    else:
        # Unsupported format
        return []

def process_multiple_media_uploads(

    file_paths: List[str], 

    max_video_duration: int = 30, 

    frame_interval: int = 1

) -> List[str]:
    """

    Process multiple media file uploads.

    

    Args:

        file_paths: List of paths to uploaded files

        max_video_duration: Maximum duration to process for videos (seconds)

        frame_interval: Interval between frames for videos (seconds)

        

    Returns:

        List of file paths to be added to gallery (images and extracted frames)

    """
    all_paths = []
    
    for file_path in file_paths:
        processed_paths = process_single_media_upload(file_path, max_video_duration, frame_interval)
        all_paths.extend(processed_paths)
    
    return all_paths

def handle_single_media_upload(file_path: str, gallery: List, max_video_duration: int = 30, frame_interval: int = 1) -> Tuple[List, Optional[str]]:
    """

    Handle a single media file upload and update gallery.

    

    Args:

        file_path: Path to the uploaded file

        gallery: Current gallery list

        max_video_duration: Maximum duration to process for videos (seconds)

        frame_interval: Interval between frames for videos (seconds)

        

    Returns:

        Tuple of (updated_gallery, None) for Gradio compatibility

    """
    if gallery is None:
        gallery = []
    
    if not file_path:
        return gallery, None
    
    # Process the media file
    processed_paths = process_single_media_upload(file_path, max_video_duration, frame_interval)
    
    # Create gallery items and add to gallery
    for path in processed_paths:
        gallery_item = create_gallery_item(path)
        if gallery_item:
            gallery.append(gallery_item)
    
    return gallery, None

def handle_multiple_media_uploads(

    file_paths: List, 

    gallery: List, 

    max_video_duration: int = 30, 

    frame_interval: int = 1

) -> List:
    """

    Handle multiple media file uploads and update gallery.

    

    Args:

        file_paths: List of uploaded file paths

        gallery: Current gallery list

        max_video_duration: Maximum duration to process for videos (seconds)

        frame_interval: Interval between frames for videos (seconds)

        

    Returns:

        Updated gallery list

    """
    if gallery is None:
        gallery = []
    
    if not file_paths:
        return gallery
    
    # Process all media files
    processed_paths = process_multiple_media_uploads(file_paths, max_video_duration, frame_interval)
    
    # Create gallery items and add to gallery
    for path in processed_paths:
        gallery_item = create_gallery_item(path)
        if gallery_item:
            gallery.append(gallery_item)
    
    return gallery

def get_supported_formats() -> dict:
    """Get dictionary of supported file formats."""
    return {
        'images': SUPPORTED_IMAGE_FORMATS,
        'videos': SUPPORTED_VIDEO_FORMATS,
        'all': SUPPORTED_IMAGE_FORMATS + SUPPORTED_VIDEO_FORMATS
    }

def validate_media_files(file_paths: List[str]) -> Tuple[List[str], List[str]]:
    """

    Validate a list of media files.

    

    Args:

        file_paths: List of file paths to validate

        

    Returns:

        Tuple of (valid_files, invalid_files)

    """
    valid_files = []
    invalid_files = []
    
    for file_path in file_paths:
        if is_supported_media(file_path):
            valid_files.append(file_path)
        else:
            invalid_files.append(file_path)
    
    return valid_files, invalid_files

# Export functions
__all__ = [
    'get_media_type',
    'is_supported_media',
    'create_gallery_item',
    'process_single_media_upload',
    'process_multiple_media_uploads',
    'handle_single_media_upload',
    'handle_multiple_media_uploads',
    'get_supported_formats',
    'validate_media_files',
    'SUPPORTED_IMAGE_FORMATS'
]