mmprompt/easy-media-track-gate/build_track_plan.py

312 lines
15 KiB
Python
Executable File

#!/usr/bin/env python3
"""Merge the approved outline + segment fragments into the prompt_override paste.
py -3 build_track_plan.py outline.json segment_01.txt segment_02.txt [segment_03.txt ...]
py -3 build_track_plan.py outline.json segment_01.json ... --plan plan.json
py -3 build_track_plan.py --check plan.json [--outline outline.json]
The deliverable is ONE paste file, plain text by default — everything else is opt-in:
1. `prompt_override.txt` (default output, zero-media stories) — plain text for the
`easy multiTrackEditor` node's `prompt_override` input (format MiniMax): the scene
prompts joined with '|' on one line, plus [start-end(,type)] markers when the rows
have unequal durations or a scene is not the text default (flf -> t2v). The editor
splits a non-JSON prompt_override on '|' — one clip per scene, no JSON needed.
<Picture N>/<Audio N>/<Video N> tokens are NOT parsed in this path.
2. `prompt_override.json` — written INSTEAD of the txt when the story has media (the
minimal {"type": "minimax_prompt_override", "prompts": [...]} payload with media
tokens; parser requires ONLY type+prompts, duration in SECONDS, other keys only when
non-default), or alongside it with --json when a zero-media story needs per-segment
continuity modes or track locks the text form cannot carry.
3. `--plan plan.json` — the optional patch plan for the node pack's
`patch_workflow.py apply` ({"editor": {..., "track_data": {...}}, "project": {...}}),
plus `track_data.json` (pretty, for inspection) next to it. Written only when asked.
Segment fragments are plain text (segment_NN.txt: the scene prompt verbatim + a
'# handoff:' line); the approved outline row supplies id, frames, modes and images.
JSON fragments are still accepted for the rare variant-b / custom-system-prompt cases.
The merge is the continuity check: it re-validates every fragment against the outline
(ids, frames, modes, verbatim media paths, context-prompt rules, handoff chain) before
anything is written. Never edit fragment prose here and never patch the paste by hand —
fix the fragment, re-validate, re-merge. Segment/track/image ids are deterministic
UUIDv5 values derived from the project title, so re-runs keep ids stable.
Standard library only.
"""
from __future__ import annotations
import argparse
import copy
import json
import sys
import uuid
from pathlib import Path
from typing import Any
sys.path.insert(0, str(Path(__file__).resolve().parent))
import verify_track_rules as vtr # noqa: E402
def stable_id(*parts: str) -> str:
seed = "easy-media-track-gate/" + "/".join(parts)
return str(uuid.uuid5(uuid.NAMESPACE_URL, seed))
def _finalize_image(img: dict[str, Any], inv_entry: dict[str, Any], seg_id: str, pos: int,
shared_default: bool) -> dict[str, Any]:
out = {
"id": img.get("id") or stable_id(seg_id, f"image-{pos}"),
"source_type": img.get("source_type", inv_entry["source_type"]),
"file_path": inv_entry["path"],
"file_name": img.get("file_name") or inv_entry.get("file_name")
or inv_entry["path"].rsplit("/", 1)[-1],
"shared_reference": img.get("shared_reference", shared_default),
}
if out["source_type"] == "url":
out["url"] = img.get("url") or inv_entry["path"]
out.pop("file_path", None)
out.pop("file_name", None)
return out
def build(outline: dict[str, Any], fragments: list[dict[str, Any]]) -> dict[str, Any]:
rows = outline["rows"]
if len(fragments) != len(rows):
raise vtr.Fail(f"expected {len(rows)} fragments (one per approved outline row), got {len(fragments)}")
prev: dict[str, Any] | None = None
for i, frag in enumerate(fragments):
vtr.validate_fragment(frag, i + 1, len(rows), prev, outline)
prev = frag
task_segments: list[dict[str, Any]] = []
for i, (frag, row) in enumerate(zip(fragments, rows)):
seg_id = stable_id(outline["title"], "segment", row["id"])
content = copy.deepcopy(frag["content"])
content.setdefault("media_type", "none")
content["media_type"] = "none"
content.setdefault("user_prompt_variant", "a")
if content.get("user_prompt_variant") == "a":
content.setdefault("user_prompt_b", "")
content.setdefault("system_prompt", "")
content.setdefault("muted", False)
content.setdefault("volume_db", 0)
if "ref_image_size" not in content:
content["ref_image_size"] = "match"
row_imgs = row.get("images", [])
final_imgs = []
for pos, (img, row_img) in enumerate(zip(content.get("images", []), row_imgs)):
inv_entry = outline["inventory"][row_img["key"]]
final_imgs.append(_finalize_image(img, inv_entry, seg_id, pos,
bool(row_img.get("shared_reference", False))))
content["images"] = final_imgs
task_segments.append({
"id": seg_id,
"start_frame": frag["start_frame"],
"end_frame": frag["end_frame"],
"color": "var(--multitrack-task-bg)",
"content": content,
})
total = max((s["end_frame"] for s in task_segments), default=0)
tracks: list[dict[str, Any]] = [{
"id": stable_id(outline["title"], "track", "tasks"),
"name": "Tasks",
"type": "task",
"color": "var(--multitrack-task-bg)",
"muted": False,
"solo": False,
"volume_db": 0,
"locked": False,
"segments": task_segments,
}]
roles = outline["_roles"]
for role, track_type in (("master_audio", "audio"), ("reference_video", "video")):
spec = roles.get(role)
if not spec:
continue
inv = outline["inventory"][spec["key"]]
media_content: dict[str, Any] = {
"media_type": track_type,
"source_type": inv["source_type"],
}
if inv["source_type"] == "url":
media_content["url"] = inv["path"]
else:
media_content["file_path"] = inv["path"]
media_content["file_name"] = inv.get("file_name") or inv["path"].rsplit("/", 1)[-1]
tracks.append({
"id": stable_id(outline["title"], "track", role),
"name": spec["name"],
"type": track_type,
"color": "var(--primary)",
"muted": False,
"solo": False,
"volume_db": 0,
"locked": False,
"audio_locked": True,
"segments": [{
"id": stable_id(outline["title"], "media", role),
"start_frame": 0,
"end_frame": max(total, 1),
"color": "var(--primary)",
"content": media_content,
}],
})
track_data: dict[str, Any] = {
"muted": False,
"volume_db": 0,
"task_markers": [],
"task_overview": False,
"tracks": tracks,
"total_length": total,
"frame_rate": outline["defaults"]["frame_rate"],
}
plan: dict[str, Any] = {
"editor": {
"resolution": copy.deepcopy(outline["editor"]["resolution"]),
"track_data": track_data,
"format": outline["editor"]["format"],
},
"project": copy.deepcopy(outline["project"]),
}
project = plan["project"]
vtr.validate_plan(plan, outline=outline)
return plan
def _summary(plan: dict[str, Any]) -> str:
data = plan["editor"]["track_data"]
task_count = sum(len(t.get("segments", [])) for t in data["tracks"] if t.get("type") == "task")
locked = [f"{t['type']}:{t.get('name', t['id'])}" for t in data["tracks"]
if t.get("audio_locked") is True]
fps = data["frame_rate"]
return (f"{task_count} task segments, total_length {data['total_length']} frames "
f"= {data['total_length'] / fps:.2f}s at {fps} fps; "
f"tracks: {', '.join(t['type'] + ' ' + t.get('name', '') for t in data['tracks'])}; "
f"audio_locked: {', '.join(locked) if locked else 'none'}")
def _write_json(path: Path, value: dict[str, Any]) -> None:
path.write_text(json.dumps(value, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
def _main(argv: list[str]) -> int:
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument("inputs", nargs="*",
help="outline.json then segment_01.(txt|json) ... (merge) or plan.json (--check)")
parser.add_argument("-o", "--output", default=None,
help="the paste file (default: prompt_override.txt for zero-media "
"stories, prompt_override.json when the story has media)")
parser.add_argument("--plan", default=None, metavar="PLAN_JSON",
help="also write the patch plan (plan.json) for the node pack's "
"patch_workflow.py apply, plus track_data.json next to it")
parser.add_argument("--json", action="store_true",
help="also write prompt_override.json (the payload for continuity "
"modes / track locks the plain text cannot carry)")
parser.add_argument("--check", action="store_true", help="re-validate an existing plan.json")
parser.add_argument("--outline", help="outline to re-check the plan against")
args = parser.parse_args(argv)
try:
if args.check:
if not args.inputs:
raise vtr.Fail("--check needs the plan file path")
plan = vtr.load_json(args.inputs[0], "patch plan")
outline = None
if args.outline:
outline = vtr.load_json(args.outline, "outline")
vtr.validate_outline(outline)
vtr.validate_plan(plan, outline=outline)
print(f"PLAN OK: {_summary(plan)}")
return 0
if len(args.inputs) < 2:
raise vtr.Fail("usage: build_track_plan.py outline.json segment_01.txt "
"[segment_02.txt ...] [--plan plan.json] [--json]")
outline = vtr.load_json(args.inputs[0], "outline")
vtr.validate_outline(outline)
fragments = [vtr.load_fragment_any(p, outline, i)
for i, p in enumerate(args.inputs[1:], 1)]
# The merge is the continuity check: build() re-walks every fragment against
# the outline (ids, frames, modes, verbatim media paths, prompt rules, handoff
# chain) and validates the assembled TRACK_DATA — the plan object itself is
# only written when --plan asks for it.
plan = build(outline, fragments)
uses_media = bool(
outline["_roles"].get("master_audio")
or outline["_roles"].get("reference_video")
or any(row.get("images") for row in outline["rows"])
)
# Plain-text form — the DEFAULT paste: the prompt_override input splits on '|'
# with no JSON at all (parse_override_segments -> one segment per part). Only
# valid with zero media — <Picture N>/<Audio N>/<Video N> tokens are JSON-only.
# Unequal row durations or non-default task types are carried by
# [start-end(,type)] markers, which the builder emits per scene (proven:
# [0-240,r2v] renders TYPE r2v, task-duration sum drives the MiniMax total).
prompts_count = len(outline["rows"])
total_s = sum(float(row["duration_seconds"]) for row in outline["rows"])
paste_path = Path(args.output) if args.output else None
json_path = None
if uses_media:
override = vtr.build_prompt_override(outline, fragments)
vtr.validate_prompt_override(copy.deepcopy(override), outline)
if paste_path is None:
paste_path = Path("prompt_override.json")
if paste_path.suffix.lower() != ".json":
raise vtr.Fail("this story has media — the paste must be the JSON payload "
"(<Picture N>/<Audio N>/<Video N> tokens are JSON-only); "
"give -o a .json path")
json_path = paste_path
json_path.write_text(
json.dumps(override, ensure_ascii=True, separators=(",", ":")) + "\n",
encoding="utf-8")
print(f"OVERRIDE OK (JSON payload): {prompts_count} prompts, "
f"duration {override.get('duration', '10 (default)')}, "
f"total {total_s:.2f}s; wrote {json_path} "
f"({json_path.stat().st_size} chars, one line)")
print("PASTE NOTE: this story has media — <Picture N>/<Audio N>/<Video N> tokens are "
"JSON-only, so the paste is prompt_override.json (the text path would leave "
"those tokens as literal prompt text)")
else:
paste_text = vtr.build_plain_text_override(outline, fragments)
vtr.validate_prompt_override_text(paste_text, copy.deepcopy(outline))
if paste_path is None:
paste_path = Path("prompt_override.txt")
paste_path.write_text(paste_text + "\n", encoding="utf-8")
print(f"PLAIN TEXT OK (the paste): {prompts_count} scenes split on '|', "
f"total {total_s:.2f}s; wrote {paste_path} "
f"({paste_path.stat().st_size} chars) — [start-end,type] markers where "
"lengths differ or a scene is not t2v")
if args.json:
override = vtr.build_prompt_override(outline, fragments)
vtr.validate_prompt_override(copy.deepcopy(override), outline)
json_path = paste_path.parent / "prompt_override.json"
json_path.write_text(
json.dumps(override, ensure_ascii=True, separators=(",", ":")) + "\n",
encoding="utf-8")
print(f"OVERRIDE OK (JSON payload): wrote {json_path} — for per-segment "
"continuity modes / track locks the plain text cannot carry")
if args.plan:
plan_path = Path(args.plan)
_write_json(plan_path, plan)
_write_json(plan_path.parent / "track_data.json", plan["editor"]["track_data"])
print(f"PLAN OK: {_summary(plan)}")
print(f"wrote {plan_path} and {plan_path.parent / 'track_data.json'} — for the "
"node pack's patch_workflow.py apply (dry-run first)")
print(f"deliver: paste {paste_path} into the multitrack editor's prompt_override "
f"input (format {outline['editor']['format']!r})")
print("NOTE: easy multiTrackTaskOutput renders ONE task segment per pass "
"(task_index, default 0 = first clip only).")
print(" - all clips: use the pack's example_workflows/MinimaxH3_ForLoop_MultiShot.json "
"(loop index -> task_index, PrimitiveInt -1 = all, auto-merge)")
print(f" - or queue manually with task_index 0..{prompts_count - 1} and join the clips")
return 0
except vtr.Fail as exc:
print(f"FAIL: {exc}")
return 1
if __name__ == "__main__":
sys.exit(_main(sys.argv[1:]))