#!/usr/bin/env python3 """Prepare one traceable training-benchmark sample for CPU-only JavisBench.""" from __future__ import annotations import argparse import csv import hashlib import json import subprocess from pathlib import Path from common import utc_now, write_json def sha256_file(path: Path) -> str: digest = hashlib.sha256() with path.open("rb") as stream: for block in iter(lambda: stream.read(4 * 1024 * 1024), b""): digest.update(block) return digest.hexdigest() def prepare(table: Path, index: int, output: Path) -> dict: if index < 0: raise ValueError("sample index must be nonnegative") table = table.resolve(strict=True) if output.exists() and any(output.iterdir()): raise FileExistsError(f"Output must be new or empty: {output}") data = json.loads(table.read_text(encoding="utf-8")) if data.get("columns") != ["index", "prompt", "local_path"]: raise ValueError(f"Unexpected benchmark table format: {table}") rows = [row for row in data["data"] if row[0] == index] if len(rows) != 1: raise ValueError(f"Sample index {index} not unique/present in {table}") _, prompt, source = rows[0] video = Path(source).resolve(strict=True) if video.name != f"sample_{index}.mp4": raise ValueError(f"Unexpected video path: {video}") if not prompt.strip(): raise ValueError("Empty prompt") # Keep the original source untouched; only derived WAV/metadata go into output. output.mkdir(parents=True, exist_ok=True) audio = (output / "sample_0000.wav").resolve() subprocess.run( ["ffmpeg", "-nostdin", "-v", "error", "-n", "-i", str(video), "-map", "0:a:0", "-ac", "1", "-ar", "16000", "-c:a", "pcm_s16le", str(audio)], check=True, ) csv_path = (output / "javisbench_input.csv").resolve() with csv_path.open("w", newline="", encoding="utf-8") as stream: writer = csv.DictWriter( stream, fieldnames=["video_id", "text", "path", "audio_path", "pred_video_path", "pred_audio_path"], ) writer.writeheader() writer.writerow({ "video_id": f"benchmark_sample_{index}", "text": prompt, "path": str(video), "audio_path": str(audio), "pred_video_path": str(video), "pred_audio_path": str(audio), }) manifest = { "created_at_utc": utc_now(), "identity": "local training benchmark, NOT HF published TurboT2AV; NOT 200-prompt test set", "source_table": str(table), "source_table_sha256": sha256_file(table), "source_video": str(video), "source_video_sha256": sha256_file(video), "source_sample_index": index, "prompt": prompt, "derived_audio": str(audio), "derived_audio_sha256": sha256_file(audio), "input_csv": str(csv_path), } write_json(output / "provenance.json", manifest) return manifest def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--table", required=True, type=Path) parser.add_argument("--index", type=int, default=0) parser.add_argument("--output", required=True, type=Path) args = parser.parse_args() print(json.dumps(prepare(args.table, args.index, args.output), indent=2)) if __name__ == "__main__": main()