Files
Orchestrator/dhive/Projekt-KIQ-HP/scratch_convert.py

41 lines
1.3 KiB
Python

from PIL import Image, ImageSequence
import imageio
import numpy as np
import sys
def convert_webp_to_mp4(webp_path, mp4_path, fps=30):
try:
# Load the Animated WebP
img = Image.open(webp_path)
frames = []
for frame in ImageSequence.Iterator(img):
# Convert to RGB, as MP4 (H264) does not support RGBA seamlessly without special pixel formats
# and imageio handles RGB best
frame = frame.convert('RGB')
arr = np.array(frame)
# Ensure even dimensions for h264 encoder
h, w = arr.shape[:2]
h = h - (h % 2)
w = w - (w % 2)
frames.append(arr[:h, :w, :])
if not frames:
print("No frames found in the image.")
return
# Write to MP4 using imageio
writer = imageio.get_writer(mp4_path, fps=fps, macro_block_size=None)
for frame in frames:
writer.append_data(imageio.core.image_as_uint(frame))
writer.close()
print(f"Successfully converted {len(frames)} frames to {mp4_path}")
except Exception as e:
print(f"Error during conversion: {e}")
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage: python convert.py input.webp output.mp4")
else:
convert_webp_to_mp4(sys.argv[1], sys.argv[2])