#!/usr/bin/env python3 # make-movie.py # Convert a stacked .dat (header + frames) to an MP4 in the same directory. # - Default FPS = 1 # - Strict 0->blue, 1->red colormap # - Borders: top/bottom = blue, left/right = red # - Nearest-neighbor upscaling so min dimension >= 480px (even W/H for yuv420p) import argparse import os import sys import struct import numpy as np try: from matplotlib.colors import Normalize, LinearSegmentedColormap except Exception: print("Error: matplotlib is required (pip install matplotlib).", file=sys.stderr) sys.exit(1) try: import av # PyAV except Exception: print("Error: PyAV is required (pip install av).", file=sys.stderr) sys.exit(1) def parse_args(): p = argparse.ArgumentParser( description="Convert a stacked .dat (header + frames) to MP4. " "Defaults to 1 FPS, draws colored borders, and upscales to >=480p." ) p.add_argument("stack_path", nargs="?", help="Path to stacked .dat (e.g., ./data/all.100x100x500.dat)") p.add_argument("--fps", type=int, default=1, help="Frames per second (default: 1)") p.add_argument("--vmin", type=float, default=0.0, help="Min value mapped to blue (default: 0.0)") p.add_argument("--vmax", type=float, default=1.0, help="Max value mapped to red (default: 1.0)") p.add_argument("--out", dest="out_mp4", help="Output MP4 path (must end with .mp4). Default: .mp4") a = p.parse_args() if not a.stack_path: p.print_help(); sys.exit(1) in_path = os.path.abspath(os.path.expanduser(a.stack_path)) if not os.path.isfile(in_path): print(f"Error: not found: {in_path}", file=sys.stderr); sys.exit(1) if a.out_mp4: out_path = os.path.abspath(os.path.expanduser(a.out_mp4)) if not out_path.lower().endswith(".mp4"): print("Error: --out must end with .mp4", file=sys.stderr); sys.exit(1) else: base, _ = os.path.splitext(in_path) out_path = base + ".mp4" return in_path, out_path, a.fps, a.vmin, a.vmax def read_header_and_shape(in_path): with open(in_path, "rb") as f: hdr = f.read(8) if len(hdr) != 8: raise RuntimeError("Header too small (need 8 bytes: int32 rows, int32 cols)") rows, cols = struct.unpack("blue, 1->red map - Borders: top/bottom blue, left/right red - Nearest-neighbor upscale by integer 'scale' - Ensure even dimensions (yuv420p requirement) """ mmap = np.memmap(in_path, dtype=" min dimension >= 480px.") def frames_iter_factory(): return frame_generator(in_path, rows, cols, frames, vmin, vmax, scale) try: write_mp4_pyav(out_path, frames_iter_factory, fps) except Exception as e: print(f"Error writing MP4: {e}", file=sys.stderr); sys.exit(1) print(f"✅ Saved MP4: {out_path}") print(f" FPS: {fps}, Frames: {frames}, Source: {rows}x{cols}, Scale: x{scale}") if __name__ == "__main__": main()