@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,294 @@
1
+ """Small ffmpeg wrapper used by the lean pipeline."""
2
+ from __future__ import annotations
3
+
4
+ import json
5
+ import re
6
+ import shutil
7
+ import subprocess
8
+ import sys
9
+ from pathlib import Path
10
+ from typing import Any
11
+
12
+
13
+ def _resolve(name: str) -> str:
14
+ """Absolute path from PATH, else the bare name so exec fails loudly and late."""
15
+ return shutil.which(name) or name
16
+
17
+
18
+ FFMPEG = _resolve("ffmpeg")
19
+ FFPROBE = _resolve("ffprobe")
20
+
21
+
22
+ def install_hint() -> str:
23
+ """One platform-appropriate sentence for a missing ffmpeg."""
24
+ if sys.platform == "darwin":
25
+ return "未找到 ffmpeg。安装:brew install ffmpeg(或用 https://evermeet.cx/ffmpeg/ 的静态包)"
26
+ if sys.platform == "win32":
27
+ return "未找到 ffmpeg。安装:winget install Gyan.FFmpeg,或从 https://www.gyan.dev/ffmpeg/builds/ 解压后把 bin 加入 PATH"
28
+ return "未找到 ffmpeg。安装:apt install ffmpeg(Debian/Ubuntu)或 dnf install ffmpeg(Fedora)"
29
+
30
+
31
+ def dependencies() -> dict[str, Any]:
32
+ """What the UI needs to explain a missing media toolchain."""
33
+ ffmpeg_path = shutil.which("ffmpeg")
34
+ ffprobe_path = shutil.which("ffprobe")
35
+ ok = bool(ffmpeg_path and ffprobe_path)
36
+ return {
37
+ "ffmpeg": bool(ffmpeg_path),
38
+ "ffprobe": bool(ffprobe_path),
39
+ "ffmpeg_path": ffmpeg_path or "",
40
+ "ok": ok,
41
+ "hint": "" if ok else install_hint(),
42
+ }
43
+
44
+
45
+ def run(args: list[str], timeout: int = 600) -> subprocess.CompletedProcess:
46
+ """Run a media command, turning a missing binary into an actionable error."""
47
+ try:
48
+ return subprocess.run(args, capture_output=True, text=True, timeout=timeout)
49
+ except FileNotFoundError as exc:
50
+ raise RuntimeError(f"{Path(str(args[0])).name} 不可用:{install_hint()}") from exc
51
+
52
+
53
+ def probe(path: str | Path) -> dict[str, Any]:
54
+ target = Path(path)
55
+ if not target.is_file():
56
+ raise FileNotFoundError(str(target))
57
+ proc = run([FFPROBE, "-v", "error", "-print_format", "json", "-show_format", "-show_streams", str(target)], timeout=60)
58
+ if proc.returncode != 0:
59
+ raise ValueError(f"ffprobe failed: {proc.stderr[:300]}")
60
+ data = json.loads(proc.stdout or "{}")
61
+ streams = data.get("streams") or []
62
+ video = next((s for s in streams if s.get("codec_type") == "video"), None)
63
+ audio = next((s for s in streams if s.get("codec_type") == "audio"), None)
64
+ if not video:
65
+ raise ValueError("no video stream")
66
+ width, height = int(video.get("width") or 0), int(video.get("height") or 0)
67
+ duration = float((data.get("format") or {}).get("duration") or video.get("duration") or 0.0)
68
+ fps = _fraction(video.get("avg_frame_rate") or video.get("r_frame_rate"))
69
+ return {
70
+ "path": str(target),
71
+ "duration_s": round(duration, 3),
72
+ "width": width,
73
+ "height": height,
74
+ "fps": round(fps, 3),
75
+ "aspect": _aspect(width, height),
76
+ "video_codec": video.get("codec_name"),
77
+ "audio_codec": (audio or {}).get("codec_name"),
78
+ "has_audio": audio is not None,
79
+ "size_bytes": int((data.get("format") or {}).get("size") or target.stat().st_size),
80
+ }
81
+
82
+
83
+ def _fraction(value: Any) -> float:
84
+ try:
85
+ num, den = str(value or "0/1").split("/", 1)
86
+ return float(num) / float(den or 1)
87
+ except (ValueError, ZeroDivisionError):
88
+ return 0.0
89
+
90
+
91
+ def _aspect(width: int, height: int) -> str:
92
+ if height > width * 1.15:
93
+ return "9:16"
94
+ if width > height * 1.15:
95
+ return "16:9"
96
+ return "1:1"
97
+
98
+
99
+ def detect_cuts(path: str | Path, duration: float, threshold: float = 0.18) -> list[float]:
100
+ proc = run(
101
+ [FFMPEG, "-hide_banner", "-i", str(path), "-vf", f"select='gt(scene,{threshold})',showinfo", "-f", "null", "-"],
102
+ timeout=300,
103
+ )
104
+ times = sorted({round(float(x), 3) for x in re.findall(r"pts_time:([0-9.]+)", proc.stderr or "")})
105
+ merged: list[float] = []
106
+ for value in times:
107
+ if 0.2 < value < duration - 0.2 and (not merged or value - merged[-1] >= 0.35):
108
+ merged.append(value)
109
+ return merged
110
+
111
+
112
+ def extract_frame(source: str | Path, at_s: float, target: str | Path, width: int = 360) -> Path:
113
+ out = Path(target)
114
+ out.parent.mkdir(parents=True, exist_ok=True)
115
+ proc = run(
116
+ [FFMPEG, "-v", "error", "-y", "-ss", f"{max(0.0, at_s):.3f}", "-i", str(source), "-frames:v", "1",
117
+ "-vf", f"scale={max(80, width)}:-2", "-q:v", "3", str(out)],
118
+ timeout=60,
119
+ )
120
+ if proc.returncode != 0 or not out.is_file():
121
+ raise ValueError(f"frame extraction failed: {proc.stderr[:200]}")
122
+ return out
123
+
124
+
125
+ def make_demo_clip(
126
+ source: str | Path | None,
127
+ target: str | Path,
128
+ *,
129
+ duration: float,
130
+ width: int,
131
+ height: int,
132
+ fps: int = 30,
133
+ ) -> Path:
134
+ """Create a deterministic demo clip.
135
+
136
+ With a reference source, trim/loop it to the requested duration and frame.
137
+ Without a source, render a test pattern so text-to-video can be exercised.
138
+ The clip is explicitly a demo; it is not a paid provider result.
139
+ """
140
+ out = Path(target)
141
+ out.parent.mkdir(parents=True, exist_ok=True)
142
+ duration = max(1.0, float(duration))
143
+ vf = (
144
+ f"scale={width}:{height}:force_original_aspect_ratio=decrease,"
145
+ f"pad={width}:{height}:(ow-iw)/2:(oh-ih)/2:color=0x111827,"
146
+ f"fps={fps},format=yuv420p"
147
+ )
148
+ if source and Path(source).is_file():
149
+ facts = probe(source)
150
+ if facts.get("has_audio"):
151
+ cmd = [
152
+ FFMPEG, "-v", "error", "-y", "-stream_loop", "-1", "-i", str(source),
153
+ "-t", f"{duration:.3f}", "-vf", vf,
154
+ "-af", "apad,atrim=0:%s,asetpts=N/SR/TB" % f"{duration:.3f}",
155
+ "-c:v", "libx264", "-preset", "veryfast", "-crf", "23",
156
+ "-c:a", "aac", "-ar", "48000", "-ac", "2", "-shortest",
157
+ "-movflags", "+faststart", str(out),
158
+ ]
159
+ else:
160
+ cmd = [
161
+ FFMPEG, "-v", "error", "-y", "-stream_loop", "-1", "-i", str(source),
162
+ "-f", "lavfi", "-i", "anullsrc=r=48000:cl=stereo",
163
+ "-t", f"{duration:.3f}", "-vf", vf,
164
+ "-map", "0:v:0", "-map", "1:a:0",
165
+ "-c:v", "libx264", "-preset", "veryfast", "-crf", "23",
166
+ "-c:a", "aac", "-ar", "48000", "-ac", "2", "-shortest",
167
+ "-movflags", "+faststart", str(out),
168
+ ]
169
+ else:
170
+ cmd = [
171
+ FFMPEG, "-v", "error", "-y",
172
+ "-f", "lavfi", "-i", f"testsrc2=size={width}x{height}:rate={fps}",
173
+ "-f", "lavfi", "-i", "anullsrc=r=48000:cl=stereo",
174
+ "-t", f"{duration:.3f}", "-vf", "format=yuv420p",
175
+ "-map", "0:v:0", "-map", "1:a:0",
176
+ "-c:v", "libx264", "-preset", "veryfast", "-crf", "23",
177
+ "-c:a", "aac", "-ar", "48000", "-ac", "2", "-shortest",
178
+ "-movflags", "+faststart", str(out),
179
+ ]
180
+ proc = run(cmd, timeout=300)
181
+ if proc.returncode != 0 or not out.is_file():
182
+ raise RuntimeError(f"demo clip failed: {proc.stderr[-500:]}")
183
+ return out
184
+
185
+
186
+ def ensure_audio(source: str | Path, target: str | Path) -> Path:
187
+ facts = probe(source)
188
+ if facts.get("has_audio"):
189
+ if Path(target) != Path(source):
190
+ shutil.copy2(source, target)
191
+ return Path(target)
192
+ proc = run(
193
+ [FFMPEG, "-v", "error", "-y", "-i", str(source), "-f", "lavfi", "-i", "anullsrc=r=48000:cl=stereo",
194
+ "-map", "0:v:0", "-map", "1:a:0", "-shortest", "-c:v", "copy", "-c:a", "aac", "-ar", "48000", "-ac", "2", str(target)],
195
+ timeout=300,
196
+ )
197
+ if proc.returncode != 0:
198
+ raise RuntimeError(f"add audio failed: {proc.stderr[-300:]}")
199
+ return Path(target)
200
+
201
+
202
+ def normalize(source: str | Path, target: str | Path, *, width: int, height: int, fps: int = 30, duration: float | None = None) -> Path:
203
+ out = Path(target)
204
+ out.parent.mkdir(parents=True, exist_ok=True)
205
+ vf = (
206
+ f"scale={width}:{height}:force_original_aspect_ratio=decrease,"
207
+ f"pad={width}:{height}:(ow-iw)/2:(oh-ih)/2:color=0x111827,"
208
+ f"fps={fps},format=yuv420p"
209
+ )
210
+ args = [FFMPEG, "-v", "error", "-y", "-i", str(source)]
211
+ if duration:
212
+ args += ["-t", f"{float(duration):.3f}"]
213
+ args += ["-vf", vf, "-af", "loudnorm=I=-16:TP=-1.5:LRA=11", "-c:v", "libx264", "-preset", "veryfast", "-crf", "23",
214
+ "-c:a", "aac", "-ar", "48000", "-ac", "2", "-movflags", "+faststart", str(out)]
215
+ proc = run(args, timeout=300)
216
+ if proc.returncode != 0 or not out.is_file():
217
+ # Audio filter may fail on a silent/odd track; retry with a silent bed.
218
+ ensure_audio(source, out.with_suffix(".audio.mp4"))
219
+ proc = run([FFMPEG, "-v", "error", "-y", "-i", str(out.with_suffix(".audio.mp4")), "-vf", vf,
220
+ "-c:v", "libx264", "-preset", "veryfast", "-crf", "23", "-c:a", "aac", "-ar", "48000", "-ac", "2",
221
+ "-movflags", "+faststart", str(out)], timeout=300)
222
+ if proc.returncode != 0 or not out.is_file():
223
+ raise RuntimeError(f"normalize failed: {proc.stderr[-500:]}")
224
+ return out
225
+
226
+
227
+
228
+ def loop_to_duration(source: str | Path, target: str | Path, *, width: int, height: int, fps: int, duration: float) -> Path:
229
+ """Normalize/trim/loop a clip to an exact duration.
230
+
231
+ Used by the per-segment replacement path: provider clips may be shorter or
232
+ longer than the storyboard slot. Looping is preferable to a gap; the slot is
233
+ then cut at the exact storyboard boundary.
234
+ """
235
+ out = Path(target)
236
+ out.parent.mkdir(parents=True, exist_ok=True)
237
+ fixed = out.with_suffix(".withaudio.mp4")
238
+ ensure_audio(source, fixed)
239
+ vf = (
240
+ f"scale={width}:{height}:force_original_aspect_ratio=decrease,"
241
+ f"pad={width}:{height}:(ow-iw)/2:(oh-ih)/2:color=0x111827,"
242
+ f"fps={fps},format=yuv420p"
243
+ )
244
+ proc = run([
245
+ FFMPEG, "-v", "error", "-y", "-stream_loop", "-1", "-i", str(fixed),
246
+ "-t", f"{float(duration):.3f}", "-vf", vf,
247
+ "-af", "apad", "-c:v", "libx264", "-preset", "veryfast", "-crf", "23",
248
+ "-c:a", "aac", "-ar", "48000", "-ac", "2", "-movflags", "+faststart", str(out),
249
+ ], timeout=300)
250
+ try:
251
+ fixed.unlink()
252
+ except OSError:
253
+ pass
254
+ if proc.returncode != 0 or not out.is_file():
255
+ raise RuntimeError(f"loop_to_duration failed: {proc.stderr[-500:]}")
256
+ return out
257
+
258
+ def thumbnail(source: str | Path, target: str | Path, at_s: float = 0.5) -> Path:
259
+ return extract_frame(source, at_s, target, width=360)
260
+
261
+
262
+ def transcode(
263
+ source: str | Path,
264
+ target: str | Path,
265
+ *,
266
+ bitrate: str = "8M",
267
+ fps: int = 30,
268
+ trim_start: float | None = None,
269
+ trim_end: float | None = None,
270
+ ) -> Path:
271
+ """Post-only export: re-encode bitrate/fps and optionally trim.
272
+
273
+ No video model is called. Subtitle burn-in is intentionally not attempted:
274
+ the local ffmpeg build has no libass/drawtext, so the SRT stays a sidecar.
275
+ """
276
+ out = Path(target)
277
+ out.parent.mkdir(parents=True, exist_ok=True)
278
+ args = [FFMPEG, "-v", "error", "-y"]
279
+ if trim_start:
280
+ args += ["-ss", f"{float(trim_start):.3f}"]
281
+ args += ["-i", str(source)]
282
+ if trim_end:
283
+ args += ["-to", f"{float(trim_end):.3f}"]
284
+ args += [
285
+ "-vf", f"fps={int(fps)},format=yuv420p",
286
+ "-c:v", "libx264", "-preset", "veryfast", "-b:v", bitrate,
287
+ "-maxrate", bitrate, "-bufsize", bitrate,
288
+ "-c:a", "aac", "-ar", "48000", "-ac", "2",
289
+ "-movflags", "+faststart", str(out),
290
+ ]
291
+ proc = run(args, timeout=300)
292
+ if proc.returncode != 0 or not out.is_file():
293
+ raise RuntimeError(f"transcode failed: {proc.stderr[-500:]}")
294
+ return out
@@ -0,0 +1,124 @@
1
+ """FFmpeg stitching: normalize clips, concat, optional insert plan.
2
+
3
+ The first slice supports hard cuts, audio keep/mute, and black/crop/blur fit.
4
+ Crossfade is reported as unsupported rather than silently ignored.
5
+ """
6
+ from __future__ import annotations
7
+
8
+ import tempfile
9
+ from pathlib import Path
10
+ from typing import Any
11
+
12
+ from . import ffmpeg
13
+
14
+
15
+ def _fit_filter(index: int, width: int, height: int, fit: str) -> tuple[str, str]:
16
+ """Return (filter start, output label) for one video stream."""
17
+ src = f"[{index}:v]"
18
+ if fit == "crop":
19
+ return (
20
+ f"{src}trim=start={{start}}:end={{end}},setpts=PTS-STARTPTS,"
21
+ f"scale={width}:{height}:force_original_aspect_ratio=increase,crop={width}:{height},"
22
+ f"fps=30,format=yuv420p",
23
+ f"[v{index}]",
24
+ )
25
+ if fit == "blur_pad":
26
+ return (
27
+ f"{src}trim=start={{start}}:end={{end}},setpts=PTS-STARTPTS,split=2[bg{index}][fg{index}];"
28
+ f"[bg{index}]scale={width}:{height}:force_original_aspect_ratio=increase,crop={width}:{height},boxblur=20:2[bgf{index}];"
29
+ f"[fg{index}]scale={width}:{height}:force_original_aspect_ratio=decrease[fgf{index}];"
30
+ f"[bgf{index}][fgf{index}]overlay=(W-w)/2:(H-h)/2,fps=30,format=yuv420p",
31
+ f"[v{index}]",
32
+ )
33
+ return (
34
+ f"{src}trim=start={{start}}:end={{end}},setpts=PTS-STARTPTS,"
35
+ f"scale={width}:{height}:force_original_aspect_ratio=decrease,"
36
+ f"pad={width}:{height}:(ow-iw)/2:(oh-ih)/2:color=0x111827,fps=30,format=yuv420p",
37
+ f"[v{index}]",
38
+ )
39
+
40
+
41
+ def plan(clips: list[dict[str, Any]], *, width: int, height: int, fit: str = "blur_pad", transition: str = "cut", audio: str = "keep") -> dict[str, Any]:
42
+ rows = []
43
+ total = 0.0
44
+ warnings: list[str] = []
45
+ for i, clip in enumerate(clips):
46
+ facts = ffmpeg.probe(clip["path"])
47
+ start = max(0.0, float(clip.get("in_s") or 0.0))
48
+ end = float(clip.get("out_s") or facts["duration_s"])
49
+ end = min(end, facts["duration_s"])
50
+ if end <= start:
51
+ raise ValueError(f"clip {i} has no duration")
52
+ length = end - start
53
+ total += length
54
+ rows.append({"index": i, "path": clip["path"], "in_s": start, "out_s": end, "len_s": round(length, 3), "has_audio": facts.get("has_audio")})
55
+ if transition not in {"cut"}:
56
+ warnings.append(f"转场 {transition} 暂未实现,已按硬切输出")
57
+ if audio not in {"keep", "mute"}:
58
+ warnings.append(f"音频策略 {audio} 暂未实现,已按 keep 输出")
59
+ return {"clips": rows, "duration_s": round(total, 3), "width": width, "height": height, "fit": fit, "transition": "cut", "audio": audio if audio in {"keep", "mute"} else "keep", "warnings": warnings}
60
+
61
+
62
+ def execute(plan_data: dict[str, Any], target: str | Path) -> Path:
63
+ out = Path(target)
64
+ out.parent.mkdir(parents=True, exist_ok=True)
65
+ clips = plan_data["clips"]
66
+ width, height = int(plan_data["width"]), int(plan_data["height"])
67
+ fit = plan_data.get("fit") or "black_pad"
68
+ audio_mode = plan_data.get("audio") or "keep"
69
+ if not clips:
70
+ raise ValueError("no clips")
71
+ tmpdir = Path(tempfile.mkdtemp(prefix="adstudio-stitch-"))
72
+ inputs: list[str] = []
73
+ with_audio: list[Path] = []
74
+ try:
75
+ for i, clip in enumerate(clips):
76
+ src = Path(clip["path"])
77
+ if clip.get("has_audio"):
78
+ with_audio.append(src)
79
+ else:
80
+ fixed = tmpdir / f"clip_{i}.mp4"
81
+ ffmpeg.ensure_audio(src, fixed)
82
+ with_audio.append(fixed)
83
+ inputs.extend(["-i", str(with_audio[-1])])
84
+
85
+ filters: list[str] = []
86
+ labels: list[str] = []
87
+ for i, clip in enumerate(clips):
88
+ start, end = float(clip["in_s"]), float(clip["out_s"])
89
+ vfilter, vlabel = _fit_filter(i, width, height, fit)
90
+ vfilter = vfilter.format(start=f"{start:.3f}", end=f"{end:.3f}")
91
+ filters.append(f"{vfilter}{vlabel}")
92
+ if audio_mode == "keep":
93
+ filters.append(
94
+ f"[{i}:a]atrim=start={start:.3f}:end={end:.3f},asetpts=PTS-STARTPTS,"
95
+ f"aresample=48000,aformat=sample_fmts=fltp:channel_layouts=stereo[a{i}]"
96
+ )
97
+ labels.append(f"[v{i}][a{i}]")
98
+ else:
99
+ labels.append(f"[v{i}]")
100
+ if audio_mode == "keep":
101
+ filters.append("".join(labels) + f"concat=n={len(clips)}:v=1:a=1[outv][outa]")
102
+ map_args = ["-map", "[outv]", "-map", "[outa]"]
103
+ else:
104
+ filters.append("".join(labels) + f"concat=n={len(clips)}:v=1:a=0[outv]")
105
+ map_args = ["-map", "[outv]", "-an"]
106
+ cmd = [ffmpeg.FFMPEG, "-v", "error", "-y", *inputs, "-filter_complex", ";".join(filters), *map_args,
107
+ "-c:v", "libx264", "-preset", "veryfast", "-crf", "22", "-movflags", "+faststart"]
108
+ if audio_mode == "keep":
109
+ cmd += ["-c:a", "aac", "-ar", "48000", "-ac", "2"]
110
+ cmd.append(str(out))
111
+ proc = ffmpeg.run(cmd, timeout=600)
112
+ if proc.returncode != 0 or not out.is_file():
113
+ raise RuntimeError(f"stitch failed: {proc.stderr[-800:]}")
114
+ return out
115
+ finally:
116
+ for path in tmpdir.glob("*"):
117
+ try:
118
+ path.unlink()
119
+ except OSError:
120
+ pass
121
+ try:
122
+ tmpdir.rmdir()
123
+ except OSError:
124
+ pass
@@ -0,0 +1,45 @@
1
+ """SRT generation from the Ad+ Studio 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 {}