137 lines
4.7 KiB
Python
137 lines
4.7 KiB
Python
|
|
"""Versioned story continuity and half-open frame projection."""
|
||
|
|
|
||
|
|
from typing import Literal
|
||
|
|
from pydantic import Field, model_validator
|
||
|
|
from app.domain.stories import StorySpecV3, StoryShot, Edit, FrameParameters
|
||
|
|
from app.domain.timeline import Strict, Render, Audio
|
||
|
|
from app.schemas.editor_audio import AudioSelectionV2
|
||
|
|
from app.domain.stories import VideoParameters
|
||
|
|
|
||
|
|
|
||
|
|
class ProductionPolicy(Strict):
|
||
|
|
max_shots: int = Field(default=60, ge=1, le=1000)
|
||
|
|
max_total_frames: int = Field(default=7200, ge=1)
|
||
|
|
max_repairs_per_shot: int = Field(default=2, ge=0, le=2)
|
||
|
|
max_total_repairs: int = Field(default=12, ge=0)
|
||
|
|
|
||
|
|
|
||
|
|
class ContinuityState(Strict):
|
||
|
|
summary: str = ""
|
||
|
|
entities: dict[str, str] = Field(default_factory=dict)
|
||
|
|
location: str = ""
|
||
|
|
time: str = ""
|
||
|
|
camera: str = ""
|
||
|
|
motion: str = ""
|
||
|
|
|
||
|
|
|
||
|
|
class ShotContinuity(Strict):
|
||
|
|
transition: Literal["cut", "continue", "scene_change"] = "cut"
|
||
|
|
group_id: str | None = None
|
||
|
|
state_in: ContinuityState = Field(default_factory=ContinuityState)
|
||
|
|
state_out: ContinuityState = Field(default_factory=ContinuityState)
|
||
|
|
end_prompt: str = ""
|
||
|
|
expected_motion: bool = True
|
||
|
|
endpoint_mode: Literal["first_frame", "first_last_frame"] = "first_frame"
|
||
|
|
|
||
|
|
|
||
|
|
class NativeVideoParameters(FrameParameters):
|
||
|
|
profile: Literal["minimax-h3-fl2va-bf16-v2"]
|
||
|
|
source_frames: int = Field(default=124, ge=124, le=345)
|
||
|
|
num_inference_steps: int = Field(default=50, ge=2, le=100)
|
||
|
|
upscale: bool = False
|
||
|
|
|
||
|
|
@model_validator(mode="after")
|
||
|
|
def valid_length(self):
|
||
|
|
if (self.source_frames - 124) % 17:
|
||
|
|
raise ValueError("invalid H3-v2 native frame count")
|
||
|
|
return self
|
||
|
|
|
||
|
|
|
||
|
|
class EditV4(Edit):
|
||
|
|
dedup_frames: Literal[0, 1] = 0
|
||
|
|
|
||
|
|
|
||
|
|
class StoryShotV4(StoryShot):
|
||
|
|
continuity: ShotContinuity = Field(default_factory=ShotContinuity)
|
||
|
|
edit: EditV4 = Field(default_factory=EditV4)
|
||
|
|
video_parameters: NativeVideoParameters | VideoParameters = Field(
|
||
|
|
default_factory=VideoParameters
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
class RenderV4(Render):
|
||
|
|
audio: AudioSelectionV2 | Audio | None = None
|
||
|
|
|
||
|
|
|
||
|
|
class StorySpecV4(StorySpecV3):
|
||
|
|
schema_version: Literal[4] = 4
|
||
|
|
shots: list[StoryShotV4] = Field(min_length=1, max_length=1000)
|
||
|
|
render: RenderV4 = Field(default_factory=RenderV4)
|
||
|
|
|
||
|
|
@model_validator(mode="after")
|
||
|
|
def valid_edges(self):
|
||
|
|
previous = None
|
||
|
|
for shot in self.shots:
|
||
|
|
if shot.continuity.transition == "continue":
|
||
|
|
if (
|
||
|
|
previous is None
|
||
|
|
or previous.scene_id != shot.scene_id
|
||
|
|
or previous.continuity.group_id != shot.continuity.group_id
|
||
|
|
):
|
||
|
|
raise ValueError("continue requires preceding shot in same scene and group")
|
||
|
|
before = previous.continuity.state_out
|
||
|
|
after = shot.continuity.state_in
|
||
|
|
for field in ("location", "time", "camera", "motion"):
|
||
|
|
if (
|
||
|
|
getattr(before, field)
|
||
|
|
and getattr(after, field)
|
||
|
|
and getattr(before, field) != getattr(after, field)
|
||
|
|
):
|
||
|
|
raise ValueError("inconsistent continue state: " + field)
|
||
|
|
if any(
|
||
|
|
before.entities[key] != after.entities[key]
|
||
|
|
for key in before.entities.keys() & after.entities.keys()
|
||
|
|
):
|
||
|
|
raise ValueError("inconsistent continue entity state")
|
||
|
|
elif shot.edit.dedup_frames:
|
||
|
|
raise ValueError("dedup requires continue")
|
||
|
|
if shot.continuity.endpoint_mode == "first_last_frame" and not isinstance(
|
||
|
|
shot.video_parameters, NativeVideoParameters
|
||
|
|
):
|
||
|
|
raise ValueError("first_last_frame requires H3-v2")
|
||
|
|
previous = shot
|
||
|
|
return self
|
||
|
|
|
||
|
|
|
||
|
|
def compile_story_timing(spec):
|
||
|
|
from app.domain.stories import parse_story_spec
|
||
|
|
|
||
|
|
model = parse_story_spec(spec)
|
||
|
|
shots = []
|
||
|
|
cursor = 0
|
||
|
|
for shot in model.shots:
|
||
|
|
edit = shot.edit
|
||
|
|
q = getattr(edit, "dedup_frames", 0)
|
||
|
|
count = edit.duration_frames - q
|
||
|
|
shots.append(
|
||
|
|
dict(
|
||
|
|
shot_id=shot.id,
|
||
|
|
source_start_frame=edit.start_frame,
|
||
|
|
source_end_frame=edit.start_frame + edit.duration_frames,
|
||
|
|
dedup_frames=q,
|
||
|
|
contribution_frames=count,
|
||
|
|
start_frame=cursor,
|
||
|
|
end_frame=cursor + count,
|
||
|
|
)
|
||
|
|
)
|
||
|
|
cursor += count
|
||
|
|
return dict(total_frames=cursor, shots=shots)
|
||
|
|
|
||
|
|
|
||
|
|
def upgrade_story_spec(spec):
|
||
|
|
from app.domain.stories import parse_story_spec
|
||
|
|
|
||
|
|
raw = parse_story_spec(spec).model_dump(mode="json")
|
||
|
|
raw["schema_version"] = 4
|
||
|
|
return StorySpecV4.model_validate(raw).model_dump(mode="json")
|