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

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 (49) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +4 -0
  3. package/README.md +28 -2
  4. package/client.js +2157 -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 +676 -0
  9. package/engine/src/adclip/image_params.py +64 -0
  10. package/engine/src/adclip/intents.py +190 -0
  11. package/engine/src/adclip/llm.py +142 -0
  12. package/engine/src/adclip/media/__init__.py +0 -0
  13. package/engine/src/adclip/media/asr.py +79 -0
  14. package/engine/src/adclip/media/ffmpeg.py +294 -0
  15. package/engine/src/adclip/media/stitch.py +124 -0
  16. package/engine/src/adclip/media/subtitles.py +45 -0
  17. package/engine/src/adclip/media/vision.py +105 -0
  18. package/engine/src/adclip/models.py +127 -0
  19. package/engine/src/adclip/params.py +363 -0
  20. package/engine/src/adclip/personas.py +60 -0
  21. package/engine/src/adclip/pipeline/__init__.py +0 -0
  22. package/engine/src/adclip/pipeline/analyze.py +133 -0
  23. package/engine/src/adclip/pipeline/generate.py +201 -0
  24. package/engine/src/adclip/pipeline/image_runner.py +131 -0
  25. package/engine/src/adclip/pipeline/persona.py +56 -0
  26. package/engine/src/adclip/pipeline/post.py +33 -0
  27. package/engine/src/adclip/pipeline/qa.py +41 -0
  28. package/engine/src/adclip/pipeline/replace.py +191 -0
  29. package/engine/src/adclip/pipeline/runner.py +138 -0
  30. package/engine/src/adclip/pipeline/script.py +226 -0
  31. package/engine/src/adclip/pipeline/segment.py +87 -0
  32. package/engine/src/adclip/providers/__init__.py +0 -0
  33. package/engine/src/adclip/providers/ark.py +122 -0
  34. package/engine/src/adclip/providers/base.py +30 -0
  35. package/engine/src/adclip/providers/discovery.py +56 -0
  36. package/engine/src/adclip/providers/kling.py +101 -0
  37. package/engine/src/adclip/providers/minimax.py +155 -0
  38. package/engine/src/adclip/providers/minimax_image.py +86 -0
  39. package/engine/src/adclip/settings.py +109 -0
  40. package/engine/src/adclip/store.py +378 -0
  41. package/host/dsh.js +60 -0
  42. package/host/engine.js +265 -0
  43. package/host/proxy.js +142 -0
  44. package/host/supervisor.js +251 -0
  45. package/icon.svg +6 -0
  46. package/index.js +39 -0
  47. package/locale/en.json +12 -0
  48. package/locale/zh.json +12 -0
  49. package/package.json +47 -4
