@chad.dev/dsh-adclip 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +28 -2
  4. package/client.js +2256 -0
  5. package/cordis.patch.yml +10 -0
  6. package/engine/requirements.txt +9 -0
  7. package/engine/src/adclip/__init__.py +0 -0
  8. package/engine/src/adclip/api.py +704 -0
  9. package/engine/src/adclip/catalog.py +341 -0
  10. package/engine/src/adclip/image_params.py +45 -0
  11. package/engine/src/adclip/intents.py +209 -0
  12. package/engine/src/adclip/llm.py +142 -0
  13. package/engine/src/adclip/media/__init__.py +0 -0
  14. package/engine/src/adclip/media/asr.py +79 -0
  15. package/engine/src/adclip/media/ffmpeg.py +294 -0
  16. package/engine/src/adclip/media/stitch.py +124 -0
  17. package/engine/src/adclip/media/subtitles.py +45 -0
  18. package/engine/src/adclip/media/vision.py +105 -0
  19. package/engine/src/adclip/models.py +127 -0
  20. package/engine/src/adclip/params.py +158 -0
  21. package/engine/src/adclip/personas.py +60 -0
  22. package/engine/src/adclip/pipeline/__init__.py +0 -0
  23. package/engine/src/adclip/pipeline/analyze.py +133 -0
  24. package/engine/src/adclip/pipeline/generate.py +196 -0
  25. package/engine/src/adclip/pipeline/image_runner.py +146 -0
  26. package/engine/src/adclip/pipeline/persona.py +56 -0
  27. package/engine/src/adclip/pipeline/post.py +40 -0
  28. package/engine/src/adclip/pipeline/qa.py +44 -0
  29. package/engine/src/adclip/pipeline/replace.py +191 -0
  30. package/engine/src/adclip/pipeline/runner.py +138 -0
  31. package/engine/src/adclip/pipeline/script.py +226 -0
  32. package/engine/src/adclip/pipeline/segment.py +87 -0
  33. package/engine/src/adclip/providers/__init__.py +0 -0
  34. package/engine/src/adclip/providers/ark.py +122 -0
  35. package/engine/src/adclip/providers/ark_image.py +114 -0
  36. package/engine/src/adclip/providers/base.py +30 -0
  37. package/engine/src/adclip/providers/discovery.py +56 -0
  38. package/engine/src/adclip/providers/kling.py +101 -0
  39. package/engine/src/adclip/providers/minimax.py +155 -0
  40. package/engine/src/adclip/providers/minimax_image.py +89 -0
  41. package/engine/src/adclip/settings.py +109 -0
  42. package/engine/src/adclip/store.py +378 -0
  43. package/host/dsh.js +60 -0
  44. package/host/engine.js +265 -0
  45. package/host/proxy.js +142 -0
  46. package/host/supervisor.js +251 -0
  47. package/icon.svg +6 -0
  48. package/index.js +39 -0
  49. package/locale/en.json +12 -0
  50. package/locale/zh.json +12 -0
  51. package/package.json +47 -4
