AlekseyCalvin commited on
Commit
434512f
·
verified ·
1 Parent(s): 0581299

Create video_utils.py

Browse files
Files changed (1) hide show
  1. video_utils.py +29 -0
video_utils.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import os
3
+ import numpy as np
4
+ import zipfile
5
+ from PIL import Image
6
+
7
+ def frames_to_video(frames, output_path, fps=10):
8
+ if not frames: return None
9
+ # Ensure dimensions are compatible with most players (multiples of 2)
10
+ width, height = frames[0].size
11
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
12
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
13
+
14
+ for frame in frames:
15
+ # Convert PIL to NumPy, then RGB to BGR for OpenCV
16
+ frame_numpy = np.array(frame)
17
+ out.write(cv2.cvtColor(frame_numpy, cv2.COLOR_RGB2BGR))
18
+
19
+ out.release()
20
+ return output_path
21
+
22
+ def export_to_zip(frames, zip_name="frames.zip"):
23
+ with zipfile.ZipFile(zip_name, 'w') as zipf:
24
+ for i, frame in enumerate(frames):
25
+ frame_path = f"frame_{i:04d}.png"
26
+ frame.save(frame_path)
27
+ zipf.write(frame_path)
28
+ os.remove(frame_path)
29
+ return zip_name