AlekseyCalvin commited on
Commit
8a4be01
·
verified ·
1 Parent(s): 52c62dd

Create video_utils.py

Browse files
Files changed (1) hide show
  1. video_utils.py +25 -0
video_utils.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import os
3
+ import zipfile
4
+ from PIL import Image
5
+
6
+ def frames_to_video(frames, output_path, fps=10):
7
+ """Compiles a list of PIL images into an MP4 video."""
8
+ if not frames: return None
9
+ width, height = frames[0].size
10
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
11
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
12
+ for frame in frames:
13
+ out.write(cv2.cvtColor(np.array(frame), cv2.COLOR_RGB2BGR))
14
+ out.release()
15
+ return output_path
16
+
17
+ def export_to_zip(frames, zip_name="frames.zip"):
18
+ """Saves all frames to a temporary ZIP for batch download."""
19
+ with zipfile.ZipFile(zip_name, 'w') as zipf:
20
+ for i, frame in enumerate(frames):
21
+ frame_path = f"frame_{i:04d}.png"
22
+ frame.save(frame_path)
23
+ zipf.write(frame_path)
24
+ os.remove(frame_path)
25
+ return zip_name