```python import os import numpy as np from PIL import Image import tempfile import imageio def resize_image(image, target_size=(576, 576)): """Resize image maintaining aspect ratio with padding""" if isinstance(image, np.ndarray): image = Image.fromarray(image) image = image.convert("RGB") original_size = image.size # Calculate aspect ratio target_ratio = target_size[0] / target_size[1] original_ratio = original_size[0] / original_size[1] if original_ratio > target_ratio: # Image is wider new_width = target_size[0] new_height = int(target_size[0] / original_ratio) else: # Image is taller new_height = target_size[1] new_width = int(target_size[1] * original_ratio) # Resize image = image.resize((new_width, new_height), Image.Resampling.LANCZOS) # Create new image with padding new_image = Image.new("RGB", target_size, (0, 0, 0)) paste_x = (target_size[0] - new_width) // 2 paste_y = (target_size[1] - new_height) // 2 new_image.paste(image, (paste_x, paste_y)) return new_image def create_video_from_frames(frames, fps=12, output_path=None): """Create video from list of frames""" if output_path is None: timestamp = int(time.time()) output_path = f"/tmp/video_{timestamp}.mp4" # Ensure frames are numpy arrays np_frames = [] for frame in frames: if isinstance(frame, Image.Image): np_frames.append(np.array(frame)) else: np_frames.append(frame) # Write video with imageio.get_writer(output_path, fps=fps, codec="libx264") as writer: for frame in np_frames: writer.append_data(frame) return output_path def extract_frames(video_path, fps=None): """Extract frames from video""" reader = imageio.get_reader(video_path) frames = [] for i, frame in enumerate(reader): if fps and i % int(reader.get_meta_data()['fps'] / fps) == 0: frames.append(frame) elif not fps: frames.append(frame) return frames def add_audio_to_video(video_path, audio_path, output_path=None): """Add audio to video (requires ffmpeg)""" if output_path is None: output_path = video_path.replace('.mp4', '_with_audio.mp4') import subprocess cmd = [ 'ffmpeg', '-i', video_path, '-i', audio_path, '-c:v', 'copy', '-c:a', 'aac', '-strict', 'experimental', '-shortest', output_path ] try: subprocess.run(cmd, check=True, capture_output=True) return output_path except Exception as e: print(f"Error adding audio: {e}") return video_path ``` File 5: Dockerfile (Optional - for advanced deployment) ```dockerfile FROM python:3.10-slim WORKDIR /app # Install system dependencies RUN apt-get update && apt-get install -y \ ffmpeg \ libgl1-mesa-glx \ libglib2.0-0 \ && rm -rf /var/lib/apt/lists/* # Copy requirements and install Python dependencies COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Copy application code COPY . . # Create cache directory RUN mkdir -p /root/.cache/huggingface # Expose port EXPOSE 7860 # Health check HEALTHCHECK CMD curl --fail http://localhost:7860 || exit 1 # Run the application CMD ["python", "app.py"] ``` ---