"""Convert the three generated candidates to 25fps; retain real forward motion.""" import json from pathlib import Path import av import numpy as np from PIL import Image, ImageDraw root = Path("/opt/hel714-material") assets = Path("/opt/hel714-sample/dist/assets") metrics = {} sheet = Image.new("RGB", (7 * 220, 3 * 245)) draw = ImageDraw.Draw(sheet) for row, (kind, name) in enumerate([("idle", "talk"), ("enter", "enter"), ("return", "return")]): source = next(root.glob(f"{kind}_*.webm")) frames = [f.to_image() for f in av.open(str(source)).decode(video=0)] out = av.open(str(assets / f"{name}.mp4"), "w") stream = out.add_stream("libx264", rate=25) stream.width = stream.height = 512 stream.pix_fmt = "yuv420p" stream.options = {"crf": "18", "preset": "fast"} count = round(len(frames) * 25 / 16) for i in range(count): frame = av.VideoFrame.from_image(frames[min(len(frames) - 1, int(i * 16 / 25))]) for packet in stream.encode(frame): out.mux(packet) for packet in stream.encode(): out.mux(packet) out.close() for column, index in enumerate(np.linspace(0, len(frames) - 1, 7).astype(int)): sheet.paste(frames[index].resize((220, 220)), (column * 220, row * 245)) draw.text((column * 220, row * 245 + 222), f"{kind} f{index}", fill="white") metrics[kind] = {"input_frames": len(frames), "output_frames": count, "seconds": count / 25} sheet.save(root / "candidate-contact.png") (root / "candidate-metrics.json").write_text(json.dumps(metrics, indent=2)) print(metrics)