"""track_data (the multitrack editor JSON) -> RenderPlan. Pure extraction, no ComfyUI. Mirrors what ``MultiTrackEditor.execute`` feeds into TRACKS_INFO for the fixed pipeline: task tracks carry the prompt segments that become generation tasks; video/audio tracks carry media references. """ from __future__ import annotations import json from dataclasses import dataclass, field from typing import Any class TrackDataError(ValueError): pass @dataclass class Segment: id: str start_frame: int end_frame: int prompt: str = "" task_mode: str = "default" continuity_mode: str = "shot" ref_image_size: str = "match" media_type: str = "none" images: list[Any] = field(default_factory=list) muted: bool = False volume_db: float = 0.0 @property def frame_count(self) -> int: return max(0, self.end_frame - self.start_frame) @dataclass class Track: id: str name: str type: str task_mode: str = "default" muted: bool = False solo: bool = False volume_db: float = 0.0 locked: bool = False segments: list[Segment] = field(default_factory=list) @dataclass class RenderPlan: width: int height: int frame_rate: int total_length: int segments: list[Segment] = field(default_factory=list) # ordered task segments tracks: list[Track] = field(default_factory=list) project_name: str = "MoxieBench" steps: int = 4 seed: int = 0 lora: str | None = None lora_strength: float = 1.0 upscale_width: int | None = None upscale_height: int | None = None upscale_quality: str = "ULTRA" codec: str = "h264" @property def duration_seconds(self) -> float: return self.total_length / float(self.frame_rate or 24) def summary(self) -> dict[str, Any]: return { "width": self.width, "height": self.height, "frame_rate": self.frame_rate, "total_length": self.total_length, "duration_seconds": round(self.duration_seconds, 3), "steps": self.steps, "seed": self.seed, "lora": self.lora, "project_name": self.project_name, "upscale": ( {"width": self.upscale_width, "height": self.upscale_height, "quality": self.upscale_quality} if self.upscale_width else None ), "segments": [ { "id": s.id, "prompt": s.prompt, "start_frame": s.start_frame, "end_frame": s.end_frame, "frame_count": s.frame_count, "task_mode": s.task_mode, "continuity_mode": s.continuity_mode, } for s in self.segments ], } def parse_track_data(raw: str | dict[str, Any]) -> dict[str, Any]: if isinstance(raw, str): try: raw = json.loads(raw) except json.JSONDecodeError as exc: raise TrackDataError(f"track_data is not valid JSON: {exc}") from exc if not isinstance(raw, dict): raise TrackDataError("track_data must be an object") if not isinstance(raw.get("tracks"), list): raise TrackDataError("track_data.tracks must be a list") return raw def build_render_plan( track_data: str | dict[str, Any], *, width: int, height: int, frame_rate: int | None = None, steps: int = 4, seed: int = 0, project_name: str = "MoxieBench", lora: str | None = None, lora_strength: float = 1.0, upscale_width: int | None = None, upscale_height: int | None = None, upscale_quality: str = "ULTRA", codec: str = "h264", ) -> RenderPlan: data = parse_track_data(track_data) # Timeline fps: editor stores it on track_data.frame_rate (24 in the base # workflow); the combine project_data may override it. if frame_rate is None: frame_rate = int(data.get("frame_rate") or 24) total = int(data.get("total_length") or 0) tracks: list[Track] = [] segments: list[Segment] = [] for t in data["tracks"]: if not isinstance(t, dict): continue track = Track( id=str(t.get("id", "")), name=str(t.get("name", "")), type=str(t.get("type", "task")), task_mode=str(t.get("task_mode", "default")), muted=bool(t.get("muted", False)), solo=bool(t.get("solo", False)), volume_db=float(t.get("volume_db", 0)), locked=bool(t.get("locked", False)), ) for seg in t.get("segments") or []: if not isinstance(seg, dict): continue content = seg.get("content") or {} s = Segment( id=str(seg.get("id", "")), start_frame=int(seg.get("start_frame", 0)), end_frame=int(seg.get("end_frame", 0)), prompt=str(content.get("user_prompt", "") or ""), task_mode=str(content.get("task_mode", track.task_mode or "default")), continuity_mode=str(content.get("continuity_mode", "shot")), ref_image_size=str(content.get("ref_image_size", "match")), media_type=str(content.get("media_type", "none")), images=list(content.get("images") or []), muted=bool(content.get("muted", False)), volume_db=float(content.get("volume_db", 0)), ) track.segments.append(s) if track.type == "task" and s.frame_count > 0: segments.append(s) tracks.append(track) segments.sort(key=lambda s: (s.start_frame, s.end_frame)) return RenderPlan( width=int(width), height=int(height), frame_rate=int(frame_rate), total_length=total, segments=segments, tracks=tracks, project_name=str(project_name), steps=int(steps), seed=int(seed), lora=lora, lora_strength=float(lora_strength), upscale_width=upscale_width, upscale_height=upscale_height, upscale_quality=str(upscale_quality), codec=str(codec), )