Deforum_Soonr / dev /video_utils.py
AlekseyCalvin's picture
Rename video_utils.py to dev/video_utils.py
ab4e8a2 verified
import cv2
import os
import numpy as np
import zipfile
from PIL import Image
def frames_to_video(frames, output_path, fps=10):
if not frames: return None
# Ensure dimensions are compatible with most players (multiples of 2)
width, height = frames[0].size
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
for frame in frames:
# Convert PIL to NumPy, then RGB to BGR for OpenCV
frame_numpy = np.array(frame)
out.write(cv2.cvtColor(frame_numpy, cv2.COLOR_RGB2BGR))
out.release()
return output_path
def export_to_zip(frames, zip_name="frames.zip"):
with zipfile.ZipFile(zip_name, 'w') as zipf:
for i, frame in enumerate(frames):
frame_path = f"frame_{i:04d}.png"
frame.save(frame_path)
zipf.write(frame_path)
os.remove(frame_path)
return zip_name