Spaces:
Sleeping
Sleeping
| from PIL import Image | |
| import numpy as np | |
| import imageio.v3 as iio | |
| from typing import List | |
| def frames_to_video(frames: List[Image.Image], fps: int, file_path: str) -> str: | |
| """Converts a list of PIL images to a video file (MP4).""" | |
| # Convert PIL images to numpy arrays (RGB) | |
| video_data = [np.array(frame.convert("RGB")) for frame in frames] | |
| # Save video using imageio | |
| # We rely on the external imageio-ffmpeg dependency for libx264 encoding | |
| iio.imwrite( | |
| file_path, | |
| video_data, | |
| fps=fps, | |
| codec='libx264', | |
| quality=8, # High quality setting (range 0-10, default 5) | |
| pixelformat='yuv420p' # Ensures compatibility with most players/browsers | |
| ) | |
| return file_path |