BlueSourceJY's picture
ShotStream priority 1-200 (part 3)
f0f0344 verified
Raw History Blame Contribute Delete
2.72 kB
import csv, hashlib, json, os, sys
root, out = sys.argv[1], sys.argv[2]
jdir = os.path.join(out, "json"); os.makedirs(jdir, exist_ok=True)
ids = [l.strip() for l in open(os.path.join(root, "priority_200.ids.txt")) if l.strip()]
assert len(ids) == 200
rows, prompts_index = [], {}
for pr, eid in enumerate(ids, 1):
ed = os.path.join(root, "episodes", eid)
sm = json.load(open(os.path.join(ed, "shot_mapping.json")))
ss = json.load(open(os.path.join(ed, "story_script.json")))
assert sm["episode_id"] == eid and sm.get("generation_priority", pr) == pr, (eid, pr)
shots = sm["shots"]; n = len(shots)
assert [s["shot_id"] for s in shots] == [f"Shot{i}" for i in range(1, n + 1)], eid
d = {"global_caption": ""}; plist = []
for i, s in enumerate(shots, 1):
sc = [x for x in ss["scenes"] if x["scene_num"] == s["storymem_scene_num"]]
assert len(sc) == 1
p = sc[0]["video_prompts"][s["storymem_shot_num"] - 1]
h = hashlib.sha256(p.encode("utf-8")).hexdigest()
assert h == s["prompt_sha256"], (eid, i)
d[f"shot{i}"] = p; plist.append({"shot": i, "prompt": p, "sha256": h})
jp = os.path.join(jdir, f"{eid}.json")
with open(jp, "w", encoding="utf-8") as f: json.dump(d, f, ensure_ascii=False, indent=1)
# re-read check: exact round trip
back = json.load(open(jp, encoding="utf-8"))
assert all(back[f"shot{i}"] == plist[i-1]["prompt"] for i in range(1, n + 1))
fr = [[81 * k, 81 * (k + 1)] for k in range(n)]
rows.append({"shot_num_from_caption": n, "json_path": os.path.abspath(jp),
"frame_number": json.dumps(fr), "episode_id": eid, "priority": pr})
prompts_index[eid] = {"priority": pr, "shot_count": n, "shots": plist}
json.dump(prompts_index, open(os.path.join(out, "prompts_index.json"), "w"), ensure_ascii=False, indent=1)
cols = ["shot_num_from_caption", "json_path", "frame_number", "episode_id", "priority"]
def wcsv(path, rs):
with open(path, "w", newline="") as f:
w = csv.DictWriter(f, fieldnames=cols); w.writeheader(); w.writerows(rs)
wcsv(os.path.join(out, "all_200.csv"), rows)
shards = [[] for _ in range(4)]; tot = [0] * 4
for r in rows: # priority order, assign to shard with fewest shots (ties -> lowest index)
k = min(range(4), key=lambda j: (tot[j], j)); shards[k].append(r); tot[k] += r["shot_num_from_caption"]
for k in range(4): wcsv(os.path.join(out, f"shard_{k}.csv"), shards[k])
print("shard shots:", tot, "episodes:", [len(s) for s in shards])
smoke = [r for r in rows if r["episode_id"] in ("74Od5-Fmf60_s00006_n4_clean_v2", "hERyYjy3O3U_s00003_n8_clean_v3_alt8")]
assert [r["priority"] for r in smoke] == [1, 7]
wcsv(os.path.join(out, "smoke.csv"), smoke)