File size: 4,531 Bytes
838951c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import tempfile
import os
import imageio
from PIL import Image
from typing import List, Optional
import shutil

def create_video_from_frames(frames: List[np.ndarray], fps: int = 24) -> str:
    """
    Create a video file from a list of frames
    
    Args:
        frames (List[np.ndarray]): List of video frames as numpy arrays
        fps (int): Frames per second for the output video
        
    Returns:
        str: Path to the generated video file
    """
    if not frames:
        raise ValueError("No frames provided")
    
    # Create temporary file
    temp_dir = tempfile.mkdtemp()
    video_path = os.path.join(temp_dir, "generated_video.mp4")
    
    # Ensure frames are in the right format
    processed_frames = []
    for frame in frames:
        if isinstance(frame, np.ndarray):
            # Convert numpy array to PIL Image
            if frame.dtype != np.uint8:
                frame = (frame * 255).astype(np.uint8)
            if len(frame.shape) == 3 and frame.shape[2] == 3:
                # RGB image
                pil_image = Image.fromarray(frame, mode='RGB')
            elif len(frame.shape) == 3 and frame.shape[2] == 4:
                # RGBA image
                pil_image = Image.fromarray(frame, mode='RGBA')
            else:
                # Grayscale
                pil_image = Image.fromarray(frame, mode='L')
        else:
            # Assume it's already a PIL Image
            pil_image = frame
        
        processed_frames.append(pil_image)
    
    # Save as video
    with imageio.get_writer(video_path, fps=fps) as writer:
        for frame in processed_frames:
            # Convert PIL to numpy array
            frame_array = np.array(frame)
            writer.append_data(frame_array)
    
    return video_path

def save_video_temp(frames: List, fps: int = 24) -> str:
    """
    Save video frames to a temporary file
    
    Args:
        frames (List): List of video frames
        fps (int): Frames per second
        
    Returns:
        str: Path to the saved video file
    """
    try:
        return create_video_from_frames(frames, fps)
    except Exception as e:
        # Fallback: save as GIF if video creation fails
        temp_dir = tempfile.mkdtemp()
        gif_path = os.path.join(temp_dir, "generated_video.gif")
        
        # Convert frames to PIL Images and save as GIF
        pil_frames = []
        for frame in frames:
            if isinstance(frame, np.ndarray):
                if frame.dtype != np.uint8:
                    frame = (frame * 255).astype(np.uint8)
                pil_frame = Image.fromarray(frame)
            else:
                pil_frame = frame
            pil_frames.append(pil_frame)
        
        if pil_frames:
            pil_frames[0].save(
                gif_path,
                save_all=True,
                append_images=pil_frames[1:],
                duration=1000 // fps,
                loop=0
            )
            return gif_path
        else:
            raise Exception("No valid frames to save")

def cleanup_temp_files():
    """Clean up temporary files"""
    temp_dir = tempfile.gettempdir()
    # Clean up files older than 1 hour
    current_time = time.time()
    for filename in os.listdir(temp_dir):
        if filename.startswith("generated_video"):
            file_path = os.path.join(temp_dir, filename)
            try:
                if os.path.getmtime(file_path) < current_time - 3600:
                    if os.path.isfile(file_path):
                        os.unlink(file_path)
                    elif os.path.isdir(file_path):
                        shutil.rmtree(file_path)
            except Exception:
                pass

def validate_prompt(prompt: str) -> bool:
    """
    Validate that the prompt is not empty and has reasonable length
    
    Args:
        prompt (str): Input prompt
        
    Returns:
        bool: True if prompt is valid
    """
    if not prompt or not prompt.strip():
        return False
    if len(prompt.strip()) < 3:
        return False
    if len(prompt.strip()) > 1000:
        return False
    return True

def format_status_message(message: str, success: bool = True) -> str:
    """
    Format status message with appropriate emoji
    
    Args:
        message (str): Status message
        success (bool): Whether the operation was successful
        
    Returns:
        str: Formatted status message
    """
    emoji = "βœ…" if success else "❌"
    return f"{emoji} {message}"