Spaces:
Sleeping
Sleeping
File size: 745 Bytes
bd63620 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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 |