BlueSourceJY's picture
ShotStream priority 1-200 (part 3)
f0f0344 verified
Raw History Blame Contribute Delete
3.92 kB
# validates outputs, writes per-episode metadata, timing table, run_manifest, and builds release dir
import csv, datetime as dt, hashlib, json, os, re, shutil, subprocess, sys
W = os.path.expanduser("~/shotstream_run")
idx = json.load(open(f"{W}/inputs/prompts_index.json"))
DXB = dt.timezone(dt.timedelta(hours=4))
def parse_log(path, gpu):
t = {}; st = {}
for line in open(path, errors="replace", newline="\n"):
m = re.match(r"(\d{4}-\d\d-\d\dT[\d:.]+Z) .*?\[EPISODE\] (start|saved) (\S+)", line, re.S)
if not m: continue
ts = dt.datetime.strptime(m.group(1), "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=dt.timezone.utc)
if m.group(2) == "start": st[m.group(3)] = ts
else: t[m.group(3)] = {"seconds": round((ts - st[m.group(3)]).total_seconds(), 2), "gpu": gpu,
"start": st[m.group(3)], "end": ts, "log": os.path.basename(path)}
return t
timing = parse_log(f"{W}/logs/smoke.log", 0)
for k in range(4):
timing.update(parse_log(f"{W}/logs/shard_{k}.log", k)) # later reruns override
shard_of = {}
for k in range(4):
for r in csv.DictReader(open(f"{W}/inputs/shard_{k}.csv")): shard_of[r["episode_id"]] = k
def probe(p):
out = subprocess.run(["ffprobe", "-v", "error", "-count_frames", "-select_streams", "v:0", "-show_entries",
"stream=width,height,r_frame_rate,nb_read_frames", "-of", "json", p], capture_output=True, text=True)
s = json.loads(out.stdout)["streams"][0]
dec = subprocess.run(["ffmpeg", "-v", "error", "-xerror", "-i", p, "-f", "null", "-"], capture_output=True, text=True)
return s, dec.returncode == 0 and not dec.stderr.strip()
rel = f"{W}/release"; problems = []; rows = []
for eid, info in sorted(idx.items(), key=lambda x: x[1]["priority"]):
n = info["shot_count"]; k = shard_of[eid]; src = f"{W}/outputs/shard_{k}/{eid}.mp4"
if not os.path.exists(src): problems.append((eid, "missing")); continue
s, ok = probe(src)
if not (s["width"] == 832 and s["height"] == 480 and s["r_frame_rate"] == "16/1" and int(s["nb_read_frames"]) == 81 * n and ok):
problems.append((eid, s, ok)); continue
if eid not in timing: problems.append((eid, "no timing")); continue
d = f"{rel}/episodes/{eid}"; os.makedirs(d, exist_ok=True)
shutil.copy2(src, f"{d}/episode.mp4")
vsha = hashlib.sha256(open(src, "rb").read()).hexdigest()
t = timing[eid]
md = {"episode_id": eid, "priority": info["priority"], "shot_count": n,
"shot_frame_ranges": [[81 * i, 81 * (i + 1)] for i in range(n)],
"cut_frames": [81 * i for i in range(1, n)], "fps": 16, "resolution": {"width": 832, "height": 480},
"num_frames": 81 * n, "seed": 0, "global_caption": "",
"shots": [{"shot": p["shot"], "prompt": p["prompt"], "prompt_sha256": p["sha256"]} for p in info["shots"]],
"video_sha256": vsha, "generation_seconds": t["seconds"], "gpu_index": t["gpu"],
"generated_at_dubai": t["end"].astimezone(DXB).isoformat(), "source_log": t["log"]}
json.dump(md, open(f"{d}/metadata.json", "w"), ensure_ascii=False, indent=2)
rows.append([eid, info["priority"], n, k, t["gpu"], t["seconds"], round(t["seconds"] / n, 2),
t["start"].astimezone(DXB).isoformat(), t["end"].astimezone(DXB).isoformat(), vsha])
print("ok episodes:", len(rows), "problems:", len(problems))
for p in problems: print("PROBLEM", p)
os.makedirs(f"{rel}/runs", exist_ok=True)
with open(f"{rel}/runs/timing.csv", "w", newline="") as f:
w = csv.writer(f); w.writerow(["episode_id", "priority", "shot_count", "shard", "gpu", "seconds", "seconds_per_shot", "start_dubai", "end_dubai", "video_sha256"]); w.writerows(rows)
if rows:
tot_s = sum(r[5] for r in rows); tot_shots = sum(r[2] for r in rows)
print("sum gen seconds", round(tot_s, 1), "shots", tot_shots, "avg s/shot", round(tot_s / tot_shots, 3))
sys.exit(1 if problems else 0)