#!/usr/bin/env python """Compose truth | sim... side-by-side comparison video with labels. Usage: python compose_video.py --world work/B_03/world \ --sims work/B_03/out_p1 work/B_03/out_p2 \ --labels "mu=0.5 e=0.05" "mu=0.02 e=0.9" \ --out work/B_03/compare_B_03.mp4 """ import argparse import json from pathlib import Path import cv2 import numpy as np def read_frames(path): cap = cv2.VideoCapture(str(path)) frames = [] while True: ok, f = cap.read() if not ok: break frames.append(f) cap.release() return frames def main(): ap = argparse.ArgumentParser() ap.add_argument("--world", required=True) ap.add_argument("--sims", nargs="+", required=True) ap.add_argument("--labels", nargs="+", required=True) ap.add_argument("--truth-label", default="truth (reference)") ap.add_argument("--out", required=True) args = ap.parse_args() assert len(args.labels) == len(args.sims) spec = json.loads((Path(args.world) / "spec.json").read_text()) W, H = spec["video"]["width"], spec["video"]["height"] fps = spec["video"]["fps"] or 24.0 panels = [(args.truth_label, read_frames(Path(args.world) / "render.mp4"))] for sim, lab in zip(args.sims, args.labels): panels.append((lab, read_frames(Path(sim) / "sim.mp4"))) n = min(len(f) for _, f in panels) canvas_w, canvas_h = W * len(panels), H writer = cv2.VideoWriter(str(args.out), cv2.VideoWriter_fourcc(*"mp4v"), fps, (canvas_w, canvas_h)) font = cv2.FONT_HERSHEY_SIMPLEX for i in range(n): row = [] for k, (lab, frames) in enumerate(panels): f = frames[i] if f.shape[:2] != (H, W): f = cv2.resize(f, (W, H)) tag = f"{lab} [{i+1}/{n}]" cv2.rectangle(f, (0, 0), (W, 34), (0, 0, 0), -1) cv2.putText(f, tag, (10, 24), font, 0.62, (255, 255, 255), 2, cv2.LINE_AA) if k: cv2.line(f, (0, 0), (0, H), (255, 255, 255), 2) row.append(f) writer.write(np.hstack(row)) writer.release() print(f"wrote {args.out} ({n} frames, {canvas_w}x{canvas_h})") if __name__ == "__main__": main()