@@ -0,0 +1,191 @@
1
+ """True single-shot replacement and rollback.
2
+
3
+ Unlike the older /regenerate endpoint, this keeps the existing final clip and
4
+ only regenerates one storyboard slot, then splices it back with ffmpeg. The
5
+ previous final is kept under versions/ so the edit can be rolled back.
6
+ """
7
+ from __future__ import annotations
8
+
9
+ import os
10
+ import shutil
11
+ import threading
12
+ import traceback
13
+ from pathlib import Path
14
+ from typing import Any
15
+
16
+ from .. import params, store
17
+ from ..media import ffmpeg
18
+ from ..media import stitch as stitch_mod
19
+ from . import generate, qa, segment
20
+
21
+
22
+ def _storyboard(run: Any) -> list[dict[str, Any]]:
23
+ data = store.read_json(store.run_dir(run.id) / "artifacts" / "storyboard.json", {}) or {}
24
+ return list(data.get("segments") or run.storyboard or [])
25
+
26
+
27
+ def _find(run: Any, segment_id: str) -> dict[str, Any]:
28
+ for row in _storyboard(run):
29
+ if str(row.get("id")) == segment_id:
30
+ return row
31
+ raise ValueError(f"segment not found: {segment_id}")
32
+
33
+
34
+ def _versions(run_id: str) -> Path:
35
+ path = store.run_dir(run_id) / "versions"
36
+ path.mkdir(parents=True, exist_ok=True)
37
+ return path
38
+
39
+
40
+ def _backup(run: Any, final: Path) -> str:
41
+ name = f"v{run.version}_{store.new_id('pre')}.mp4"
42
+ target = _versions(run.id) / name
43
+ shutil.copy2(final, target)
44
+ return f"versions/{name}"
45
+
46
+
47
+ def _refresh(run: Any) -> None:
48
+ final = store.run_artifact(run.id, "final.mp4")
49
+ segment.build_storyboard(run.id)
50
+ qa_report = qa.check(run.id)
51
+ facts = ffmpeg.probe(final)
52
+ result = dict(run.result or {})
53
+ result.update({
54
+ "video": store.rel_to_data(final),
55
+ "srt": store.rel_to_data(store.run_artifact(run.id, "subtitles.srt")) if store.run_artifact(run.id, "subtitles.srt").is_file() else None,
56
+ "width": facts.get("width"),
57
+ "height": facts.get("height"),
58
+ "duration_s": facts.get("duration_s"),
59
+ "qa": qa_report,
60
+ "demo": run.demo,
61
+ "model": result.get("model"),
62
+ })
63
+ run.result = result
64
+ run.status = "succeeded"
65
+ run.step = "qa"
66
+ run.progress = 1.0
67
+ run.error = None
68
+ run.finished_at = store.now()
69
+ store.write_run(run)
70
+
71
+
72
+ def _run_replace(run_id: str, segment_id: str) -> None:
73
+ run = store.read_run(run_id)
74
+ if run is None:
75
+ return
76
+ try:
77
+ seg = _find(run, segment_id)
78
+ final = store.run_artifact(run_id, "final.mp4")
79
+ if not final.is_file():
80
+ raise FileNotFoundError("final video is missing")
81
+ advanced = params.merged_advanced(run.advanced)
82
+ width, height = params.size_for(str(advanced.get("aspect") or "9:16"), str(advanced.get("resolution") or "720p"))
83
+ fps = int(advanced.get("fps") or 30)
84
+ old_facts = ffmpeg.probe(final)
85
+ start = max(0.0, float(seg.get("start_s") or 0))
86
+ end = min(float(old_facts["duration_s"]), float(seg.get("end_s") or old_facts["duration_s"]))
87
+ slot_len = max(0.5, end - start)
88
+ raw = store.run_artifact(run_id, f"segment_replace_{segment_id}_raw.mp4")
89
+ normalized = store.run_artifact(run_id, f"segment_replace_{segment_id}.mp4")
90
+ generated = generate.generate_segment(run, seg, raw)
91
+ run = store.read_run(run_id) or run
92
+ run.progress = 0.65
93
+ run.step = "stitch"
94
+ store.write_run(run)
95
+ ffmpeg.loop_to_duration(raw, normalized, width=width, height=height, fps=fps, duration=slot_len)
96
+
97
+ before = start > 0.08
98
+ after = end < float(old_facts["duration_s"]) - 0.08
99
+ clips: list[dict[str, Any]] = []
100
+ if before:
101
+ clips.append({"path": str(final), "in_s": 0.0, "out_s": start})
102
+ clips.append({"path": str(normalized), "in_s": 0.0, "out_s": slot_len})
103
+ if after:
104
+ clips.append({"path": str(final), "in_s": end, "out_s": float(old_facts["duration_s"])})
105
+ target = store.run_artifact(run_id, "final.replace.mp4")
106
+ plan_data = stitch_mod.plan(clips, width=width, height=height, fit="black_pad", transition="cut", audio="keep")
107
+ stitch_mod.execute(plan_data, target)
108
+ run = store.read_run(run_id) or run
109
+ run.progress = 0.9
110
+ run.step = "qa"
111
+ store.write_run(run)
112
+ backup = _backup(run, final)
113
+ os.replace(target, final)
114
+ run = store.read_run(run_id) or run
115
+ run.version = int(run.version or 1) + 1
116
+ run.segment_edits = list(run.segment_edits or [])
117
+ run.segment_edits.append({
118
+ "segment_id": segment_id,
119
+ "index": seg.get("index"),
120
+ "from_version": int(run.version) - 1,
121
+ "to_version": int(run.version),
122
+ "backup": backup,
123
+ "model": generated.get("model"),
124
+ "created_at": store.now(),
125
+ })
126
+ run.changes = list(run.changes or [])
127
+ run.changes.append({
128
+ "field": "segment.replace",
129
+ "from": seg.get("index"),
130
+ "to": segment_id,
131
+ "stage": "generate",
132
+ "requires_regeneration": False,
133
+ "label": "替换分镜 #" + str(int(seg.get("index") or 0) + 1),
134
+ })
135
+ _refresh(run)
136
+ except Exception as exc: # noqa: BLE001
137
+ run = store.read_run(run_id)
138
+ if run:
139
+ run.status = "failed"
140
+ run.error = f"{type(exc).__name__}: {exc}"[:800]
141
+ run.finished_at = store.now()
142
+ store.write_run(run)
143
+ traceback.print_exc()
144
+
145
+
146
+ def start_replace(run_id: str, segment_id: str) -> Any:
147
+ run = store.read_run(run_id)
148
+ if run is None:
149
+ raise ValueError("run not found")
150
+ if run.status == "running":
151
+ raise ValueError("run is busy")
152
+ seg = _find(run, segment_id)
153
+ if run.status != "succeeded":
154
+ raise ValueError("only a succeeded run can be edited")
155
+ run.status = "running"
156
+ run.step = "segment"
157
+ run.progress = 0.1
158
+ run.error = None
159
+ store.write_run(run)
160
+ threading.Thread(target=_run_replace, args=(run_id, segment_id), daemon=True, name="adstudio-segment-replace").start()
161
+ return run
162
+
163
+
164
+ def rollback(run_id: str) -> Any:
165
+ run = store.read_run(run_id)
166
+ if run is None:
167
+ raise ValueError("run not found")
168
+ edits = list(run.segment_edits or [])
169
+ if not edits:
170
+ raise ValueError("no segment edits to roll back")
171
+ entry = edits[-1]
172
+ backup = store.run_dir(run_id) / str(entry.get("backup") or "")
173
+ final = store.run_artifact(run_id, "final.mp4")
174
+ if not backup.is_file():
175
+ raise ValueError("segment backup is missing")
176
+ current = _versions(run_id) / f"v{run.version}_{store.new_id('rollback')}.mp4"
177
+ shutil.copy2(final, current)
178
+ shutil.copy2(backup, final)
179
+ run.segment_edits = edits[:-1]
180
+ run.version = int(run.version or 1) + 1
181
+ run.changes = list(run.changes or [])
182
+ run.changes.append({
183
+ "field": "segment.rollback",
184
+ "from": entry.get("segment_id"),
185
+ "to": None,
186
+ "stage": "generate",
187
+ "requires_regeneration": False,
188
+ "label": "回滚分镜 #" + str(int(entry.get("index") or 0) + 1),
189
+ })
190
+ _refresh(run)
191
+ return run
@@ -0,0 +1,138 @@
1
+ """Sequential run executor with cancellation and step persistence."""
2
+ from __future__ import annotations
3
+
4
+ import traceback
5
+ from concurrent.futures import ThreadPoolExecutor
6
+ from typing import Any
7
+
8
+ from .. import store
9
+ from ..models import RunStep
10
+ from ..media import ffmpeg
11
+ from . import analyze, generate, post, qa, script, segment
12
+
13
+ STEPS = [
14
+ ("analyze", "拆解参考片", 0.1),
15
+ ("gag", "抽梗", 0.2),
16
+ ("script", "改编脚本", 0.3),
17
+ ("generate", "生成原生对白视频", 0.6),
18
+ ("post", "后期", 0.85),
19
+ ("qa", "检查", 0.95),
20
+ ]
21
+
22
+
23
+ class Canceled(RuntimeError):
24
+ pass
25
+
26
+
27
+ def _set_step(run_id: str, name: str, status: str, progress: float, error: str | None = None) -> None:
28
+ run = store.read_run(run_id)
29
+ if run is None:
30
+ return
31
+ for step in run.steps:
32
+ if step.name == name:
33
+ step.status = status # type: ignore[assignment]
34
+ step.progress = progress
35
+ if status == "running":
36
+ step.started_at = store.now()
37
+ if status in {"done", "failed", "skipped"}:
38
+ step.finished_at = store.now()
39
+ if error:
40
+ step.error = error
41
+ break
42
+ run.step = name
43
+ run.progress = progress
44
+ store.write_run(run)
45
+
46
+
47
+ def _check_cancel(run_id: str) -> None:
48
+ run = store.read_run(run_id)
49
+ if run and run.cancel_requested:
50
+ raise Canceled("canceled by user")
51
+
52
+
53
+ def execute(run_id: str) -> None:
54
+ run = store.read_run(run_id)
55
+ if run is None:
56
+ return
57
+ run.status = "running"
58
+ run.error = None
59
+ run.steps = [RunStep(name=name, label=label) for name, label, _ in STEPS]
60
+ store.write_run(run)
61
+ try:
62
+ _check_cancel(run_id)
63
+ _set_step(run_id, "analyze", "running", 0.05)
64
+ skeleton = analyze.analyze(run_id)
65
+ _set_step(run_id, "analyze", "done", 0.1)
66
+ _check_cancel(run_id)
67
+
68
+ _set_step(run_id, "gag", "running", 0.15)
69
+ run = store.read_run(run_id)
70
+ gag = script.build_gag(run, skeleton)
71
+ store.atomic_write_json(store.run_dir(run_id) / "artifacts" / "gag.json", gag)
72
+ _set_step(run_id, "gag", "done", 0.2)
73
+ _check_cancel(run_id)
74
+
75
+ _set_step(run_id, "script", "running", 0.25)
76
+ run = store.read_run(run_id)
77
+ script_doc = script.build_script(run, skeleton, gag)
78
+ store.atomic_write_json(store.run_dir(run_id) / "artifacts" / "script.json", script_doc)
79
+ _set_step(run_id, "script", "done", 0.3)
80
+ _check_cancel(run_id)
81
+
82
+ _set_step(run_id, "generate", "running", 0.35)
83
+ generated = generate.generate(run_id)
84
+ run = store.read_run(run_id)
85
+ if run:
86
+ run.demo = bool(generated.get("demo", True))
87
+ store.write_run(run)
88
+ _set_step(run_id, "generate", "done", 0.6)
89
+ _check_cancel(run_id)
90
+
91
+ _set_step(run_id, "post", "running", 0.7)
92
+ result = post.post(run_id)
93
+ _set_step(run_id, "post", "done", 0.85)
94
+ _check_cancel(run_id)
95
+
96
+ segment.build_storyboard(run_id)
97
+ _set_step(run_id, "qa", "running", 0.9)
98
+ qa_report = qa.check(run_id)
99
+ _set_step(run_id, "qa", "done", 1.0)
100
+
101
+ run = store.read_run(run_id)
102
+ if run:
103
+ run.status = "succeeded"
104
+ run.progress = 1.0
105
+ run.finished_at = store.now()
106
+ run.demo = bool(generated.get("demo", True))
107
+ run.result = {**result, "qa": qa_report, "demo": bool(generated.get("demo", True)), "model": generated.get("model")}
108
+ store.write_run(run)
109
+ store.write_context({"last_run_id": run.id, "last_reference_asset_id": (run.inputs or {}).get("reference_asset_id"), "last_product_ids": (run.inputs or {}).get("product_asset_ids"), "last_params": run.advanced, "last_brief": (run.inputs or {}).get("brief")})
110
+ except Canceled:
111
+ run = store.read_run(run_id)
112
+ if run:
113
+ run.status = "canceled"
114
+ run.error = "已取消"
115
+ run.finished_at = store.now()
116
+ store.write_run(run)
117
+ except Exception as exc: # noqa: BLE001
118
+ run = store.read_run(run_id)
119
+ if run:
120
+ run.status = "failed"
121
+ run.error = f"{type(exc).__name__}: {exc}"[:800]
122
+ run.finished_at = store.now()
123
+ store.write_run(run)
124
+ traceback.print_exc()
125
+
126
+
127
+ class Manager:
128
+ def __init__(self, workers: int = 2) -> None:
129
+ self.pool = ThreadPoolExecutor(max_workers=workers, thread_name_prefix="adstudio-run")
130
+
131
+ def start(self, run_id: str) -> None:
132
+ self.pool.submit(execute, run_id)
133
+
134
+ def shutdown(self) -> None:
135
+ self.pool.shutdown(wait=False, cancel_futures=True)
136
+
137
+
138
+ manager = Manager()
@@ -0,0 +1,226 @@
1
+ """Deterministic demo script and gag artefacts.
2
+
3
+ The real LLM/VLM extraction lands after the provider bake-off. This module
4
+ keeps the run shape complete so the UI, storyboard, QA and edits can be built
5
+ and tested without API keys.
6
+ """
7
+ from __future__ import annotations
8
+
9
+ import re
10
+ from typing import Any
11
+
12
+ from .. import llm
13
+ from . import persona as persona_mod
14
+
15
+
16
+ def _text_model(run: Any) -> dict[str, Any] | None:
17
+ value = (run.advanced or {}).get("text_model")
18
+ return value if isinstance(value, dict) else None
19
+
20
+
21
+ def _fallback_gag(skeleton: dict[str, Any]) -> dict[str, Any]:
22
+ beats = skeleton.get("beats") or []
23
+ must_keep = []
24
+ if beats:
25
+ must_keep.append({"id": "m1", "text": "开头 2 秒内出现人物和商品", "beat": beats[0]["id"], "confidence": 0.6})
26
+ if len(beats) > 2:
27
+ must_keep.append({"id": "m2", "text": "中段出现价格/卖点信息", "beat": beats[len(beats) // 2]["id"], "confidence": 0.55})
28
+ if beats:
29
+ must_keep.append({"id": "m3", "text": "结尾有明确的行动或结果", "beat": beats[-1]["id"], "confidence": 0.55})
30
+ return {
31
+ "version": 2,
32
+ "mechanism": "demo_recreate",
33
+ "mechanism_label": "Demo:结构保真占位梗卡",
34
+ "template_type": "剧情对话",
35
+ "confidence": 0.55,
36
+ "must_keep": must_keep,
37
+ "timing": {"punchline_beat": beats[-1]["id"] if beats else None, "punchline_at_s": beats[-1]["end_s"] if beats else None},
38
+ "transferable": True,
39
+ "replicability": 0.6,
40
+ "risk_tags": ["demo 占位"],
41
+ "source": {"degraded": True, "reason": "demo pipeline"},
42
+ }
43
+
44
+
45
+ def build_gag(run: Any, skeleton: dict[str, Any]) -> dict[str, Any]:
46
+ """Ask the MiniMax text model for a structured gag; fall back locally."""
47
+ brief = str((run.inputs or {}).get("brief") or "").strip()
48
+ beats = skeleton.get("beats") or []
49
+ transcript = (skeleton.get("transcript") or [])[:24]
50
+ transcript_text = "\n".join(f"- {x.get('start_s')}-{x.get('end_s')}s {x.get('text')}" for x in transcript)[:1600]
51
+ summary = "\n".join(
52
+ f"- {b['id']} {b['start_s']:.1f}-{b['end_s']:.1f}s [{b.get('function')}] 画面:{((b.get('visual') or {}).get('action') or '')[:90]}"
53
+ for b in beats
54
+ )
55
+ prompt = f"""参考片结构:
56
+ {summary}
57
+
58
+ 参考片台词/ASR:
59
+ {transcript_text or '(无可用台词)'}
60
+
61
+ 用户/产品需求:
62
+ {brief or '无'}
63
+
64
+ {persona_mod.persona_instruction(run.advanced)}
65
+
66
+ 请判断这条参考片靠什么机制成立,并给出 3-6 条必须保留、可被第三人检查的具体要求。
67
+ 只输出 JSON:
68
+ {{
69
+ "mechanism": "price_compare_punchline|status_contrast|misunderstanding|exaggeration|repetition|snatch|callout|testimonial|unboxing|none",
70
+ "mechanism_label": "一句中文说明",
71
+ "must_keep": [{{"text": "具体要求", "beat": "b0", "confidence": 0.0}}],
72
+ "punchline_beat": "b0 或 null",
73
+ "confidence": 0.0,
74
+ "risk_tags": ["合规或执行风险"]
75
+ }}
76
+ must_keep 每条必须挂到上面真实存在的 beat;不要写空话。不要编造用户没有提供的原价、对比价、销量、功效或绝对化宣传。没有提供对比价时,只写活动价,不要自己补一个原价数字。"""
77
+ data = llm.chat_json("你是广告片结构分析师。只输出 JSON,不要解释。", prompt, max_tokens=2400, temperature=0.2, text_model=_text_model(run))
78
+ if not data:
79
+ return _fallback_gag(skeleton)
80
+ valid_beats = {b["id"] for b in beats}
81
+ keeps = []
82
+ for i, item in enumerate(data.get("must_keep") or []):
83
+ beat = str(item.get("beat") or "")
84
+ text = str(item.get("text") or "").strip()
85
+ if not text or beat not in valid_beats:
86
+ continue
87
+ keeps.append({"id": f"m{i+1}", "text": persona_mod.neutralize_people(text, run.advanced)[:120], "beat": beat, "confidence": float(item.get("confidence") or 0.6)})
88
+ if len(keeps) >= 6:
89
+ break
90
+ if not keeps:
91
+ fallback = _fallback_gag(skeleton)
92
+ fallback["mechanism"] = str(data.get("mechanism") or fallback["mechanism"])
93
+ fallback["mechanism_label"] = str(data.get("mechanism_label") or fallback["mechanism_label"])
94
+ fallback["confidence"] = float(data.get("confidence") or fallback["confidence"])
95
+ fallback["source"] = {"model": llm.model_label(), "degraded": True, "reason": "LLM must_keep invalid"}
96
+ return fallback
97
+ if len(keeps) < 3:
98
+ fallback = _fallback_gag(skeleton)
99
+ for item in fallback["must_keep"]:
100
+ if len(keeps) >= 3:
101
+ break
102
+ if all(item["beat"] != existing["beat"] for existing in keeps):
103
+ keeps.append(item)
104
+ return {
105
+ "version": 2,
106
+ "mechanism": str(data.get("mechanism") or "none"),
107
+ "mechanism_label": str(data.get("mechanism_label") or ""),
108
+ "template_type": "剧情对话",
109
+ "confidence": float(data.get("confidence") or 0.6),
110
+ "must_keep": keeps[:6],
111
+ "timing": {"punchline_beat": data.get("punchline_beat"), "punchline_at_s": None},
112
+ "transferable": True,
113
+ "replicability": float(data.get("confidence") or 0.6),
114
+ "risk_tags": list(data.get("risk_tags") or []),
115
+ "source": {"model": llm.model_label(), "degraded": False},
116
+ }
117
+
118
+
119
+ def _spoken_lines(brief: str) -> list[str]:
120
+ """Turn a mixed brief into short, speakable lines for the demo pipeline.
121
+
122
+ The real LLM script adapter will replace this. Until then, never let the
123
+ model read the whole brief (including production instructions) as dialogue.
124
+ """
125
+ clauses = [part.strip(" ::,,;;。\n") for part in re.split(r"[。;;,,\n]", brief)]
126
+ keep = []
127
+ for clause in clauses:
128
+ if not clause or len(clause) < 2:
129
+ continue
130
+ # Production instructions are not dialogue.
131
+ if re.search(r"保留|不要|要求|改成|更生活化|结构|动作|镜头|台词|信息|修改|参考", clause):
132
+ continue
133
+ if len(clause) > 24:
134
+ clause = clause[:24]
135
+ keep.append(clause)
136
+ if not keep:
137
+ keep = ["这个产品现在活动价 19.9", "不油腻,追剧吃刚好", "真的可以试试"]
138
+ return keep[:3]
139
+
140
+
141
+ def _fallback_script(run: Any, skeleton: dict[str, Any], gag: dict[str, Any]) -> dict[str, Any]:
142
+ brief = str((run.inputs or {}).get("brief") or "").strip()
143
+ spoken = _spoken_lines(brief)
144
+ beats = skeleton.get("beats") or []
145
+ rows = []
146
+ for i, beat in enumerate(beats):
147
+ texts = spoken[:3] if len(beats) == 1 else [spoken[min(i, len(spoken) - 1)]] if spoken else ["这个产品现在活动价 19.9"]
148
+ lines = []
149
+ for j, text in enumerate(texts[:3]):
150
+ speaker = "A" if j % 2 == 0 else "B"
151
+ lines.append({"speaker": speaker, "text": text[:24], "chars": len(text[:24])})
152
+ rows.append({
153
+ "id": beat["id"], "function": beat.get("function"), "len_s": beat.get("len_s"),
154
+ "cps": 4.0, "max_chars": max(12, int(float(beat.get("len_s") or 2) * 4.0)),
155
+ "chars": sum(line["chars"] for line in lines), "fit": "ok", "lines": lines,
156
+ "shot": (persona_mod.persona_label(run.advanced) + "在中景里自然交流,商品可见") if persona_mod.persona_label(run.advanced) else "人物在中景里自然交流,商品可见", "start_s": beat.get("start_s"), "end_s": beat.get("end_s"),
157
+ })
158
+ return {"version": 2, "beats": rows, "totals": {"duration_s": (skeleton.get("source") or {}).get("duration_s"), "chars": sum(r["chars"] for r in rows)}, "must_keep_map": {i["id"]: i.get("beat") for i in gag.get("must_keep") or []}, "compliance_flags": ["demo 文案,价格/功效需人工核对"], "source": {"degraded": True, "reason": "demo pipeline"}}
159
+
160
+
161
+ def build_script(run: Any, skeleton: dict[str, Any], gag: dict[str, Any]) -> dict[str, Any]:
162
+ brief = str((run.inputs or {}).get("brief") or "").strip()
163
+ advanced = run.advanced or {}
164
+ beats = skeleton.get("beats") or []
165
+ target = float(advanced.get("duration") or (skeleton.get("source") or {}).get("duration_s") or 8)
166
+ transcript = (skeleton.get("transcript") or [])[:24]
167
+ transcript_text = "\n".join(f"- {x.get('start_s')}-{x.get('end_s')}s {x.get('text')}" for x in transcript)[:1600]
168
+ beat_text = "\n".join(
169
+ f"- {b['id']} {b['start_s']:.1f}-{b['end_s']:.1f}s [{b.get('function')}] 画面:{((b.get('visual') or {}).get('action') or '')[:90]}"
170
+ for b in beats
171
+ )
172
+ keeps = "\n".join(f"- [{k['beat']}] {k['text']}" for k in gag.get("must_keep") or [])
173
+ prompt = f"""目标时长:{target:g}s;参考片 beat:
174
+ {beat_text}
175
+
176
+ 参考片台词/ASR:
177
+ {transcript_text or '(无可用台词)'}
178
+
179
+ 必须保留:
180
+ {keeps}
181
+
182
+ 产品/需求:
183
+ {brief or '产品展示,突出卖点,最后引导购买'}
184
+
185
+ {persona_mod.persona_instruction(run.advanced)}
186
+
187
+ 写一段可直接交给原生对白视频模型的中文脚本。要求:
188
+ - 只输出 JSON;
189
+ - 每个 beat 给出 1-3 行短台词,每行不超过 18 个汉字;
190
+ - 台词自然、像人说话,不要把产品说明/制作要求整段念出来;
191
+ - 保留用户给出的价格数字和 must_keep 的动作/信息;
192
+ - 不要编造未提供的原价、对比价、销量、功效或绝对化宣传;没有对比价就只写活动价;
193
+ - 尽量用 A/B 两个说话人,除非明显是单人场景;
194
+ - 如果存在模特设定,参考片人物只用于动作/构图,action 不得沿用原片性别/年龄/外貌;
195
+ - 每个 beat 给一个画面 action。
196
+ JSON:
197
+ {{"beats":[{{"id":"b0","lines":[{{"speaker":"A","text":"..."}}],"shot":"..."}}]}}"""
198
+ data = llm.chat_json("你是广告片编剧。只输出 JSON,不要解释。", prompt, max_tokens=3600, temperature=0.45, text_model=_text_model(run))
199
+ if not data or not isinstance(data.get("beats"), list):
200
+ fallback = _fallback_script(run, skeleton, gag)
201
+ fallback["source"] = {"degraded": True, "reason": "LLM script failed: " + (llm.LAST_ERROR or "empty response")}
202
+ return fallback
203
+ by_id = {str(item.get("id")): item for item in data["beats"] if isinstance(item, dict)}
204
+ rows = []
205
+ for beat in beats:
206
+ item = by_id.get(beat["id"]) or {}
207
+ lines = []
208
+ for j, line in enumerate((item.get("lines") or [])[:3]):
209
+ text = str((line or {}).get("text") or "").strip()
210
+ if not text:
211
+ continue
212
+ text = text[:24]
213
+ speaker = str((line or {}).get("speaker") or ("A" if j % 2 == 0 else "B"))
214
+ lines.append({"speaker": speaker if speaker in {"A", "B"} else "A", "text": text, "chars": len(text)})
215
+ if not lines:
216
+ fallback = _fallback_script(run, skeleton, gag)
217
+ fallback_row = next((r for r in fallback["beats"] if r["id"] == beat["id"]), None)
218
+ lines = fallback_row["lines"] if fallback_row else [{"speaker": "A", "text": "这个产品现在活动价 19.9", "chars": 13}]
219
+ rows.append({
220
+ "id": beat["id"], "function": beat.get("function"), "len_s": beat.get("len_s"),
221
+ "cps": 4.0, "max_chars": max(12, int(float(beat.get("len_s") or 2) * 4.0)),
222
+ "chars": sum(line["chars"] for line in lines), "fit": "ok", "lines": lines,
223
+ "shot": persona_mod.neutralize_people(str(item.get("shot") or "人物在中景里自然交流,商品可见"), run.advanced)[:120],
224
+ "start_s": beat.get("start_s"), "end_s": beat.get("end_s"),
225
+ })
226
+ return {"version": 2, "beats": rows, "totals": {"duration_s": target, "chars": sum(r["chars"] for r in rows)}, "must_keep_map": {i["id"]: i.get("beat") for i in gag.get("must_keep") or []}, "compliance_flags": ["LLM 文案,价格/功效需人工核对"], "source": {"model": llm.model_label(), "degraded": False}}
@@ -0,0 +1,87 @@
1
+ """Storyboard derivation from the final clip and the script."""
2
+ from __future__ import annotations
3
+
4
+ from typing import Any
5
+
6
+ from .. import params, store
7
+ from ..media import ffmpeg
8
+
9
+
10
+ def _segments_for(run: Any, skeleton: dict[str, Any], script: dict[str, Any], final_s: float) -> list[dict[str, Any]]:
11
+ advanced = params.merged_advanced(run.advanced)
12
+ target = float(advanced.get("duration") or final_s or 6)
13
+ source_s = float((skeleton.get("source") or {}).get("duration_s") or target)
14
+ scale = (target / source_s) if source_s > 0 else 1.0
15
+ script_beats = {row.get("id"): row for row in script.get("beats") or []}
16
+ beats = skeleton.get("beats") or []
17
+ rows: list[dict[str, Any]] = []
18
+ for i, beat in enumerate(beats):
19
+ start = float(beat.get("start_s") or 0) * scale
20
+ end = float(beat.get("end_s") or target) * scale
21
+ start = min(max(0.0, start), max(0.0, target - 0.3))
22
+ end = min(max(start + 0.3, end), target)
23
+ line = " / ".join(str(x.get("text") or "") for x in (script_beats.get(beat.get("id")) or {}).get("lines") or [])
24
+ rows.append({
25
+ "id": f"s{len(rows)}",
26
+ "index": len(rows),
27
+ "start_s": round(start, 3),
28
+ "end_s": round(end, 3),
29
+ "function": beat.get("function") or "beat",
30
+ "source": "generated" if run.generation_mode == "fine" else "one_shot_slice",
31
+ "thumbnail": None,
32
+ "dialogue": line,
33
+ "action": (script_beats.get(beat.get("id")) or {}).get("shot") or (beat.get("visual") or {}).get("action") or "",
34
+ "product": "",
35
+ "status": "ok",
36
+ "asset_id": None,
37
+ "take": 1,
38
+ "note": "demo analysis segment",
39
+ })
40
+ # Fine mode must expose the actual generation split even when the reference
41
+ # is shorter than the target. Keep two readable segments for 8s+ targets.
42
+ if run.generation_mode == "fine" and len(rows) < 2 and target >= 8:
43
+ rows = [{
44
+ "id": "s0", "index": 0, "start_s": 0.0, "end_s": round(target / 2, 3),
45
+ "function": "hook", "source": "generated", "thumbnail": None,
46
+ "dialogue": rows[0]["dialogue"] if rows else "", "action": rows[0]["action"] if rows else "",
47
+ "product": "", "status": "ok", "asset_id": None, "take": 1, "note": "demo fine segment",
48
+ }, {
49
+ "id": "s1", "index": 1, "start_s": round(target / 2, 3), "end_s": round(target, 3),
50
+ "function": "punchline", "source": "generated", "thumbnail": None,
51
+ "dialogue": "", "action": "产品展示/行动落点", "product": "",
52
+ "status": "ok", "asset_id": None, "take": 1, "note": "demo fine segment",
53
+ }]
54
+ if not rows:
55
+ rows = [{
56
+ "id": "s0", "index": 0, "start_s": 0.0, "end_s": round(target, 3), "function": "beat",
57
+ "source": "one_shot_slice", "thumbnail": None, "dialogue": "", "action": "",
58
+ "product": "", "status": "ok", "asset_id": None, "take": 1, "note": "demo",
59
+ }]
60
+ return rows
61
+
62
+
63
+ def build_storyboard(run_id: str) -> dict[str, Any]:
64
+ run = store.read_run(run_id)
65
+ if run is None:
66
+ raise FileNotFoundError(run_id)
67
+ skeleton = store.read_json(store.run_dir(run_id) / "artifacts" / "skeleton.json", {}) or {}
68
+ script = store.read_json(store.run_dir(run_id) / "artifacts" / "script.json", {}) or {}
69
+ final = store.run_artifact(run_id, "final.mp4")
70
+ final_s = 0.0
71
+ if final.is_file():
72
+ try:
73
+ final_s = float(ffmpeg.probe(final)["duration_s"])
74
+ except Exception:
75
+ pass
76
+ rows = _segments_for(run, skeleton, script, final_s)
77
+ for i, seg in enumerate(rows):
78
+ if final.is_file():
79
+ thumb = store.run_artifact(run_id, f"storyboard/seg_{i}.jpg")
80
+ try:
81
+ ffmpeg.extract_frame(final, (float(seg["start_s"]) + float(seg["end_s"])) / 2, thumb, width=360)
82
+ seg["thumbnail"] = store.rel_to_data(thumb)
83
+ except Exception:
84
+ pass
85
+ payload = {"version": 1, "generation_mode": run.generation_mode, "segments": rows}
86
+ store.atomic_write_json(store.run_dir(run_id) / "artifacts" / "storyboard.json", payload)
87
+ return payload
File without changes