@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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +28 -2
- package/client.js +2256 -0
- package/cordis.patch.yml +10 -0
- package/engine/requirements.txt +9 -0
- package/engine/src/adclip/__init__.py +0 -0
- package/engine/src/adclip/api.py +704 -0
- package/engine/src/adclip/catalog.py +341 -0
- package/engine/src/adclip/image_params.py +45 -0
- package/engine/src/adclip/intents.py +209 -0
- package/engine/src/adclip/llm.py +142 -0
- package/engine/src/adclip/media/__init__.py +0 -0
- package/engine/src/adclip/media/asr.py +79 -0
- package/engine/src/adclip/media/ffmpeg.py +294 -0
- package/engine/src/adclip/media/stitch.py +124 -0
- package/engine/src/adclip/media/subtitles.py +45 -0
- package/engine/src/adclip/media/vision.py +105 -0
- package/engine/src/adclip/models.py +127 -0
- package/engine/src/adclip/params.py +158 -0
- package/engine/src/adclip/personas.py +60 -0
- package/engine/src/adclip/pipeline/__init__.py +0 -0
- package/engine/src/adclip/pipeline/analyze.py +133 -0
- package/engine/src/adclip/pipeline/generate.py +196 -0
- package/engine/src/adclip/pipeline/image_runner.py +146 -0
- package/engine/src/adclip/pipeline/persona.py +56 -0
- package/engine/src/adclip/pipeline/post.py +40 -0
- package/engine/src/adclip/pipeline/qa.py +44 -0
- package/engine/src/adclip/pipeline/replace.py +191 -0
- package/engine/src/adclip/pipeline/runner.py +138 -0
- package/engine/src/adclip/pipeline/script.py +226 -0
- package/engine/src/adclip/pipeline/segment.py +87 -0
- package/engine/src/adclip/providers/__init__.py +0 -0
- package/engine/src/adclip/providers/ark.py +122 -0
- package/engine/src/adclip/providers/ark_image.py +114 -0
- package/engine/src/adclip/providers/base.py +30 -0
- package/engine/src/adclip/providers/discovery.py +56 -0
- package/engine/src/adclip/providers/kling.py +101 -0
- package/engine/src/adclip/providers/minimax.py +155 -0
- package/engine/src/adclip/providers/minimax_image.py +89 -0
- package/engine/src/adclip/settings.py +109 -0
- package/engine/src/adclip/store.py +378 -0
- package/host/dsh.js +60 -0
- package/host/engine.js +265 -0
- package/host/proxy.js +142 -0
- package/host/supervisor.js +251 -0
- package/icon.svg +6 -0
- package/index.js +39 -0
- package/locale/en.json +12 -0
- package/locale/zh.json +12 -0
- package/package.json +47 -4
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"""MiniMax text helper for structured gag/script generation.
|
|
2
|
+
|
|
3
|
+
This reuses the same MiniMax key as the video adapter; it is not a second
|
|
4
|
+
provider configuration. When the key is absent or the call fails, callers fall
|
|
5
|
+
back to deterministic demo content rather than blocking generation.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
import os
|
|
11
|
+
import re
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
import httpx
|
|
15
|
+
|
|
16
|
+
BASE = os.environ.get("MINIMAX_API_BASE", "https://api.minimaxi.com").rstrip("/")
|
|
17
|
+
MODEL = os.environ.get("ADSTUDIO_TEXT_MODEL", "MiniMax-M3")
|
|
18
|
+
DSH_URL = (os.environ.get("ADSTUDIO_DSH_CHAT_URL") or (os.environ.get("DSH_WEB_URL", "http://127.0.0.1:63215").rstrip("/") + "/adplus/api/dsh/chat")).strip()
|
|
19
|
+
TEXT_PROVIDER = os.environ.get("ADSTUDIO_TEXT_PROVIDER", "auto").strip().lower()
|
|
20
|
+
LAST_ERROR = ""
|
|
21
|
+
LAST_MODEL = ""
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def available() -> bool:
|
|
25
|
+
if TEXT_PROVIDER in {"dsh", "auto"} and DSH_URL:
|
|
26
|
+
return True
|
|
27
|
+
return bool(os.environ.get("MINIMAX_API_KEY", "").strip())
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def model_label() -> str:
|
|
31
|
+
return LAST_MODEL or MODEL
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def provider_label() -> str:
|
|
35
|
+
if TEXT_PROVIDER in {"dsh", "auto"} and DSH_URL:
|
|
36
|
+
return "dsh"
|
|
37
|
+
if os.environ.get("MINIMAX_API_KEY", "").strip():
|
|
38
|
+
return "minimax"
|
|
39
|
+
return "none"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _normalize_text_model(value: Any) -> dict[str, str] | None:
|
|
43
|
+
if not isinstance(value, dict):
|
|
44
|
+
return None
|
|
45
|
+
provider = str(value.get("provider") or "").strip()
|
|
46
|
+
model = str(value.get("model") or "").strip()
|
|
47
|
+
if not provider or not model:
|
|
48
|
+
return None
|
|
49
|
+
return {"provider": provider, "model": model}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _strip_reasoning(text: str) -> str:
|
|
53
|
+
text = re.sub(r"<think>.*?</think>", "", text or "", flags=re.S)
|
|
54
|
+
return text.strip()
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _chat_dsh(system: str, user: str, *, max_tokens: int, temperature: float, text_model: dict[str, str] | None = None) -> str:
|
|
58
|
+
global LAST_ERROR, LAST_MODEL
|
|
59
|
+
payload = {
|
|
60
|
+
"system": system,
|
|
61
|
+
"messages": [{"role": "user", "content": [{"type": "text", "text": user}]}],
|
|
62
|
+
"maxTokens": max_tokens,
|
|
63
|
+
}
|
|
64
|
+
if text_model:
|
|
65
|
+
payload["provider"] = text_model["provider"]
|
|
66
|
+
payload["model"] = text_model["model"]
|
|
67
|
+
with httpx.Client(timeout=httpx.Timeout(45.0, connect=8.0)) as client:
|
|
68
|
+
response = client.post(DSH_URL, json=payload)
|
|
69
|
+
if response.status_code >= 400:
|
|
70
|
+
raise RuntimeError(f"DSH bridge HTTP {response.status_code}: {response.text[:300]}")
|
|
71
|
+
data = response.json()
|
|
72
|
+
dsh = data.get("dsh") or {}
|
|
73
|
+
text = _strip_reasoning(str(dsh.get("text") or ""))
|
|
74
|
+
if not text:
|
|
75
|
+
raise RuntimeError("DSH bridge returned empty text")
|
|
76
|
+
LAST_MODEL = str(dsh.get("model") or "dsh")
|
|
77
|
+
return text
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _chat_minimax(system: str, user: str, *, max_tokens: int, temperature: float) -> str:
|
|
81
|
+
global LAST_ERROR, LAST_MODEL
|
|
82
|
+
key = os.environ.get("MINIMAX_API_KEY", "").strip()
|
|
83
|
+
if not key:
|
|
84
|
+
raise RuntimeError("MINIMAX_API_KEY 未配置")
|
|
85
|
+
body = {
|
|
86
|
+
"model": MODEL,
|
|
87
|
+
"messages": [
|
|
88
|
+
{"role": "system", "content": system},
|
|
89
|
+
{"role": "user", "content": user},
|
|
90
|
+
],
|
|
91
|
+
"max_tokens": max_tokens,
|
|
92
|
+
"temperature": temperature,
|
|
93
|
+
}
|
|
94
|
+
with httpx.Client(timeout=httpx.Timeout(90.0, connect=15.0)) as client:
|
|
95
|
+
response = client.post(f"{BASE}/v1/text/chatcompletion_v2", headers={"Authorization": f"Bearer {key}", "Content-Type": "application/json"}, json=body)
|
|
96
|
+
if response.status_code >= 400:
|
|
97
|
+
raise RuntimeError(f"MiniMax text HTTP {response.status_code}: {response.text[:300]}")
|
|
98
|
+
data = response.json()
|
|
99
|
+
base = data.get("base_resp") or {}
|
|
100
|
+
if base.get("status_code") not in (0, None):
|
|
101
|
+
raise RuntimeError(f"MiniMax text rejected: {base.get('status_msg')}")
|
|
102
|
+
content = ((data.get("choices") or [{}])[0].get("message") or {}).get("content") or ""
|
|
103
|
+
text = _strip_reasoning(str(content))
|
|
104
|
+
LAST_MODEL = MODEL
|
|
105
|
+
return text
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def chat(system: str, user: str, *, max_tokens: int = 1600, temperature: float = 0.3, text_model: dict[str, str] | None = None) -> str:
|
|
109
|
+
global LAST_ERROR
|
|
110
|
+
LAST_ERROR = ""
|
|
111
|
+
errors = []
|
|
112
|
+
if TEXT_PROVIDER != "minimax" and DSH_URL:
|
|
113
|
+
try:
|
|
114
|
+
return _chat_dsh(system, user, max_tokens=max_tokens, temperature=temperature, text_model=_normalize_text_model(text_model))
|
|
115
|
+
except Exception as exc: # noqa: BLE001
|
|
116
|
+
errors.append(f"DSH: {type(exc).__name__}: {exc}")
|
|
117
|
+
if TEXT_PROVIDER != "dsh" and os.environ.get("MINIMAX_API_KEY", "").strip():
|
|
118
|
+
try:
|
|
119
|
+
return _chat_minimax(system, user, max_tokens=max_tokens, temperature=temperature)
|
|
120
|
+
except Exception as exc: # noqa: BLE001
|
|
121
|
+
errors.append(f"MiniMax: {type(exc).__name__}: {exc}")
|
|
122
|
+
LAST_ERROR = " | ".join(errors)[:500] or "no text provider configured"
|
|
123
|
+
raise RuntimeError(LAST_ERROR)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def chat_json(system: str, user: str, *, max_tokens: int = 1600, temperature: float = 0.25, text_model: dict[str, str] | None = None) -> dict[str, Any] | None:
|
|
127
|
+
global LAST_ERROR
|
|
128
|
+
LAST_ERROR = ""
|
|
129
|
+
for attempt in range(2):
|
|
130
|
+
try:
|
|
131
|
+
text = chat(system, user, max_tokens=max_tokens + attempt * 800, temperature=temperature, text_model=text_model)
|
|
132
|
+
start, end = text.find("{"), text.rfind("}")
|
|
133
|
+
if start < 0 or end <= start:
|
|
134
|
+
LAST_ERROR = f"attempt {attempt + 1}: no JSON in {len(text)} chars"
|
|
135
|
+
continue
|
|
136
|
+
value = json.loads(text[start:end + 1])
|
|
137
|
+
if isinstance(value, dict):
|
|
138
|
+
return value
|
|
139
|
+
LAST_ERROR = f"attempt {attempt + 1}: JSON root is not an object"
|
|
140
|
+
except Exception as exc: # noqa: BLE001
|
|
141
|
+
LAST_ERROR = f"attempt {attempt + 1}: {type(exc).__name__}: {exc}"[:300]
|
|
142
|
+
return None
|
|
File without changes
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"""Lightweight Chinese ASR for reference clips.
|
|
2
|
+
|
|
3
|
+
faster-whisper is optional: if it is not installed (or ADSTUDIO_ASR=0), the
|
|
4
|
+
analysis pipeline falls back to the heuristic transcript-free path. Audio is
|
|
5
|
+
decoded by ffmpeg to PCM first because the installed PyAV/Python combination
|
|
6
|
+
does not accept faster-whisper's container options directly.
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import importlib.util
|
|
11
|
+
import os
|
|
12
|
+
import subprocess
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from .ffmpeg import FFMPEG
|
|
17
|
+
|
|
18
|
+
_MODEL = None
|
|
19
|
+
_MODEL_NAME = ""
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def available() -> bool:
|
|
23
|
+
if os.environ.get("ADSTUDIO_ASR", "1") == "0":
|
|
24
|
+
return False
|
|
25
|
+
return importlib.util.find_spec("faster_whisper") is not None
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _load_model():
|
|
29
|
+
global _MODEL, _MODEL_NAME
|
|
30
|
+
name = os.environ.get("ADSTUDIO_ASR_MODEL", "small")
|
|
31
|
+
if _MODEL is not None and _MODEL_NAME == name:
|
|
32
|
+
return _MODEL
|
|
33
|
+
from faster_whisper import WhisperModel
|
|
34
|
+
|
|
35
|
+
_MODEL = WhisperModel(name, device="cpu", compute_type="int8")
|
|
36
|
+
_MODEL_NAME = name
|
|
37
|
+
return _MODEL
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _pcm(path: str | Path) -> bytes:
|
|
41
|
+
proc = subprocess.run(
|
|
42
|
+
[FFMPEG, "-v", "error", "-i", str(path), "-vn", "-ac", "1", "-ar", "16000", "-f", "f32le", "-"],
|
|
43
|
+
capture_output=True,
|
|
44
|
+
timeout=120,
|
|
45
|
+
)
|
|
46
|
+
if proc.returncode != 0:
|
|
47
|
+
raise RuntimeError(f"audio decode failed: {proc.stderr[-300:]!r}")
|
|
48
|
+
return proc.stdout
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def transcribe(path: str | Path, *, max_seconds: float = 60.0) -> list[dict[str, Any]]:
|
|
52
|
+
if not available() or not Path(path).is_file():
|
|
53
|
+
return []
|
|
54
|
+
try:
|
|
55
|
+
import numpy as np
|
|
56
|
+
|
|
57
|
+
audio = np.frombuffer(_pcm(path), dtype=np.float32)
|
|
58
|
+
if audio.size < 1600:
|
|
59
|
+
return []
|
|
60
|
+
max_samples = int(max_seconds * 16000)
|
|
61
|
+
if audio.size > max_samples:
|
|
62
|
+
audio = audio[:max_samples]
|
|
63
|
+
model = _load_model()
|
|
64
|
+
segments, _info = model.transcribe(
|
|
65
|
+
audio,
|
|
66
|
+
language=os.environ.get("ADSTUDIO_ASR_LANGUAGE", "zh"),
|
|
67
|
+
vad_filter=True,
|
|
68
|
+
beam_size=1,
|
|
69
|
+
condition_on_previous_text=False,
|
|
70
|
+
)
|
|
71
|
+
rows = []
|
|
72
|
+
for seg in segments:
|
|
73
|
+
text = str(seg.text or "").strip()
|
|
74
|
+
if not text:
|
|
75
|
+
continue
|
|
76
|
+
rows.append({"start_s": round(float(seg.start), 3), "end_s": round(float(seg.end), 3), "text": text})
|
|
77
|
+
return rows
|
|
78
|
+
except Exception:
|
|
79
|
+
return []
|
|
@@ -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_synthetic_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 synthetic clip for tests.
|
|
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 synthetic: only the test mode uses it, never 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"synthetic 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
|