""" Extract frames from the Motive Loop Closure Dataset raw video clips. Usage: # Extract all sequences at 2 fps (default) python3 extract_frames.py # Extract a specific sequence python3 extract_frames.py --sequence v102_20260909_loop01 # Custom frame rate and output root python3 extract_frames.py --fps 5 --frames-root ./frames_5fps Outputs frames to: data///frames/fps/000001.jpg ... Requirements: ffmpeg must be on PATH """ from __future__ import annotations import argparse, subprocess, sys from pathlib import Path SCRIPT_DIR = Path(__file__).parent VIDEOS_DIR = SCRIPT_DIR / "videos" DATA_DIR = SCRIPT_DIR / "data" CLIP_DURATION = 180 # seconds def extract(mp4: Path, out_dir: Path, fps: int): out_dir.mkdir(parents=True, exist_ok=True) existing = list(out_dir.glob("*.jpg")) if existing: print(f" {out_dir.name}/ already has {len(existing)} frames — skipping") return len(existing) cmd = [ "ffmpeg", "-y", "-i", str(mp4), "-vf", f"fps={fps}", "-q:v", "3", str(out_dir / "%06d.jpg"), ] result = subprocess.run(cmd, capture_output=True) if result.returncode != 0: print(f" ERROR: ffmpeg failed:\n{result.stderr.decode()[-300:]}") return 0 n = len(list(out_dir.glob("*.jpg"))) print(f" → {n} frames") return n def main(): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("--fps", type=int, default=2) ap.add_argument("--sequence", default=None, help="Extract only this sequence") ap.add_argument("--frames-root", default=None, help="Root for frame output (default: data///frames/fps/)") args = ap.parse_args() sequences = sorted(DATA_DIR.iterdir()) if DATA_DIR.exists() else [] if args.sequence: sequences = [DATA_DIR / args.sequence] total_frames = 0 for seq_dir in sequences: if not seq_dir.is_dir(): continue print(f"\n[{seq_dir.name}]") for role in ("origin", "revisit"): role_dir = seq_dir / role if not role_dir.exists(): continue # Find the mp4 in videos/ # filename pattern: v{vehicle}_{role}_{YYYYMMDD_HHMMSS}_front_facing.mp4 parts = seq_dir.name.split("_") # v102, 20260909, loop01 vehicle = parts[0][1:] # "102" date = parts[1] # "20260909" mp4s = sorted(VIDEOS_DIR.glob(f"v{vehicle}_{role}_*_front_facing.mp4")) # Match by date mp4s = [m for m in mp4s if date in m.name] if not mp4s: print(f" [{role}] no MP4 found in videos/ — download it first") continue mp4 = mp4s[0] if args.frames_root: out_dir = Path(args.frames_root) / seq_dir.name / role else: out_dir = role_dir / "frames" / f"{args.fps}fps" print(f" [{role}] {mp4.name} @ {args.fps} fps") total_frames += extract(mp4, out_dir, args.fps) print(f"\nDone. {total_frames:,} frames extracted.") if __name__ == "__main__": main()