mediakit-cli 0.1.2__py3-none-any.whl
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.
- mediakit_cli/__init__.py +5 -0
- mediakit_cli/__main__.py +4 -0
- mediakit_cli/auto.py +217 -0
- mediakit_cli/cli.py +665 -0
- mediakit_cli/config.py +68 -0
- mediakit_cli/dub.py +174 -0
- mediakit_cli/edit.py +280 -0
- mediakit_cli/index.py +101 -0
- mediakit_cli/lyrics.py +92 -0
- mediakit_cli/mcp_server.py +162 -0
- mediakit_cli/music.py +226 -0
- mediakit_cli/ocr.py +129 -0
- mediakit_cli/polish.py +85 -0
- mediakit_cli/probe.py +89 -0
- mediakit_cli/shotcard.py +133 -0
- mediakit_cli/shots.py +109 -0
- mediakit_cli/subs.py +349 -0
- mediakit_cli/util.py +111 -0
- mediakit_cli/vlm.py +88 -0
- mediakit_cli/watch.py +119 -0
- mediakit_cli-0.1.2.dist-info/METADATA +221 -0
- mediakit_cli-0.1.2.dist-info/RECORD +26 -0
- mediakit_cli-0.1.2.dist-info/WHEEL +5 -0
- mediakit_cli-0.1.2.dist-info/entry_points.txt +3 -0
- mediakit_cli-0.1.2.dist-info/licenses/LICENSE +21 -0
- mediakit_cli-0.1.2.dist-info/top_level.txt +1 -0
mediakit_cli/cli.py
ADDED
|
@@ -0,0 +1,665 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import json
|
|
5
|
+
import sys
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from . import __version__
|
|
9
|
+
from .config import ollama_base, vlm_model
|
|
10
|
+
from .util import ensure_dir, human_duration, out_json, grab_frame
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _cmd_probe(args) -> dict:
|
|
14
|
+
from .probe import probe
|
|
15
|
+
|
|
16
|
+
return probe(args.file)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _cmd_shots(args) -> dict:
|
|
20
|
+
from .shots import contact_sheet, detect_shots, write_otio
|
|
21
|
+
from .util import media_path
|
|
22
|
+
|
|
23
|
+
src = media_path(args.file)
|
|
24
|
+
shots = detect_shots(src, threshold=args.threshold, min_seconds=args.min_len)
|
|
25
|
+
result = {"path": str(src), "count": len(shots), "shots": shots, "files": {}}
|
|
26
|
+
if args.out:
|
|
27
|
+
out = ensure_dir(args.out)
|
|
28
|
+
(out / "shots.json").write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
29
|
+
result["files"]["shots"] = str(out / "shots.json")
|
|
30
|
+
if args.sheet:
|
|
31
|
+
sheet = contact_sheet(src, shots, out / "contact.jpg", limit=args.sheet)
|
|
32
|
+
result["files"]["contact"] = str(sheet)
|
|
33
|
+
if args.otio:
|
|
34
|
+
otio = write_otio(src, shots, out / "timeline.otio")
|
|
35
|
+
result["files"]["otio"] = str(otio)
|
|
36
|
+
return result
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _cmd_frames(args) -> dict:
|
|
40
|
+
from .probe import probe
|
|
41
|
+
from .util import media_path
|
|
42
|
+
|
|
43
|
+
src = media_path(args.file)
|
|
44
|
+
info = probe(src)
|
|
45
|
+
duration = float(info.get("duration") or 0)
|
|
46
|
+
out = ensure_dir(args.out)
|
|
47
|
+
n = max(1, args.n)
|
|
48
|
+
items = []
|
|
49
|
+
for i in range(n):
|
|
50
|
+
t = round(duration * (i + 0.5) / n, 3)
|
|
51
|
+
dst = out / f"f{i + 1:03d}.jpg"
|
|
52
|
+
grab_frame(src, t, dst, width=args.width)
|
|
53
|
+
items.append({"t": t, "path": str(dst)})
|
|
54
|
+
return {"path": str(src), "duration": round(duration, 3), "count": n, "frames": items}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _cmd_subs(args) -> dict:
|
|
58
|
+
from .subs import build_lines, transcribe, write_subtitles
|
|
59
|
+
from .util import media_path
|
|
60
|
+
|
|
61
|
+
src = media_path(args.file)
|
|
62
|
+
asr = transcribe(src, language=args.lang, model=args.model, vad=not args.no_vad)
|
|
63
|
+
lines = build_lines(
|
|
64
|
+
asr, max_chars=args.max_chars, max_dur=args.max_dur, pause=args.pause
|
|
65
|
+
)
|
|
66
|
+
formats = tuple(f.strip() for f in args.formats.split(",") if f.strip())
|
|
67
|
+
res = None
|
|
68
|
+
if args.size:
|
|
69
|
+
try:
|
|
70
|
+
w, h = args.size.lower().replace("*", "x").split("x")
|
|
71
|
+
res = (int(w), int(h))
|
|
72
|
+
except Exception:
|
|
73
|
+
raise ValueError("--size 要写成 1080x1920 这种形式")
|
|
74
|
+
out = ensure_dir(args.out)
|
|
75
|
+
files = write_subtitles(
|
|
76
|
+
lines,
|
|
77
|
+
out,
|
|
78
|
+
stem=args.stem,
|
|
79
|
+
formats=formats,
|
|
80
|
+
font_size=args.font_size,
|
|
81
|
+
resolution=res,
|
|
82
|
+
karaoke=args.karaoke,
|
|
83
|
+
)
|
|
84
|
+
result = {
|
|
85
|
+
"path": str(src),
|
|
86
|
+
"language": asr["language"],
|
|
87
|
+
"duration": asr["duration"],
|
|
88
|
+
"segments": len(asr["segments"]),
|
|
89
|
+
"words": len(asr["words"]),
|
|
90
|
+
"lines": len(lines),
|
|
91
|
+
"files": files,
|
|
92
|
+
"preview": lines[:8],
|
|
93
|
+
"asr_segments": asr["segments"],
|
|
94
|
+
}
|
|
95
|
+
if args.qa:
|
|
96
|
+
from .subs import quality_report
|
|
97
|
+
|
|
98
|
+
result["qa"] = quality_report(lines, max_chars=args.max_chars)
|
|
99
|
+
return result
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _cmd_subqa(args) -> dict:
|
|
103
|
+
import pysubs2
|
|
104
|
+
|
|
105
|
+
from .subs import quality_report
|
|
106
|
+
from .util import media_path
|
|
107
|
+
|
|
108
|
+
subs = pysubs2.load(str(media_path(args.file)))
|
|
109
|
+
lines = [
|
|
110
|
+
{"text": ev.plaintext, "start": ev.start / 1000.0, "end": ev.end / 1000.0}
|
|
111
|
+
for ev in subs
|
|
112
|
+
if ev.plaintext.strip()
|
|
113
|
+
]
|
|
114
|
+
report = quality_report(
|
|
115
|
+
lines,
|
|
116
|
+
max_cps_cjk=args.max_cps_cjk,
|
|
117
|
+
max_cps_latin=args.max_cps_latin,
|
|
118
|
+
max_chars=args.max_chars,
|
|
119
|
+
)
|
|
120
|
+
report["file"] = str(args.file)
|
|
121
|
+
return report
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _cmd_ocr(args) -> dict:
|
|
125
|
+
import json
|
|
126
|
+
|
|
127
|
+
from .ocr import ocr_video, write_ocr_srt
|
|
128
|
+
|
|
129
|
+
result = ocr_video(
|
|
130
|
+
args.file,
|
|
131
|
+
every=args.every,
|
|
132
|
+
max_frames=args.max_frames,
|
|
133
|
+
min_score=args.min_score,
|
|
134
|
+
progress=lambda m: print(f"[ocr] {m}", file=sys.stderr),
|
|
135
|
+
)
|
|
136
|
+
out = ensure_dir(args.out)
|
|
137
|
+
json_path = out / "ocr.json"
|
|
138
|
+
json_path.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
139
|
+
result["files"] = {"json": str(json_path)}
|
|
140
|
+
if result["segments"]:
|
|
141
|
+
result["files"]["srt"] = write_ocr_srt(result, out / "ocr.srt")
|
|
142
|
+
return result
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def _cmd_index(args) -> dict:
|
|
146
|
+
from .index import build_index
|
|
147
|
+
|
|
148
|
+
return build_index(
|
|
149
|
+
args.file,
|
|
150
|
+
args.out,
|
|
151
|
+
every=args.every,
|
|
152
|
+
max_frames=args.max_frames,
|
|
153
|
+
progress=lambda m: print(f"[index] {m}", file=sys.stderr),
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _cmd_search(args) -> dict:
|
|
158
|
+
from .index import search
|
|
159
|
+
|
|
160
|
+
return search(args.dir, args.query, topk=args.top)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _cmd_lyrics(args) -> dict:
|
|
164
|
+
from .lyrics import make_lyrics
|
|
165
|
+
|
|
166
|
+
return make_lyrics(
|
|
167
|
+
args.file,
|
|
168
|
+
args.out,
|
|
169
|
+
lang=args.lang,
|
|
170
|
+
model=args.model,
|
|
171
|
+
separate=not args.no_separate,
|
|
172
|
+
max_chars=args.max_chars,
|
|
173
|
+
pause=args.pause,
|
|
174
|
+
karaoke=not args.no_karaoke,
|
|
175
|
+
resolution=_parse_size(args.size, (1920, 1080)),
|
|
176
|
+
sep_model=args.sep_model,
|
|
177
|
+
progress=lambda m: print(f"[lyrics] {m}", file=sys.stderr),
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def _cmd_dub(args) -> dict:
|
|
182
|
+
from .dub import dub
|
|
183
|
+
|
|
184
|
+
return dub(
|
|
185
|
+
args.file,
|
|
186
|
+
args.srt,
|
|
187
|
+
args.out,
|
|
188
|
+
voice_name=args.voice,
|
|
189
|
+
rate=args.rate,
|
|
190
|
+
mix_original=args.mix_original,
|
|
191
|
+
fit=not args.no_fit,
|
|
192
|
+
progress=lambda m: print(f"[dub] {m}", file=sys.stderr),
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def _cmd_voices(args) -> dict:
|
|
197
|
+
from .dub import list_voices
|
|
198
|
+
|
|
199
|
+
return {"voices": list_voices()}
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _cmd_shotcard(args) -> dict:
|
|
203
|
+
from .shotcard import analyze
|
|
204
|
+
|
|
205
|
+
return analyze(
|
|
206
|
+
args.file,
|
|
207
|
+
args.out,
|
|
208
|
+
max_shots=args.max_shots,
|
|
209
|
+
use_vlm=args.vlm,
|
|
210
|
+
vlm_max=args.vlm_max,
|
|
211
|
+
vlm_model=args.vlm_model,
|
|
212
|
+
progress=lambda m: print(f"[shotcard] {m}", file=sys.stderr),
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def _cmd_music(args) -> dict:
|
|
217
|
+
from .music import analyze
|
|
218
|
+
from .util import media_path
|
|
219
|
+
|
|
220
|
+
result = analyze(
|
|
221
|
+
media_path(args.file),
|
|
222
|
+
with_beats=not args.no_beats,
|
|
223
|
+
with_chords=args.chords,
|
|
224
|
+
with_melody=args.melody,
|
|
225
|
+
melody_max=args.melody_max,
|
|
226
|
+
engine=args.engine,
|
|
227
|
+
)
|
|
228
|
+
if args.out:
|
|
229
|
+
out = Path(args.out)
|
|
230
|
+
ensure_dir(out.parent)
|
|
231
|
+
out.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
232
|
+
result["out"] = str(out.resolve())
|
|
233
|
+
return result
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def _cmd_watch(args) -> dict:
|
|
237
|
+
from .watch import build_card
|
|
238
|
+
|
|
239
|
+
return build_card(
|
|
240
|
+
args.file,
|
|
241
|
+
args.out,
|
|
242
|
+
lang=args.lang,
|
|
243
|
+
model=args.model,
|
|
244
|
+
max_frames=args.frames,
|
|
245
|
+
do_asr=not args.no_asr,
|
|
246
|
+
do_subs=not args.no_subs,
|
|
247
|
+
do_ocr=args.ocr,
|
|
248
|
+
progress=lambda m: print(f"[watch] {m}", file=sys.stderr),
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def _cmd_edit(args) -> dict:
|
|
253
|
+
from .edit import load_plan, render
|
|
254
|
+
|
|
255
|
+
plan = load_plan(args.plan)
|
|
256
|
+
return render(plan, args.out)
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def _parse_size(text: str, default=(1080, 1920)):
|
|
260
|
+
if not text:
|
|
261
|
+
return default
|
|
262
|
+
try:
|
|
263
|
+
w, h = text.lower().replace("*", "x").split("x")
|
|
264
|
+
return (int(w), int(h))
|
|
265
|
+
except Exception:
|
|
266
|
+
raise ValueError("尺寸要写成 1080x1920 这种形式")
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def _cmd_auto(args) -> dict:
|
|
270
|
+
from .auto import generate
|
|
271
|
+
|
|
272
|
+
return generate(
|
|
273
|
+
args.music,
|
|
274
|
+
args.src,
|
|
275
|
+
args.out,
|
|
276
|
+
size=_parse_size(args.size),
|
|
277
|
+
fps=args.fps,
|
|
278
|
+
fit=args.fit,
|
|
279
|
+
beats_per_cut=args.beats_per_cut,
|
|
280
|
+
pace=args.pace,
|
|
281
|
+
transition=args.transition,
|
|
282
|
+
transition_duration=args.transition_duration,
|
|
283
|
+
max_sources=args.max_sources,
|
|
284
|
+
min_shot=args.min_shot,
|
|
285
|
+
seed=args.seed,
|
|
286
|
+
gain_db=args.gain_db,
|
|
287
|
+
plan_only=args.plan_only,
|
|
288
|
+
progress=lambda m: print(f"[auto] {m}", file=sys.stderr),
|
|
289
|
+
)
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def _cmd_polish(args) -> dict:
|
|
293
|
+
from .polish import polish
|
|
294
|
+
|
|
295
|
+
return polish(
|
|
296
|
+
args.file,
|
|
297
|
+
args.out,
|
|
298
|
+
model=args.model,
|
|
299
|
+
endpoint=args.endpoint,
|
|
300
|
+
max_cjk=args.max_cjk,
|
|
301
|
+
split=not args.no_split,
|
|
302
|
+
optimize=not args.no_optimize,
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
# ---------- human-readable output ----------
|
|
307
|
+
|
|
308
|
+
def _print_probe(info: dict) -> None:
|
|
309
|
+
v = info.get("video") or {}
|
|
310
|
+
a = info.get("audio") or {}
|
|
311
|
+
print(f"文件 {info['path']}")
|
|
312
|
+
print(f"时长 {human_duration(info['duration'])} ({info['duration']}s)")
|
|
313
|
+
print(f"体积 {info['size_bytes'] / 1024 / 1024:.2f} MB 码率 {info['bit_rate'] // 1000} kbps")
|
|
314
|
+
if v:
|
|
315
|
+
print(f"画面 {v['width']}x{v['height']} @ {v['fps']}fps {v['codec']} {v['pix_fmt']}")
|
|
316
|
+
if a:
|
|
317
|
+
print(f"声音 {a['channels']}ch {a['sample_rate']}Hz {a['codec']}")
|
|
318
|
+
if not a:
|
|
319
|
+
print("声音 (无音轨)")
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def _print_shots(res: dict) -> None:
|
|
323
|
+
print(f"共 {res['count']} 个镜头")
|
|
324
|
+
for s in res["shots"][:15]:
|
|
325
|
+
print(f" {s['index']:02d} {human_duration(s['start'])} → {human_duration(s['end'])} {s['dur']:.2f}s")
|
|
326
|
+
if res["count"] > 15:
|
|
327
|
+
print(f" … 其余 {res['count'] - 15} 个")
|
|
328
|
+
for k, v in (res.get("files") or {}).items():
|
|
329
|
+
print(f" {k}: {v}")
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def _print_subs(res: dict) -> None:
|
|
333
|
+
print(f"语言 {res['language']} 时长 {human_duration(res['duration'])} 词 {res['words']} 个")
|
|
334
|
+
print(f"断成 {res['lines']} 行:")
|
|
335
|
+
for ln in res["preview"]:
|
|
336
|
+
print(f" {ln['start']:7.2f} → {ln['end']:7.2f} {ln['text']}")
|
|
337
|
+
if res["lines"] > len(res["preview"]):
|
|
338
|
+
print(f" … 其余 {res['lines'] - len(res['preview'])} 行")
|
|
339
|
+
for fmt, path in res["files"].items():
|
|
340
|
+
print(f" {fmt}: {path}")
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
def _print_subqa(res: dict) -> None:
|
|
344
|
+
print(f"{res['file']}")
|
|
345
|
+
print(f"{res['lines']} 条 · 平均 {res['avg_cps']} 字/秒 · 最高 {res['max_cps']} 字/秒")
|
|
346
|
+
if res["ok"]:
|
|
347
|
+
print("没有发现问题")
|
|
348
|
+
return
|
|
349
|
+
print(f"{res['issue_count']} 个问题:")
|
|
350
|
+
for issue in res["issues"][:20]:
|
|
351
|
+
print(f" 第 {issue['line']} 条 @{issue['time']}s [{issue['type']}] {issue['detail']}")
|
|
352
|
+
if res["issue_count"] > 20:
|
|
353
|
+
print(f" … 其余 {res['issue_count'] - 20} 个")
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
def _print_ocr(res: dict) -> None:
|
|
357
|
+
print(f"{res['path']}")
|
|
358
|
+
print(f"每 {res['every']}s 一帧 · {len(res['frames'])} 帧 · 合并成 {len(res['segments'])} 段")
|
|
359
|
+
for seg in res["segments"][:8]:
|
|
360
|
+
print(f" {seg['start']:7.2f} → {seg['end']:7.2f} {seg['text']}")
|
|
361
|
+
if len(res["segments"]) > 8:
|
|
362
|
+
print(f" … 其余 {len(res['segments']) - 8} 段")
|
|
363
|
+
for k, v in (res.get("files") or {}).items():
|
|
364
|
+
print(f" {k}: {v}")
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def _print_index(res: dict) -> None:
|
|
368
|
+
print(f"索引 {res['index']}")
|
|
369
|
+
print(f"{res['frames']} 帧 · 每 {res['every']}s · {res['model']}")
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
def _print_search(res: dict) -> None:
|
|
373
|
+
print(f"「{res['query']}」在 {res['video']}")
|
|
374
|
+
for hit in res["hits"]:
|
|
375
|
+
print(f" {hit['t']:8.2f}s score {hit['score']:.3f} {hit['thumb']}")
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
def _print_lyrics(res: dict) -> None:
|
|
379
|
+
print(f"歌曲 {res['song']}")
|
|
380
|
+
if res["vocals"]:
|
|
381
|
+
print(f"人声 {res['vocals']}")
|
|
382
|
+
print(f"语言 {res['language']} · {res['duration']}s · {res['words']} 词 · {res['lines']} 行")
|
|
383
|
+
for ln in res["preview"]:
|
|
384
|
+
print(f" {ln['start']:7.2f} → {ln['end']:7.2f} {ln['text']}")
|
|
385
|
+
for k, v in res["files"].items():
|
|
386
|
+
print(f" {k}: {v}")
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
def _print_dub(res: dict) -> None:
|
|
390
|
+
print(f"输出 {res['out']}")
|
|
391
|
+
print(f"{res['lines']} 句 · 音色 {res['voice']} · 语速 {res['rate']}")
|
|
392
|
+
if res.get("squeezed"):
|
|
393
|
+
print(f"{res['squeezed']} 句超长,已加速压进字幕时长(最大 {res['max_tempo']}×)")
|
|
394
|
+
if res["mix_original"]:
|
|
395
|
+
print("原声保留 25%,与配音混合")
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
def _print_voices(res: dict) -> None:
|
|
399
|
+
for v in res["voices"]:
|
|
400
|
+
print(v)
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
def _print_shotcard(res: dict) -> None:
|
|
404
|
+
s = res["summary"]
|
|
405
|
+
print(f"{res['path']} · {res['count']} 个镜头 · {res['duration']}s")
|
|
406
|
+
print(
|
|
407
|
+
f"平均亮度 {s['avg_brightness']} · 平均饱和 {s['avg_saturation']} · "
|
|
408
|
+
f"静止 {s['static_shots']} / 运动 {s['moving_shots']}"
|
|
409
|
+
)
|
|
410
|
+
for shot in res["shots"][:6]:
|
|
411
|
+
colors = " ".join(c["hex"] for c in shot["colors"][:3])
|
|
412
|
+
print(
|
|
413
|
+
f" {shot['index']:02d} {shot['start']:.2f}-{shot['end']:.2f}s "
|
|
414
|
+
f"亮 {shot['brightness']:.2f} 饱和 {shot['saturation']:.2f} "
|
|
415
|
+
f"{shot['motion']['label']}({shot['motion']['magnitude']}) {colors}"
|
|
416
|
+
)
|
|
417
|
+
if shot.get("vlm") and shot["vlm"].get("caption"):
|
|
418
|
+
v = shot["vlm"]
|
|
419
|
+
print(f" {v.get('shot_size', '?')} · {v.get('camera', '?')} · {v.get('mood', '?')} · {v['caption']}")
|
|
420
|
+
if res["count"] > 6:
|
|
421
|
+
print(f" … 其余 {res['count'] - 6} 个")
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
def _print_music(res: dict) -> None:
|
|
425
|
+
key = res.get("key") or {}
|
|
426
|
+
print(f"时长 {human_duration(res['duration'])} 速度 {res['tempo_bpm']} BPM 拍数 {res['beat_count']}")
|
|
427
|
+
if res.get("engine") and res["engine"] != "librosa":
|
|
428
|
+
print(f"节拍引擎 {res['engine']} 强拍 {len(res.get('downbeats') or [])} 个")
|
|
429
|
+
print(f"调性 {key.get('label')}(相关度 {key.get('correlation')})")
|
|
430
|
+
print(f"响度 {res['loudness_dbfs']} dBFS 峰值 {res['peak_dbfs']} dBFS")
|
|
431
|
+
print(f"段落 {len(res['sections'])} 段:")
|
|
432
|
+
for sec in res["sections"]:
|
|
433
|
+
print(f" {human_duration(sec['start'])} → {human_duration(sec['end'])} energy {sec['energy']}")
|
|
434
|
+
if res.get("chords"):
|
|
435
|
+
line = " → ".join(c["chord"] for c in res["chords"][:14])
|
|
436
|
+
print(f"和弦 {len(res['chords'])} 段:{line}")
|
|
437
|
+
if res.get("melody"):
|
|
438
|
+
notes = " ".join(m["note"] for m in res["melody"][:10])
|
|
439
|
+
print(f"旋律 {len(res['melody'])} 个音:{notes} …")
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
def _print_watch(card: dict) -> None:
|
|
443
|
+
info = card["probe"]
|
|
444
|
+
print(f"文件 {info['path']}")
|
|
445
|
+
print(f"时长 {human_duration(info['duration'])} 镜头 {len(card['shots'])} 个 关键帧 {len(card['keyframes'])} 张")
|
|
446
|
+
print(f"转写 {len(card['transcript'])} 段" + (f" (失败:{card['asr_error']})" if card["asr_error"] else ""))
|
|
447
|
+
for seg in card["transcript"][:6]:
|
|
448
|
+
print(f" {seg['start']:7.2f} → {seg['end']:7.2f} {seg['text']}")
|
|
449
|
+
if len(card["transcript"]) > 6:
|
|
450
|
+
print(f" … 其余 {len(card['transcript']) - 6} 段")
|
|
451
|
+
for k, v in card["files"].items():
|
|
452
|
+
if isinstance(v, dict):
|
|
453
|
+
for kk, vv in v.items():
|
|
454
|
+
print(f" {kk}: {vv}")
|
|
455
|
+
elif v:
|
|
456
|
+
print(f" {k}: {v}")
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
def _print_auto(res: dict) -> None:
|
|
460
|
+
print(f"输出 {res.get('out') or '(只出计划,未渲染)'}")
|
|
461
|
+
print(
|
|
462
|
+
f"{res['tempo_bpm']} BPM · {res['segments']} 段 · "
|
|
463
|
+
f"{res['pool_shots']} 个候选镜头 / {res['sources']} 个素材文件"
|
|
464
|
+
)
|
|
465
|
+
print(f"种子 {res['seed']} · 复用 {res['stats']['reused']} 次 · 素材不够压短 {res['stats']['clamped']} 次")
|
|
466
|
+
print(f"计划 {res['plan']}")
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
def _print_polish(res: dict) -> None:
|
|
470
|
+
print(f"输入 {res['in']}")
|
|
471
|
+
print(f"输出 {res['out']} ({res['lines']} 行,{res['bytes']} 字节)")
|
|
472
|
+
print(f"模型 {res['model']} @ {res['endpoint']}")
|
|
473
|
+
print("注意:VideoCaptioner 优化完会主动去标点;4B 级模型偶有改写,先小样对比再全量")
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
def _print_edit(res: dict) -> None:
|
|
477
|
+
print(f"输出 {res['out']}")
|
|
478
|
+
print(f"{res['clips']} 个片段 {res['duration']:.2f}s {res['size'][0]}x{res['size'][1]} @ {res['fps']}fps")
|
|
479
|
+
print(f"字幕 {'有' if res['subtitled'] else '无'} 配乐 {'有' if res['music'] else '无'} 原声 {'保留' if res['keep_audio'] else '丢弃'}")
|
|
480
|
+
for note in res.get("notes") or []:
|
|
481
|
+
print(f" 注意: {note}")
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
485
|
+
parser = argparse.ArgumentParser(
|
|
486
|
+
prog="mk", description="mediakit:把看视频/听音乐/做字幕/剪片子合成一套 CLI(供人或 agent 调用)"
|
|
487
|
+
)
|
|
488
|
+
parser.add_argument("--version", action="version", version=f"mediakit {__version__}")
|
|
489
|
+
parser.add_argument("--json", action="store_true", help="输出机器可读 JSON")
|
|
490
|
+
sub = parser.add_subparsers(dest="cmd", required=True)
|
|
491
|
+
|
|
492
|
+
p = sub.add_parser("probe", help="读媒体元信息")
|
|
493
|
+
p.add_argument("file")
|
|
494
|
+
p.set_defaults(func=_cmd_probe, printer=_print_probe)
|
|
495
|
+
|
|
496
|
+
p = sub.add_parser("shots", help="切镜头 + 联系表 + OTIO 时间轴")
|
|
497
|
+
p.add_argument("file")
|
|
498
|
+
p.add_argument("--out", help="输出目录")
|
|
499
|
+
p.add_argument("--threshold", type=float, default=27.0)
|
|
500
|
+
p.add_argument("--min-len", type=float, default=0.5, help="最短镜头秒数")
|
|
501
|
+
p.add_argument("--sheet", type=int, default=9, help="联系表最多几张,0 表示不生成")
|
|
502
|
+
p.add_argument("--otio", action="store_true", help="导出 timeline.otio")
|
|
503
|
+
p.set_defaults(func=_cmd_shots, printer=_print_shots)
|
|
504
|
+
|
|
505
|
+
p = sub.add_parser("frames", help="均匀抽帧")
|
|
506
|
+
p.add_argument("file")
|
|
507
|
+
p.add_argument("--n", type=int, default=12)
|
|
508
|
+
p.add_argument("--out", required=True)
|
|
509
|
+
p.add_argument("--width", type=int, default=960)
|
|
510
|
+
p.set_defaults(func=_cmd_frames, printer=lambda r: print(f"抽出 {r['count']} 张 → {r['frames'][0]['path'].rsplit(chr(92), 1)[0]}"))
|
|
511
|
+
|
|
512
|
+
p = sub.add_parser("subs", help="转写 + 断句,出 SRT/ASS")
|
|
513
|
+
p.add_argument("file")
|
|
514
|
+
p.add_argument("--lang", default=None, help="zh / en / ja …,不填自动识别")
|
|
515
|
+
p.add_argument("--model", default="small", help="tiny/base/small/medium/large-v3")
|
|
516
|
+
p.add_argument("--max-chars", type=float, default=16.0, help="每行最大显示宽度(汉字算 1)")
|
|
517
|
+
p.add_argument("--max-dur", type=float, default=6.0)
|
|
518
|
+
p.add_argument("--pause", type=float, default=0.28, help="停顿多少秒就断行")
|
|
519
|
+
p.add_argument("--formats", default="srt,ass")
|
|
520
|
+
p.add_argument("--stem", default="subs")
|
|
521
|
+
p.add_argument("--font-size", type=int, default=None, help="不填按画面高度自动算")
|
|
522
|
+
p.add_argument("--size", default=None, help="成片尺寸 WxH,例如 1080x1920;决定 ASS 的 PlayRes")
|
|
523
|
+
p.add_argument("--out", default=".")
|
|
524
|
+
p.add_argument("--no-vad", action="store_true")
|
|
525
|
+
p.add_argument("--karaoke", action="store_true", help="ASS 里生成逐字 \kf 卡拉OK")
|
|
526
|
+
p.add_argument("--qa", action="store_true", help="顺带跑一遍阅读速度/行长检查")
|
|
527
|
+
p.set_defaults(func=_cmd_subs, printer=_print_subs)
|
|
528
|
+
|
|
529
|
+
p = sub.add_parser("subqa", help="检查字幕阅读速度/行长/重叠")
|
|
530
|
+
p.add_argument("file")
|
|
531
|
+
p.add_argument("--max-cps-cjk", type=float, default=9.0)
|
|
532
|
+
p.add_argument("--max-cps-latin", type=float, default=20.0)
|
|
533
|
+
p.add_argument("--max-chars", type=float, default=16.0)
|
|
534
|
+
p.set_defaults(func=_cmd_subqa, printer=_print_subqa)
|
|
535
|
+
|
|
536
|
+
p = sub.add_parser("ocr", help="读画面上的字(硬字幕/歌词/录屏)")
|
|
537
|
+
p.add_argument("file")
|
|
538
|
+
p.add_argument("--out", required=True, help="输出目录")
|
|
539
|
+
p.add_argument("--every", type=float, default=2.0, help="每几秒取一帧")
|
|
540
|
+
p.add_argument("--max-frames", type=int, default=60)
|
|
541
|
+
p.add_argument("--min-score", type=float, default=0.5)
|
|
542
|
+
p.set_defaults(func=_cmd_ocr, printer=_print_ocr)
|
|
543
|
+
|
|
544
|
+
p = sub.add_parser("shotcard", help="每个镜头的亮度/饱和/主色/运动")
|
|
545
|
+
p.add_argument("file")
|
|
546
|
+
p.add_argument("--out", required=True)
|
|
547
|
+
p.add_argument("--max-shots", type=int, default=None)
|
|
548
|
+
p.add_argument("--vlm", action="store_true", help="用本机 VLM 给每个镜头写景别/机位/描述")
|
|
549
|
+
p.add_argument("--vlm-max", type=int, default=12, help="最多给几个镜头跑 VLM")
|
|
550
|
+
p.add_argument("--vlm-model", default=None)
|
|
551
|
+
p.set_defaults(func=_cmd_shotcard, printer=_print_shotcard)
|
|
552
|
+
|
|
553
|
+
p = sub.add_parser("index", help="给长视频建 CLIP 索引(可搜片段)")
|
|
554
|
+
p.add_argument("file")
|
|
555
|
+
p.add_argument("--out", required=True)
|
|
556
|
+
p.add_argument("--every", type=float, default=2.0)
|
|
557
|
+
p.add_argument("--max-frames", type=int, default=150)
|
|
558
|
+
p.set_defaults(func=_cmd_index, printer=_print_index)
|
|
559
|
+
|
|
560
|
+
p = sub.add_parser("search", help="用一句话在索引里找片段")
|
|
561
|
+
p.add_argument("dir")
|
|
562
|
+
p.add_argument("query")
|
|
563
|
+
p.add_argument("--top", type=int, default=5)
|
|
564
|
+
p.set_defaults(func=_cmd_search, printer=_print_search)
|
|
565
|
+
|
|
566
|
+
p = sub.add_parser("dub", help="用 Windows 本地语音给字幕配音")
|
|
567
|
+
p.add_argument("file")
|
|
568
|
+
p.add_argument("--srt", required=True)
|
|
569
|
+
p.add_argument("--out", required=True)
|
|
570
|
+
p.add_argument("--voice", default=None, help="音色名的一部分,例如 Huihui")
|
|
571
|
+
p.add_argument("--rate", type=int, default=0, help="-10..10")
|
|
572
|
+
p.add_argument("--mix-original", action="store_true")
|
|
573
|
+
p.add_argument("--no-fit", action="store_true", help="不把过长的配音加速压进字幕时长")
|
|
574
|
+
p.set_defaults(func=_cmd_dub, printer=_print_dub)
|
|
575
|
+
|
|
576
|
+
p = sub.add_parser("voices", help="列出本机 SAPI 语音")
|
|
577
|
+
p.set_defaults(func=_cmd_voices, printer=_print_voices)
|
|
578
|
+
|
|
579
|
+
p = sub.add_parser("lyrics", help="分人声 + 识别歌词,出 SRT/ASS/LRC")
|
|
580
|
+
p.add_argument("file")
|
|
581
|
+
p.add_argument("--out", required=True)
|
|
582
|
+
p.add_argument("--lang", default=None)
|
|
583
|
+
p.add_argument("--model", default="small")
|
|
584
|
+
p.add_argument("--no-separate", action="store_true")
|
|
585
|
+
p.add_argument("--sep-model", default=None)
|
|
586
|
+
p.add_argument("--max-chars", type=float, default=18.0)
|
|
587
|
+
p.add_argument("--pause", type=float, default=0.35)
|
|
588
|
+
p.add_argument("--no-karaoke", action="store_true")
|
|
589
|
+
p.add_argument("--size", default="1920x1080")
|
|
590
|
+
p.set_defaults(func=_cmd_lyrics, printer=_print_lyrics)
|
|
591
|
+
|
|
592
|
+
p = sub.add_parser("music", help="测 BPM/调性/段落")
|
|
593
|
+
p.add_argument("file")
|
|
594
|
+
p.add_argument("--out", help="把结果写成 JSON")
|
|
595
|
+
p.add_argument("--no-beats", action="store_true")
|
|
596
|
+
p.add_argument("--chords", action="store_true", help="估和弦进行(模板匹配)")
|
|
597
|
+
p.add_argument("--melody", action="store_true", help="估主旋律音高(pyin)")
|
|
598
|
+
p.add_argument("--melody-max", type=float, default=40.0, help="旋律只分析前 N 秒")
|
|
599
|
+
p.add_argument("--engine", choices=("librosa", "beat-this"), default="librosa",
|
|
600
|
+
help="节拍追踪器;beat-this 会同时给 downbeats")
|
|
601
|
+
p.set_defaults(func=_cmd_music, printer=_print_music)
|
|
602
|
+
|
|
603
|
+
p = sub.add_parser("watch", help="一张卡片看完一个视频")
|
|
604
|
+
p.add_argument("file")
|
|
605
|
+
p.add_argument("--out", required=True)
|
|
606
|
+
p.add_argument("--lang", default=None)
|
|
607
|
+
p.add_argument("--model", default="small")
|
|
608
|
+
p.add_argument("--frames", type=int, default=12)
|
|
609
|
+
p.add_argument("--no-asr", action="store_true")
|
|
610
|
+
p.add_argument("--no-subs", action="store_true")
|
|
611
|
+
p.add_argument("--ocr", action="store_true", help="顺带读关键帧上的字")
|
|
612
|
+
p.set_defaults(func=_cmd_watch, printer=_print_watch)
|
|
613
|
+
|
|
614
|
+
p = sub.add_parser("auto", help="音乐 + 素材 → 卡点短片")
|
|
615
|
+
p.add_argument("--music", required=True)
|
|
616
|
+
p.add_argument("--src", action="append", required=True, help="素材文件或目录,可重复给")
|
|
617
|
+
p.add_argument("--out", required=True)
|
|
618
|
+
p.add_argument("--size", default="1080x1920")
|
|
619
|
+
p.add_argument("--fps", type=int, default=30)
|
|
620
|
+
p.add_argument("--fit", default="crop", choices=["crop", "pad"])
|
|
621
|
+
p.add_argument("--beats-per-cut", type=int, default=4, help="每几拍切一次(--pace fixed 时生效)")
|
|
622
|
+
p.add_argument("--pace", default="auto", choices=["auto", "fixed"], help="auto=按段落能量变节奏")
|
|
623
|
+
p.add_argument("--max-sources", type=int, default=12)
|
|
624
|
+
p.add_argument("--min-shot", type=float, default=0.8)
|
|
625
|
+
p.add_argument("--seed", type=int, default=7)
|
|
626
|
+
p.add_argument("--gain-db", type=float, default=-14)
|
|
627
|
+
p.add_argument("--plan-only", action="store_true")
|
|
628
|
+
p.add_argument("--transition", default="none", choices=["none", "fade", "dissolve", "wipeleft", "slideleft"])
|
|
629
|
+
p.add_argument("--transition-duration", type=float, default=0.25)
|
|
630
|
+
p.set_defaults(func=_cmd_auto, printer=_print_auto)
|
|
631
|
+
|
|
632
|
+
p = sub.add_parser("polish", help="用本地 Ollama(经 VideoCaptioner)润色字幕")
|
|
633
|
+
p.add_argument("file")
|
|
634
|
+
p.add_argument("--out", required=True)
|
|
635
|
+
p.add_argument("--model", default=None)
|
|
636
|
+
p.add_argument("--endpoint", default=None)
|
|
637
|
+
p.add_argument("--max-cjk", type=int, default=16)
|
|
638
|
+
p.add_argument("--no-split", action="store_true")
|
|
639
|
+
p.add_argument("--no-optimize", action="store_true")
|
|
640
|
+
p.set_defaults(func=_cmd_polish, printer=_print_polish)
|
|
641
|
+
|
|
642
|
+
p = sub.add_parser("edit", help="按 JSON 计划剪一条片子")
|
|
643
|
+
p.add_argument("plan")
|
|
644
|
+
p.add_argument("--out", required=True)
|
|
645
|
+
p.set_defaults(func=_cmd_edit, printer=_print_edit)
|
|
646
|
+
|
|
647
|
+
return parser
|
|
648
|
+
|
|
649
|
+
|
|
650
|
+
def main(argv=None) -> int:
|
|
651
|
+
parser = build_parser()
|
|
652
|
+
args = parser.parse_args(argv)
|
|
653
|
+
try:
|
|
654
|
+
result = args.func(args)
|
|
655
|
+
except Exception as exc:
|
|
656
|
+
sys.stderr.write(f"错误: {exc}\n")
|
|
657
|
+
return 2
|
|
658
|
+
if args.json:
|
|
659
|
+
out_json(result)
|
|
660
|
+
else:
|
|
661
|
+
try:
|
|
662
|
+
args.printer(result)
|
|
663
|
+
except Exception:
|
|
664
|
+
out_json(result)
|
|
665
|
+
return 0
|