@@ -0,0 +1,45 @@
1
+ """SRT generation from the AdClip script."""
2
+ from __future__ import annotations
3
+
4
+ from pathlib import Path
5
+ from typing import Any
6
+
7
+
8
+ def _ts(seconds: float) -> str:
9
+ seconds = max(0.0, float(seconds))
10
+ h = int(seconds // 3600)
11
+ m = int((seconds % 3600) // 60)
12
+ s = int(seconds % 60)
13
+ ms = int(round((seconds - int(seconds)) * 1000))
14
+ if ms >= 1000:
15
+ s += 1
16
+ ms -= 1000
17
+ return f"{h:02d}:{m:02d}:{s:02d},{ms:03d}"
18
+
19
+
20
+ def write_srt(script: dict[str, Any], target: str | Path) -> Path:
21
+ out = Path(target)
22
+ out.parent.mkdir(parents=True, exist_ok=True)
23
+ rows: list[str] = []
24
+ index = 1
25
+ for beat in script.get("beats") or []:
26
+ lines = beat.get("lines") or []
27
+ text = " ".join(str(line.get("text") or "").strip() for line in lines).strip()
28
+ if not text:
29
+ continue
30
+ start = float(beat.get("start_s") or 0.0)
31
+ end = float(beat.get("end_s") or (start + 1.0))
32
+ rows.extend([str(index), f"{_ts(start)} --> {_ts(end)}", text, ""])
33
+ index += 1
34
+ out.write_text("\n".join(rows).strip() + ("\n" if rows else ""), encoding="utf-8")
35
+ return out
36
+
37
+
38
+ def plain_text(script: dict[str, Any]) -> str:
39
+ parts = []
40
+ for beat in script.get("beats") or []:
41
+ for line in beat.get("lines") or []:
42
+ text = str(line.get("text") or "").strip()
43
+ if text:
44
+ parts.append(text)
45
+ return " ".join(parts)
@@ -0,0 +1,105 @@
1
+ """MiniMax M3 vision analysis for reference keyframes.
2
+
3
+ This is optional and uses the same MiniMax key as text/video. It never blocks a
4
+ run: failures return no visual tags and the heuristic analysis still proceeds.
5
+ """
6
+ from __future__ import annotations
7
+
8
+ import base64
9
+ import json
10
+ import os
11
+ import re
12
+ from pathlib import Path
13
+ from typing import Any
14
+
15
+ import httpx
16
+
17
+ BASE = os.environ.get("MINIMAX_API_BASE", "https://api.minimaxi.com").rstrip("/")
18
+ MODEL = os.environ.get("ADSTUDIO_VISION_MODEL", "MiniMax-M3")
19
+ LAST_ERROR = ""
20
+
21
+
22
+ def available() -> bool:
23
+ if os.environ.get("ADSTUDIO_VISION", "1") == "0":
24
+ return False
25
+ return bool(os.environ.get("MINIMAX_API_KEY", "").strip())
26
+
27
+
28
+ def _strip(text: str) -> str:
29
+ return re.sub(r"<think>.*?</think>", "", text or "", flags=re.S).strip()
30
+
31
+
32
+ def _data_uri(path: Path) -> str:
33
+ return "data:image/jpeg;base64," + base64.b64encode(path.read_bytes()).decode("ascii")
34
+
35
+
36
+ def analyze_frames(frames: list[dict[str, Any]], *, max_frames: int = 6) -> dict[str, dict[str, Any]]:
37
+ """Return visual tags keyed by beat id. Empty dict means unavailable."""
38
+ global LAST_ERROR
39
+ LAST_ERROR = ""
40
+ key = os.environ.get("MINIMAX_API_KEY", "").strip()
41
+ if not key or not frames:
42
+ return {}
43
+ selected = frames[:max_frames]
44
+ usable = [f for f in selected if Path(str(f.get("path") or "")).is_file()]
45
+ if not usable:
46
+ return {}
47
+ labels = []
48
+ content: list[dict[str, Any]] = []
49
+ prompt = (
50
+ "你是广告片视觉分析师。按顺序分析这些参考片关键帧,只描述画面中确实看得见的内容。"
51
+ "输出 JSON:{\"frames\":[{\"id\":\"b0\",\"shot_size\":\"景别\",\"action\":\"人物/动作\","
52
+ "\"people\":\"人数/关系\",\"product\":\"商品及可见包装\",\"scene\":\"场景\","
53
+ "\"on_screen_text\":\"画面文字/价格\"}]}。"
54
+ "看不清的字段留空字符串,不要编造。"
55
+ )
56
+ content.append({"type": "text", "text": prompt})
57
+ for frame in usable:
58
+ label = str(frame.get("id") or "")
59
+ labels.append(label)
60
+ content.append({"type": "text", "text": f"关键帧 {label},时间 {frame.get('start_s', 0)}-{frame.get('end_s', 0)} 秒:"})
61
+ try:
62
+ content.append({"type": "image_url", "image_url": {"url": _data_uri(Path(str(frame["path"])))}})
63
+ except Exception:
64
+ continue
65
+ try:
66
+ with httpx.Client(timeout=httpx.Timeout(120.0, connect=15.0)) as client:
67
+ response = client.post(
68
+ f"{BASE}/v1/chat/completions",
69
+ headers={"Authorization": f"Bearer {key}", "Content-Type": "application/json"},
70
+ json={"model": MODEL, "messages": [{"role": "user", "content": content}], "max_tokens": 4000, "temperature": 0.15},
71
+ )
72
+ if response.status_code >= 400:
73
+ LAST_ERROR = f"HTTP {response.status_code}: {response.text[:240]}"
74
+ return {}
75
+ data = response.json()
76
+ raw = ((data.get("choices") or [{}])[0].get("message") or {}).get("content") or ""
77
+ text = _strip(str(raw))
78
+ start, end = text.find("{"), text.rfind("}")
79
+ if start < 0 or end <= start:
80
+ LAST_ERROR = f"no JSON in {len(text)} chars"
81
+ return {}
82
+ value = json.loads(text[start:end + 1])
83
+ rows = value.get("frames") if isinstance(value, dict) else None
84
+ if not isinstance(rows, list):
85
+ LAST_ERROR = "JSON has no frames[]"
86
+ return {}
87
+ out: dict[str, dict[str, Any]] = {}
88
+ for row in rows:
89
+ if not isinstance(row, dict):
90
+ continue
91
+ frame_id = str(row.get("id") or "")
92
+ if frame_id not in labels:
93
+ continue
94
+ out[frame_id] = {
95
+ "shot_size": str(row.get("shot_size") or "")[:30],
96
+ "action": str(row.get("action") or "")[:200],
97
+ "people": str(row.get("people") or "")[:80],
98
+ "product": str(row.get("product") or "")[:120],
99
+ "scene": str(row.get("scene") or "")[:80],
100
+ "on_screen_text": str(row.get("on_screen_text") or "")[:120],
101
+ }
102
+ return out
103
+ except Exception as exc: # noqa: BLE001
104
+ LAST_ERROR = f"{type(exc).__name__}: {exc}"[:300]
105
+ return {}
@@ -0,0 +1,127 @@
1
+ """Pydantic contracts for AdClip.
2
+
3
+ The service deliberately keeps one user-visible object: a Run. A Run owns its
4
+ storyboard, segments, versions and artifacts. There is no project/version/shot
5
+ or quote object in this service.
6
+ """
7
+ from __future__ import annotations
8
+
9
+ from typing import Any, Literal, Optional
10
+
11
+ from pydantic import BaseModel, Field
12
+
13
+
14
+ class Asset(BaseModel):
15
+ id: str
16
+ name: str
17
+ kind: Literal["video", "image", "audio", "file"]
18
+ path: str
19
+ sha256: str
20
+ bytes: int
21
+ mime: str = "application/octet-stream"
22
+ width: Optional[int] = None
23
+ height: Optional[int] = None
24
+ duration_s: Optional[float] = None
25
+ source: str = "upload"
26
+ created_at: str
27
+ original_name: str = ""
28
+
29
+
30
+ class Persona(BaseModel):
31
+ id: str
32
+ name: str
33
+ type: Literal["ai", "real", "group"] = "ai"
34
+ gender: Literal["male", "female", "any"] = "any"
35
+ age: Literal["teenager", "youth", "middle_aged", "senior"] = "youth"
36
+ ethnicity: Literal[
37
+ "caucasian", "asian", "latino", "african", "middle_eastern", "any"
38
+ ] = "asian"
39
+ body_type: Literal["slim", "standard", "athletic", "chubby"] = "standard"
40
+ exact_age: Optional[int] = None
41
+ height_cm: Optional[int] = None
42
+ hair: str = ""
43
+ outfit: str = ""
44
+ temperament: str = ""
45
+ voice_tone: str = ""
46
+ reference_audio_asset_id: Optional[str] = None
47
+ reference_images: list[str] = Field(default_factory=list)
48
+ role: Literal["A", "B", "both", "group"] = "both"
49
+ locks: dict[str, bool] = Field(default_factory=dict)
50
+ consent: dict[str, Any] = Field(default_factory=lambda: {"confirmed": False, "note": ""})
51
+ created_at: str
52
+ used_in_runs: list[str] = Field(default_factory=list)
53
+
54
+
55
+ class RunStep(BaseModel):
56
+ name: str
57
+ label: str
58
+ status: Literal["pending", "running", "done", "failed", "skipped"] = "pending"
59
+ progress: float = 0.0
60
+ started_at: Optional[str] = None
61
+ finished_at: Optional[str] = None
62
+ error: Optional[str] = None
63
+
64
+
65
+ class StoryboardSegment(BaseModel):
66
+ id: str
67
+ index: int
68
+ start_s: float
69
+ end_s: float
70
+ function: str = "beat"
71
+ source: Literal["one_shot_slice", "generated"] = "one_shot_slice"
72
+ thumbnail: Optional[str] = None
73
+ dialogue: str = ""
74
+ action: str = ""
75
+ product: str = ""
76
+ status: Literal["ok", "warn", "fail"] = "ok"
77
+ asset_id: Optional[str] = None
78
+ take: int = 1
79
+ note: str = ""
80
+
81
+
82
+ class Run(BaseModel):
83
+ id: str
84
+ scenario: str
85
+ title: str = "未命名"
86
+ status: Literal["queued", "running", "succeeded", "failed", "canceled", "interrupted"] = "queued"
87
+ step: str = "queued"
88
+ progress: float = 0.0
89
+ generation_mode: Literal["fast", "fine"] = "fast"
90
+ inputs: dict[str, Any] = Field(default_factory=dict)
91
+ advanced: dict[str, Any] = Field(default_factory=dict)
92
+ persona_map: dict[str, str] = Field(default_factory=dict)
93
+ persona_snapshot: dict[str, Any] = Field(default_factory=dict)
94
+ steps: list[RunStep] = Field(default_factory=list)
95
+ storyboard: list[StoryboardSegment] = Field(default_factory=list)
96
+ result: Optional[dict[str, Any]] = None
97
+ error: Optional[str] = None
98
+ demo: bool = False
99
+ cancel_requested: bool = False
100
+ created_at: str = ""
101
+ finished_at: Optional[str] = None
102
+ parent_run_id: Optional[str] = None
103
+ version: int = 1
104
+ changes: list[dict[str, Any]] = Field(default_factory=list)
105
+ segment_edits: list[dict[str, Any]] = Field(default_factory=list)
106
+
107
+
108
+ class StitchClip(BaseModel):
109
+ asset_id: str
110
+ in_s: float = 0.0
111
+ out_s: Optional[float] = None
112
+
113
+
114
+ class StitchRequest(BaseModel):
115
+ clips: list[StitchClip]
116
+ output: dict[str, Any] = Field(default_factory=dict)
117
+ transition: str = "cut"
118
+ audio: str = "keep"
119
+ fit: str = "blur_pad"
120
+
121
+
122
+ class AssistantRequest(BaseModel):
123
+ message: str
124
+ context: dict[str, Any] = Field(default_factory=dict)
125
+ selected_segment_id: Optional[str] = None
126
+ assets: list[dict[str, Any]] = Field(default_factory=list)
127
+ personas: list[dict[str, Any]] = Field(default_factory=list)
@@ -0,0 +1,158 @@
1
+ """Model catalogue views, scenario manifests and video parameter helpers.
2
+
3
+ The rows live in `catalog.py`, which knows both video and image models. This
4
+ module keeps the video-shaped helpers the pipeline already imports, so callers
5
+ can keep asking for "the video models" without knowing about kinds.
6
+ """
7
+ from __future__ import annotations
8
+
9
+ from typing import Any
10
+
11
+ from . import catalog as model_catalog
12
+
13
+
14
+ def catalog_rows() -> list[dict[str, Any]]:
15
+ return model_catalog.for_kind("video")
16
+
17
+
18
+ def catalog() -> list[dict[str, Any]]:
19
+ """Video models with live key presence and availability."""
20
+ return model_catalog.for_kind("video")
21
+
22
+
23
+ def get_model(model_id: str) -> dict[str, Any] | None:
24
+ row = model_catalog.get(model_id)
25
+ return row if row and row["kind"] == "video" else None
26
+
27
+
28
+ def available_models(capability: str | None = None) -> list[dict[str, Any]]:
29
+ rows = [row for row in model_catalog.for_kind("video") if row.get("available")]
30
+ if capability:
31
+ rows = [row for row in rows if capability in (row.get("capabilities") or [])]
32
+ return rows
33
+
34
+
35
+ def default_model() -> str:
36
+ return model_catalog.default_model("video", "text_to_video")
37
+
38
+
39
+ def supports(model_id: str, key: str, value: Any) -> bool:
40
+ model = get_model(model_id)
41
+ if model is None:
42
+ return False
43
+ if key == "duration":
44
+ return any(float(value) <= float(item) for item in model.get("durations", []))
45
+ if key == "resolution":
46
+ return value in (model.get("resolutions") or [])
47
+ if key == "aspect":
48
+ return value in (model.get("aspects") or [])
49
+ if key == "native_audio":
50
+ return bool(model.get("native_audio")) or not value
51
+ return True
52
+
53
+
54
+ ASPECT_SIZES = {
55
+ "480p": {
56
+ "9:16": (480, 854), "16:9": (854, 480), "1:1": (480, 480),
57
+ "4:3": (640, 480), "3:4": (480, 640), "21:9": (896, 384),
58
+ },
59
+ "720p": {
60
+ "9:16": (720, 1280), "16:9": (1280, 720), "1:1": (720, 720),
61
+ "4:3": (960, 720), "3:4": (720, 960), "21:9": (1280, 548),
62
+ },
63
+ "1080p": {
64
+ "9:16": (1080, 1920), "16:9": (1920, 1080), "1:1": (1080, 1080),
65
+ "4:3": (1440, 1080), "3:4": (1080, 1440), "21:9": (1920, 822),
66
+ },
67
+ "4k": {
68
+ "9:16": (2160, 3840), "16:9": (3840, 2160), "1:1": (2160, 2160),
69
+ "4:3": (2880, 2160), "3:4": (2160, 2880), "21:9": (3840, 1644),
70
+ },
71
+ }
72
+
73
+
74
+ def size_for(aspect: str, resolution: str) -> tuple[int, int]:
75
+ bucket = ASPECT_SIZES.get(resolution) or ASPECT_SIZES["720p"]
76
+ return bucket.get(aspect) or bucket["9:16"]
77
+
78
+
79
+ DEFAULT_ADVANCED = {
80
+ "generation_mode": "fast",
81
+ "duration": 12,
82
+ "aspect": "9:16",
83
+ "resolution": "720p",
84
+ "model": "auto",
85
+ "fidelity": "balanced",
86
+ "language": "zh",
87
+ "native_audio": True,
88
+ "subtitle": "off",
89
+ "subtitle_size": 32,
90
+ "music": "off",
91
+ "export_bitrate": "8M",
92
+ "fps": 30,
93
+ "aigc_label": True,
94
+ }
95
+
96
+
97
+ SCENARIOS: list[dict[str, Any]] = [
98
+ {
99
+ "id": "viral-remake",
100
+ "label": "爆款复刻",
101
+ "status": "ready",
102
+ "description": "学习参考片的结构和节奏,换成你的商品重拍",
103
+ "inputs": [
104
+ {"key": "reference", "type": "video", "label": "参考视频", "required": True, "max_duration_s": 30},
105
+ {"key": "product_images", "type": "image[]", "label": "商品图", "required": False, "max_items": 9},
106
+ {"key": "brief", "type": "text", "label": "产品信息、卖点、价格及修改要求", "max_length": 2000},
107
+ ],
108
+ "advanced": [
109
+ {"key": "generation_mode", "label": "生成模式", "type": "enum", "options": ["fast", "fine"], "default": "fast"},
110
+ {"key": "duration", "label": "时长", "type": "enum", "options": [5, 8, 10, 12, 15, 30], "default": 12},
111
+ {"key": "aspect", "label": "比例", "type": "enum", "options": ["9:16", "16:9", "1:1", "4:3", "3:4", "21:9"], "default": "9:16"},
112
+ {"key": "resolution", "label": "清晰度", "type": "enum", "options": ["480p", "720p", "1080p"], "default": "720p"},
113
+ {"key": "model", "label": "模型", "type": "model", "default": "auto"},
114
+ {"key": "subtitle", "label": "字幕", "type": "enum", "options": ["off", "sidecar"], "default": "off"},
115
+ ],
116
+ "examples": [],
117
+ },
118
+ {
119
+ "id": "text-to-video",
120
+ "label": "文生视频",
121
+ "status": "ready",
122
+ "description": "一句描述生成一段视频",
123
+ "inputs": [
124
+ {"key": "brief", "type": "text", "label": "描述产品、场景、动作、镜头和氛围", "required": True, "max_length": 1000},
125
+ {"key": "reference_image", "type": "image", "label": "参考图(可选)", "required": False},
126
+ ],
127
+ "advanced": [
128
+ {"key": "duration", "label": "时长", "type": "enum", "options": [5, 8, 10, 12, 15, 30], "default": 8},
129
+ {"key": "aspect", "label": "比例", "type": "enum", "options": ["9:16", "16:9", "1:1"], "default": "9:16"},
130
+ {"key": "resolution", "label": "清晰度", "type": "enum", "options": ["480p", "720p", "1080p"], "default": "720p"},
131
+ {"key": "model", "label": "模型", "type": "model", "default": "auto"},
132
+ {"key": "subtitle", "label": "字幕", "type": "enum", "options": ["off", "sidecar"], "default": "off"},
133
+ ],
134
+ "examples": [],
135
+ },
136
+ {
137
+ "id": "video-fission",
138
+ "label": "视频裂变",
139
+ "status": "coming_soon",
140
+ "description": "一条模板出多组变体",
141
+ "inputs": [],
142
+ "advanced": [],
143
+ "examples": [],
144
+ },
145
+ ]
146
+
147
+
148
+ def scenario(scenario_id: str) -> dict[str, Any] | None:
149
+ for row in SCENARIOS:
150
+ if row["id"] == scenario_id:
151
+ return row
152
+ return None
153
+
154
+
155
+ def merged_advanced(values: dict[str, Any] | None) -> dict[str, Any]:
156
+ out = dict(DEFAULT_ADVANCED)
157
+ out.update(values or {})
158
+ return out
@@ -0,0 +1,60 @@
1
+ """Persona creation and Tencent-compatible defaults."""
2
+ from __future__ import annotations
3
+
4
+ from typing import Any
5
+
6
+ from . import store
7
+ from .models import Persona
8
+
9
+
10
+ def create(data: dict[str, Any]) -> dict[str, Any]:
11
+ payload = {
12
+ "id": data.get("id") or store.new_id("persona"),
13
+ "name": str(data.get("name") or "未命名角色"),
14
+ "type": data.get("type") or "ai",
15
+ "gender": data.get("gender") or "any",
16
+ "age": data.get("age") or "youth",
17
+ "ethnicity": data.get("ethnicity") or "asian",
18
+ "body_type": data.get("body_type") or "standard",
19
+ "exact_age": data.get("exact_age"),
20
+ "height_cm": data.get("height_cm"),
21
+ "hair": str(data.get("hair") or ""),
22
+ "outfit": str(data.get("outfit") or ""),
23
+ "temperament": str(data.get("temperament") or ""),
24
+ "voice_tone": str(data.get("voice_tone") or ""),
25
+ "reference_audio_asset_id": data.get("reference_audio_asset_id"),
26
+ "reference_images": list(data.get("reference_images") or []),
27
+ "role": data.get("role") or "both",
28
+ "locks": dict(data.get("locks") or {}),
29
+ "consent": dict(data.get("consent") or {"confirmed": False, "note": ""}),
30
+ "created_at": store.now(),
31
+ "used_in_runs": list(data.get("used_in_runs") or []),
32
+ }
33
+ persona = Persona.model_validate(payload)
34
+ if persona.type == "real" and not persona.consent.get("confirmed"):
35
+ raise ValueError("真人角色必须确认授权")
36
+ return store.save_persona(persona).model_dump()
37
+
38
+
39
+ def update(persona_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
40
+ current = store.get_persona(persona_id)
41
+ if current is None:
42
+ return None
43
+ merged = {**current, **data, "id": persona_id, "created_at": current.get("created_at") or store.now()}
44
+ persona = Persona.model_validate(merged)
45
+ if persona.type == "real" and not persona.consent.get("confirmed"):
46
+ raise ValueError("真人角色必须确认授权")
47
+ return store.save_persona(persona).model_dump()
48
+
49
+
50
+ def delete(persona_id: str) -> bool:
51
+ return store.delete_persona(persona_id)
52
+
53
+
54
+ def ensure_default() -> None:
55
+ if not store.list_personas():
56
+ store.save_persona(Persona.model_validate(store.default_persona()))
57
+
58
+
59
+ def resolve(persona_id: str) -> dict[str, Any]:
60
+ return store.get_persona(persona_id) or store.default_persona()
File without changes
@@ -0,0 +1,133 @@
1
+ """Reference analysis: probe, cut detection, beat grid, storyboard thumbnails."""
2
+ from __future__ import annotations
3
+
4
+ import json
5
+ from pathlib import Path
6
+ from typing import Any
7
+
8
+ from .. import store
9
+ from ..media import asr, ffmpeg, vision
10
+
11
+
12
+ def _beats(duration: float, cuts: list[float]) -> list[dict[str, Any]]:
13
+ points = [0.0] + [round(c, 3) for c in cuts if 0.4 < c < duration - 0.4] + [round(duration, 3)]
14
+ if len(points) <= 2:
15
+ step = max(2.0, duration / 4.0)
16
+ points = [0.0]
17
+ t = step
18
+ while t < duration - 1.0:
19
+ points.append(round(t, 3))
20
+ t += step
21
+ points.append(round(duration, 3))
22
+ # merge tiny beats into the next one
23
+ merged: list[float] = [points[0]]
24
+ for value in points[1:]:
25
+ if value - merged[-1] < 0.8 and value != points[-1]:
26
+ continue
27
+ merged.append(value)
28
+ if merged[-1] != points[-1]:
29
+ merged.append(points[-1])
30
+ # cap at 6 readable beats
31
+ while len(merged) > 7:
32
+ idx = min(range(1, len(merged) - 1), key=lambda i: merged[i] - merged[i - 1])
33
+ del merged[idx]
34
+ functions = ["hook", "setup", "escalation", "insult", "cta", "punchline", "beat"]
35
+ rows = []
36
+ for i in range(len(merged) - 1):
37
+ start, end = merged[i], merged[i + 1]
38
+ if end - start < 0.25:
39
+ continue
40
+ rows.append(
41
+ {
42
+ "id": f"b{len(rows)}",
43
+ "start_s": round(start, 3),
44
+ "end_s": round(end, 3),
45
+ "len_s": round(end - start, 2),
46
+ "function": functions[min(len(rows), len(functions) - 1)],
47
+ "function_source": "heuristic",
48
+ "confidence": 0.55,
49
+ "dialogue": [],
50
+ "visual": {"action": "", "shot_size": "中景", "product": {}},
51
+ }
52
+ )
53
+ return rows
54
+
55
+
56
+ def analyze(run_id: str) -> dict[str, Any]:
57
+ run = store.read_run(run_id)
58
+ if run is None:
59
+ raise FileNotFoundError(run_id)
60
+ root = store.run_dir(run_id)
61
+ root.mkdir(parents=True, exist_ok=True)
62
+ ref_id = (run.inputs or {}).get("reference_asset_id")
63
+ source = store.asset_media_path(ref_id) if ref_id else None
64
+ facts = ffmpeg.probe(source) if source else {
65
+ "duration_s": float((run.advanced or {}).get("duration") or 12),
66
+ "width": 720, "height": 1280, "fps": 30, "aspect": "9:16", "has_audio": False,
67
+ }
68
+ duration = float(facts["duration_s"])
69
+ cuts = ffmpeg.detect_cuts(source, duration) if source else []
70
+ beats = _beats(duration, cuts)
71
+ transcript: list[dict[str, Any]] = []
72
+ if source and facts.get("has_audio") and asr.available():
73
+ try:
74
+ transcript = asr.transcribe(source)
75
+ except Exception:
76
+ transcript = []
77
+ for beat in beats:
78
+ if transcript:
79
+ texts = [
80
+ item["text"]
81
+ for item in transcript
82
+ if float(item["start_s"]) < float(beat["end_s"]) and float(item["end_s"]) > float(beat["start_s"])
83
+ ]
84
+ beat["dialogue"] = texts
85
+ beat["dialogue_source"] = "asr"
86
+ else:
87
+ beat["dialogue"] = []
88
+ if source:
89
+ thumb = store.run_artifact(run_id, f"storyboard/{beat['id']}.jpg")
90
+ try:
91
+ ffmpeg.extract_frame(source, (beat["start_s"] + beat["end_s"]) / 2, thumb, width=360)
92
+ beat["thumbnail"] = store.rel_to_data(thumb)
93
+ except Exception:
94
+ beat["thumbnail"] = None
95
+ visual: dict[str, Any] = {}
96
+ if source and vision.available():
97
+ frames = [
98
+ {"id": beat["id"], "start_s": beat["start_s"], "end_s": beat["end_s"], "path": store.run_artifact(run_id, f"storyboard/{beat['id']}.jpg")}
99
+ for beat in beats
100
+ if store.run_artifact(run_id, f"storyboard/{beat['id']}.jpg").is_file()
101
+ ]
102
+ try:
103
+ visual = vision.analyze_frames(frames)
104
+ except Exception:
105
+ visual = {}
106
+ for beat in beats:
107
+ item = visual.get(beat["id"]) or {}
108
+ if item:
109
+ beat["visual"] = {
110
+ "action": item.get("action") or "",
111
+ "shot_size": item.get("shot_size") or "中景",
112
+ "product": {"name": item.get("product") or ""},
113
+ "people": item.get("people") or "",
114
+ "scene": item.get("scene") or "",
115
+ "on_screen_text": item.get("on_screen_text") or "",
116
+ "source": "vlm",
117
+ }
118
+ skeleton = {
119
+ "version": 2,
120
+ "partial": False,
121
+ "source": {**facts, "asset_id": ref_id},
122
+ "cuts_s": cuts,
123
+ "beats": beats,
124
+ "transcript": transcript,
125
+ "confidence": {"asr": 0.85 if transcript else 0.0, "vision": 0.75 if visual else 0.0, "speakers": 0.0},
126
+ "notes": [
127
+ "ASR transcript attached." if transcript else "No transcript: sound may be absent, ASR disabled, or speech not recognized.",
128
+ "M3 vision tags attached to keyframes." if visual else ("Vision unavailable or disabled." if not vision.available() else "Vision returned no usable tags."),
129
+ ],
130
+ }
131
+ path = root / "artifacts" / "skeleton.json"
132
+ store.atomic_write_json(path, skeleton)
133
+ return skeleton