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/lyrics.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import subprocess
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from .config import find_tool
|
|
7
|
+
from .subs import build_lines, transcribe, write_subtitles
|
|
8
|
+
from .util import ensure_dir, media_path
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def separate_vocals(song, out_dir, model: str | None = None, progress=None) -> Path:
|
|
12
|
+
note = progress or (lambda *_: None)
|
|
13
|
+
out = ensure_dir(out_dir)
|
|
14
|
+
exe = find_tool("audio-separator", "MEDIAKIT_AUDIO_SEPARATOR")
|
|
15
|
+
if not exe:
|
|
16
|
+
raise RuntimeError(
|
|
17
|
+
"找不到 audio-separator:pip install audio-separator,或设置 MEDIAKIT_AUDIO_SEPARATOR"
|
|
18
|
+
)
|
|
19
|
+
cmd = [
|
|
20
|
+
exe,
|
|
21
|
+
str(song),
|
|
22
|
+
"--output_dir", str(out),
|
|
23
|
+
"--output_format", "wav",
|
|
24
|
+
]
|
|
25
|
+
if model:
|
|
26
|
+
cmd += ["--model_filename", model]
|
|
27
|
+
note("分轨中(首次会下载模型)")
|
|
28
|
+
p = subprocess.run(
|
|
29
|
+
cmd, capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=7200
|
|
30
|
+
)
|
|
31
|
+
vocals = sorted(out.glob("*Vocals*.wav"))
|
|
32
|
+
if not vocals:
|
|
33
|
+
tail = (p.stderr or p.stdout or "").strip().splitlines()[-3:]
|
|
34
|
+
raise RuntimeError("没有产出人声轨: " + " | ".join(tail))
|
|
35
|
+
return vocals[0]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _lrc_time(sec: float) -> str:
|
|
39
|
+
m, s = divmod(max(sec, 0.0), 60)
|
|
40
|
+
return f"[{int(m):02d}:{s:05.2f}]"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def write_lrc(lines: list, path) -> Path:
|
|
44
|
+
path = Path(path)
|
|
45
|
+
ensure_dir(path.parent)
|
|
46
|
+
body = "\n".join(f"{_lrc_time(ln['start'])}{ln['text']}" for ln in lines)
|
|
47
|
+
path.write_text(body + "\n", encoding="utf-8")
|
|
48
|
+
return path
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def make_lyrics(
|
|
52
|
+
song,
|
|
53
|
+
out_dir,
|
|
54
|
+
lang: str | None = None,
|
|
55
|
+
model: str = "small",
|
|
56
|
+
separate: bool = True,
|
|
57
|
+
max_chars: float = 18.0,
|
|
58
|
+
pause: float = 0.35,
|
|
59
|
+
karaoke: bool = True,
|
|
60
|
+
resolution=(1920, 1080),
|
|
61
|
+
sep_model: str | None = None,
|
|
62
|
+
progress=None,
|
|
63
|
+
) -> dict:
|
|
64
|
+
note_impl = progress or (lambda *_: None)
|
|
65
|
+
note = note_impl
|
|
66
|
+
src = media_path(song)
|
|
67
|
+
out = ensure_dir(out_dir)
|
|
68
|
+
|
|
69
|
+
audio = src
|
|
70
|
+
vocals = None
|
|
71
|
+
if separate:
|
|
72
|
+
vocals = separate_vocals(src, out / "stems", model=sep_model, progress=note_impl)
|
|
73
|
+
audio = vocals
|
|
74
|
+
note_impl(f"人声: {vocals.name}")
|
|
75
|
+
|
|
76
|
+
asr = transcribe(audio, language=lang, model=model)
|
|
77
|
+
lines = build_lines(asr, max_chars=max_chars, max_dur=8.0, pause=pause, min_chars=2.0)
|
|
78
|
+
files = write_subtitles(
|
|
79
|
+
lines, out, stem="lyrics", formats=("srt", "ass"), resolution=resolution, karaoke=karaoke
|
|
80
|
+
)
|
|
81
|
+
files["lrc"] = str(write_lrc(lines, out / "lyrics.lrc"))
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
"song": str(src),
|
|
85
|
+
"vocals": str(vocals) if vocals else None,
|
|
86
|
+
"language": asr["language"],
|
|
87
|
+
"duration": asr["duration"],
|
|
88
|
+
"words": len(asr["words"]),
|
|
89
|
+
"lines": len(lines),
|
|
90
|
+
"files": files,
|
|
91
|
+
"preview": lines[:6],
|
|
92
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"""MCP server exposing mediakit as agent tools.
|
|
2
|
+
|
|
3
|
+
Run `mediakit-mcp` to serve over stdio. Each tool returns a JSON-serializable
|
|
4
|
+
dict; heavy dependencies are imported lazily so the server starts fast.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
try:
|
|
12
|
+
from mcp.server.fastmcp import FastMCP as _Server # mcp < 2
|
|
13
|
+
except ImportError: # mcp >= 2 renamed FastMCP to MCPServer
|
|
14
|
+
try:
|
|
15
|
+
from mcp.server.mcpserver import MCPServer as _Server
|
|
16
|
+
except ImportError as exc: # pragma: no cover
|
|
17
|
+
raise SystemExit(
|
|
18
|
+
"The MCP server needs the optional dependency: pip install 'mediakit-cli[mcp]'"
|
|
19
|
+
) from exc
|
|
20
|
+
|
|
21
|
+
mcp = _Server("mediakit")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@mcp.tool()
|
|
25
|
+
def probe(path: str) -> dict:
|
|
26
|
+
"""Read container/codec/duration metadata for a media file."""
|
|
27
|
+
from .probe import probe as _probe
|
|
28
|
+
|
|
29
|
+
return _probe(path)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@mcp.tool()
|
|
33
|
+
def shots(path: str, threshold: float = 27.0, min_seconds: float = 0.5) -> dict:
|
|
34
|
+
"""Detect shot boundaries and return their timecodes."""
|
|
35
|
+
from .shots import detect_shots
|
|
36
|
+
|
|
37
|
+
shots_list = detect_shots(path, threshold=threshold, min_seconds=min_seconds)
|
|
38
|
+
return {"path": path, "count": len(shots_list), "shots": shots_list}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@mcp.tool()
|
|
42
|
+
def music(path: str, chords: bool = False, melody: bool = False, engine: str = "librosa") -> dict:
|
|
43
|
+
"""Analyze tempo, beats, key, loudness, sections; optionally chords/melody."""
|
|
44
|
+
from .music import analyze
|
|
45
|
+
|
|
46
|
+
return analyze(path, with_chords=chords, with_melody=melody, engine=engine)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@mcp.tool()
|
|
50
|
+
def subs(
|
|
51
|
+
path: str,
|
|
52
|
+
out_dir: str,
|
|
53
|
+
lang: str | None = None,
|
|
54
|
+
model: str = "small",
|
|
55
|
+
max_chars: float = 16.0,
|
|
56
|
+
size: str = "1920x1080",
|
|
57
|
+
karaoke: bool = False,
|
|
58
|
+
) -> dict:
|
|
59
|
+
"""Transcribe speech into SRT/ASS with pause-aware line breaking."""
|
|
60
|
+
from .subs import build_lines, transcribe, write_subtitles
|
|
61
|
+
|
|
62
|
+
width, height = (int(x) for x in size.lower().split("x"))
|
|
63
|
+
asr = transcribe(path, language=lang, model=model)
|
|
64
|
+
lines = build_lines(asr, max_chars=max_chars)
|
|
65
|
+
files = write_subtitles(
|
|
66
|
+
lines, out_dir, formats=("srt", "ass"), resolution=(width, height), karaoke=karaoke
|
|
67
|
+
)
|
|
68
|
+
return {"path": path, "language": asr["language"], "lines": len(lines), "files": files}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@mcp.tool()
|
|
72
|
+
def ocr(path: str, out_dir: str, every: float = 2.0, max_frames: int = 60) -> dict:
|
|
73
|
+
"""Read on-screen text (hardcoded subtitles, lyrics, screen recordings)."""
|
|
74
|
+
from .ocr import ocr_video
|
|
75
|
+
|
|
76
|
+
return ocr_video(path, every=every, max_frames=max_frames)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
@mcp.tool()
|
|
80
|
+
def shotcard(path: str, out_dir: str, vlm: bool = False, max_shots: int | None = None) -> dict:
|
|
81
|
+
"""Per-shot brightness, saturation, dominant colors, motion; optional VLM captions."""
|
|
82
|
+
from .shotcard import analyze
|
|
83
|
+
|
|
84
|
+
return analyze(path, out_dir, max_shots=max_shots, use_vlm=vlm)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@mcp.tool()
|
|
88
|
+
def index(path: str, out_dir: str, every: float = 2.0, max_frames: int = 150) -> dict:
|
|
89
|
+
"""Build a CLIP index over frames so the video can be searched by sentence."""
|
|
90
|
+
from .index import build_index
|
|
91
|
+
|
|
92
|
+
return build_index(path, out_dir, every=every, max_frames=max_frames)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
@mcp.tool()
|
|
96
|
+
def search(index_dir: str, query: str, top: int = 5) -> dict:
|
|
97
|
+
"""Search a CLIP index with a natural-language sentence."""
|
|
98
|
+
from .index import search as _search
|
|
99
|
+
|
|
100
|
+
return _search(index_dir, query, topk=top)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
@mcp.tool()
|
|
104
|
+
def lyrics(path: str, out_dir: str, lang: str | None = None, separate: bool = False) -> dict:
|
|
105
|
+
"""Transcribe song lyrics to SRT/ASS/LRC; optionally separate vocals first."""
|
|
106
|
+
from .lyrics import make_lyrics
|
|
107
|
+
|
|
108
|
+
return make_lyrics(path, out_dir, lang=lang, separate=separate)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
@mcp.tool()
|
|
112
|
+
def dub(path: str, srt: str, out: str, voice: str | None = None, mix_original: bool = False) -> dict:
|
|
113
|
+
"""Dub subtitles with local Windows SAPI voices (Windows only)."""
|
|
114
|
+
from .dub import dub as _dub
|
|
115
|
+
|
|
116
|
+
return _dub(path, srt, out, voice_name=voice, mix_original=mix_original)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
@mcp.tool()
|
|
120
|
+
def edit(plan_path: str, out: str) -> dict:
|
|
121
|
+
"""Render an edit plan (clips, subtitles, music, transitions, beat sync)."""
|
|
122
|
+
import json
|
|
123
|
+
|
|
124
|
+
from .edit import render
|
|
125
|
+
|
|
126
|
+
plan = json.loads(Path(plan_path).read_text(encoding="utf-8"))
|
|
127
|
+
return render(plan, out)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
@mcp.tool()
|
|
131
|
+
def auto(
|
|
132
|
+
music_path: str,
|
|
133
|
+
src_dir: str,
|
|
134
|
+
out: str,
|
|
135
|
+
size: str = "1080x1920",
|
|
136
|
+
fit: str = "crop",
|
|
137
|
+
seed: int = 7,
|
|
138
|
+
transition: str | None = None,
|
|
139
|
+
plan_only: bool = False,
|
|
140
|
+
) -> dict:
|
|
141
|
+
"""Beat-synced montage from a music track and a folder of footage."""
|
|
142
|
+
from .auto import generate
|
|
143
|
+
|
|
144
|
+
width, height = (int(x) for x in size.lower().split("x"))
|
|
145
|
+
return generate(
|
|
146
|
+
music_path,
|
|
147
|
+
[src_dir],
|
|
148
|
+
out,
|
|
149
|
+
size=(width, height),
|
|
150
|
+
fit=fit,
|
|
151
|
+
seed=seed,
|
|
152
|
+
transition=transition,
|
|
153
|
+
plan_only=plan_only,
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def main() -> None:
|
|
158
|
+
mcp.run()
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
if __name__ == "__main__":
|
|
162
|
+
main()
|
mediakit_cli/music.py
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
|
|
8
|
+
from .util import media_path
|
|
9
|
+
|
|
10
|
+
_NOTES = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
|
|
11
|
+
_MAJOR = np.array([6.35, 2.23, 3.48, 2.33, 4.38, 4.09, 2.52, 5.19, 2.39, 3.66, 2.29, 2.88])
|
|
12
|
+
_MINOR = np.array([6.33, 2.68, 3.52, 5.38, 2.60, 3.53, 2.54, 4.75, 3.98, 2.69, 3.34, 3.17])
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _key(chroma_mean: np.ndarray):
|
|
16
|
+
best = None
|
|
17
|
+
for i in range(12):
|
|
18
|
+
rolled = np.roll(chroma_mean, -i)
|
|
19
|
+
for mode, tpl in (("major", _MAJOR), ("minor", _MINOR)):
|
|
20
|
+
try:
|
|
21
|
+
corr = float(np.corrcoef(rolled, tpl)[0, 1])
|
|
22
|
+
except Exception:
|
|
23
|
+
continue
|
|
24
|
+
if best is None or corr > best["correlation"]:
|
|
25
|
+
best = {
|
|
26
|
+
"tonic": _NOTES[i],
|
|
27
|
+
"mode": mode,
|
|
28
|
+
"label": f"{_NOTES[i]} {mode}",
|
|
29
|
+
"correlation": round(corr, 3),
|
|
30
|
+
}
|
|
31
|
+
return best or {"tonic": None, "mode": None, "label": None, "correlation": 0.0}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _sections(y: np.ndarray, sr: int, hop: int = 512, min_gap: float = 8.0):
|
|
35
|
+
rms = __import__("librosa").feature.rms(y=y, hop_length=hop)[0]
|
|
36
|
+
step = max(1, int(sr / hop))
|
|
37
|
+
env = np.array([rms[i:i + step].mean() for i in range(0, len(rms), step)], dtype=float)
|
|
38
|
+
if len(env) < 8:
|
|
39
|
+
return [{"start": 0.0, "end": round(len(y) / sr, 2), "energy": 0.0}]
|
|
40
|
+
k = min(5, len(env))
|
|
41
|
+
env_s = np.convolve(env, np.ones(k) / k, mode="same")
|
|
42
|
+
diff = np.abs(np.diff(env_s))
|
|
43
|
+
if diff.size == 0 or float(diff.max()) == 0.0:
|
|
44
|
+
return [{"start": 0.0, "end": round(len(y) / sr, 2), "energy": round(float(env.mean()), 4)}]
|
|
45
|
+
thr = float(diff.mean() + 1.2 * diff.std())
|
|
46
|
+
cand = [i + 1 for i, v in enumerate(diff) if v > thr]
|
|
47
|
+
cuts = [0.0]
|
|
48
|
+
for c in cand:
|
|
49
|
+
t = float(c)
|
|
50
|
+
if t - cuts[-1] >= min_gap and (len(y) / sr) - t >= min_gap:
|
|
51
|
+
cuts.append(t)
|
|
52
|
+
cuts.append(float(len(y) / sr))
|
|
53
|
+
out = []
|
|
54
|
+
for a, b in zip(cuts, cuts[1:]):
|
|
55
|
+
seg = env[int(a):int(b)]
|
|
56
|
+
out.append(
|
|
57
|
+
{
|
|
58
|
+
"start": round(a, 2),
|
|
59
|
+
"end": round(b, 2),
|
|
60
|
+
"energy": round(float(seg.mean()) if len(seg) else 0.0, 4),
|
|
61
|
+
}
|
|
62
|
+
)
|
|
63
|
+
return out
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def estimate_chords(y, sr: int, beats: list, min_dur: float = 0.35) -> list:
|
|
67
|
+
import librosa
|
|
68
|
+
|
|
69
|
+
chroma = librosa.feature.chroma_cqt(y=y, sr=sr)
|
|
70
|
+
times = librosa.times_like(chroma, sr=sr)
|
|
71
|
+
duration = len(y) / sr
|
|
72
|
+
bounds = [0.0] + [b for b in beats if 0 < b < duration] + [duration]
|
|
73
|
+
raw = []
|
|
74
|
+
for a, b in zip(bounds, bounds[1:]):
|
|
75
|
+
if b - a < min_dur:
|
|
76
|
+
continue
|
|
77
|
+
mask = (times >= a) & (times < b)
|
|
78
|
+
if not mask.any():
|
|
79
|
+
continue
|
|
80
|
+
v = chroma[:, mask].mean(axis=1)
|
|
81
|
+
nv = np.linalg.norm(v)
|
|
82
|
+
if nv < 1e-6:
|
|
83
|
+
continue
|
|
84
|
+
best_label, best_score = None, -1.0
|
|
85
|
+
for root in range(12):
|
|
86
|
+
for suffix, intervals in (("", (0, 4, 7)), ("m", (0, 3, 7))):
|
|
87
|
+
tpl = np.zeros(12)
|
|
88
|
+
for i in intervals:
|
|
89
|
+
tpl[(root + i) % 12] = 1.0
|
|
90
|
+
score = float(np.dot(v, tpl) / (nv * np.linalg.norm(tpl)))
|
|
91
|
+
if score > best_score:
|
|
92
|
+
best_score = score
|
|
93
|
+
best_label = f"{_NOTES[root]}{suffix}"
|
|
94
|
+
raw.append({"start": round(a, 2), "end": round(b, 2), "chord": best_label, "score": round(best_score, 3)})
|
|
95
|
+
|
|
96
|
+
merged = []
|
|
97
|
+
for seg in raw:
|
|
98
|
+
if merged and merged[-1]["chord"] == seg["chord"]:
|
|
99
|
+
merged[-1]["end"] = seg["end"]
|
|
100
|
+
merged[-1]["score"] = round((merged[-1]["score"] + seg["score"]) / 2, 3)
|
|
101
|
+
else:
|
|
102
|
+
merged.append(dict(seg))
|
|
103
|
+
return merged
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def estimate_melody(y, sr: int, max_seconds: float = 40.0) -> list:
|
|
107
|
+
import librosa
|
|
108
|
+
|
|
109
|
+
seg = y[: int(max_seconds * sr)]
|
|
110
|
+
if len(seg) < sr:
|
|
111
|
+
return []
|
|
112
|
+
f0, voiced, _ = librosa.pyin(
|
|
113
|
+
seg,
|
|
114
|
+
fmin=float(librosa.note_to_hz("C3")),
|
|
115
|
+
fmax=float(librosa.note_to_hz("C6")),
|
|
116
|
+
sr=sr,
|
|
117
|
+
frame_length=2048,
|
|
118
|
+
)
|
|
119
|
+
times = librosa.times_like(f0, sr=sr)
|
|
120
|
+
notes, cur = [], None
|
|
121
|
+
for t, f, v in zip(times, f0, voiced):
|
|
122
|
+
if not v or not np.isfinite(f) or f <= 0:
|
|
123
|
+
continue
|
|
124
|
+
midi = int(round(float(librosa.hz_to_midi(f))))
|
|
125
|
+
if cur and cur["midi"] == midi and (t - cur["end"]) < 0.12:
|
|
126
|
+
cur["end"] = float(t)
|
|
127
|
+
else:
|
|
128
|
+
if cur:
|
|
129
|
+
notes.append(cur)
|
|
130
|
+
cur = {"start": float(t), "end": float(t), "midi": midi}
|
|
131
|
+
if cur:
|
|
132
|
+
notes.append(cur)
|
|
133
|
+
out = []
|
|
134
|
+
for n in notes:
|
|
135
|
+
if n["end"] - n["start"] < 0.08:
|
|
136
|
+
continue
|
|
137
|
+
out.append(
|
|
138
|
+
{
|
|
139
|
+
"start": round(n["start"], 2),
|
|
140
|
+
"end": round(n["end"], 2),
|
|
141
|
+
"note": _NOTES[n["midi"] % 12] + str(n["midi"] // 12 - 1),
|
|
142
|
+
"midi": n["midi"],
|
|
143
|
+
}
|
|
144
|
+
)
|
|
145
|
+
return out
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def _tempo_prior(y, sr: int, lo: float = 60.0, hi: float = 120.0):
|
|
149
|
+
"""用 tempogram 在 60–120 里挑最强峰,压住 librosa 的三连音误判。"""
|
|
150
|
+
import librosa
|
|
151
|
+
|
|
152
|
+
onset = librosa.onset.onset_strength(y=y, sr=sr)
|
|
153
|
+
tg = librosa.feature.tempogram(onset_envelope=onset, sr=sr)
|
|
154
|
+
freqs = librosa.tempo_frequencies(tg.shape[0], sr=sr)
|
|
155
|
+
profile = tg.mean(axis=1)
|
|
156
|
+
mask = (freqs >= lo) & (freqs <= hi)
|
|
157
|
+
if not mask.any():
|
|
158
|
+
return None
|
|
159
|
+
return float(freqs[mask][int(np.argmax(profile[mask]))])
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def analyze(
|
|
163
|
+
path,
|
|
164
|
+
with_beats: bool = True,
|
|
165
|
+
with_chords: bool = False,
|
|
166
|
+
with_melody: bool = False,
|
|
167
|
+
melody_max: float = 40.0,
|
|
168
|
+
engine: str = "librosa",
|
|
169
|
+
) -> dict:
|
|
170
|
+
import librosa
|
|
171
|
+
|
|
172
|
+
p = media_path(path)
|
|
173
|
+
y, sr = librosa.load(str(p), sr=22050, mono=True)
|
|
174
|
+
duration = len(y) / sr
|
|
175
|
+
|
|
176
|
+
downbeats = None
|
|
177
|
+
prior = None
|
|
178
|
+
if engine == "beat-this":
|
|
179
|
+
from beat_this.inference import File2Beats
|
|
180
|
+
|
|
181
|
+
tracker = File2Beats(checkpoint_path="final0", device="cpu", dbn=False)
|
|
182
|
+
raw_beats, raw_downbeats = tracker(str(p))
|
|
183
|
+
beats = [round(float(b), 3) for b in raw_beats]
|
|
184
|
+
downbeats = [round(float(b), 3) for b in raw_downbeats]
|
|
185
|
+
if len(beats) > 1:
|
|
186
|
+
tempo = float(60.0 / float(np.median(np.diff(beats))))
|
|
187
|
+
else:
|
|
188
|
+
tempo = 0.0
|
|
189
|
+
else:
|
|
190
|
+
prior = _tempo_prior(y, sr)
|
|
191
|
+
tempo, beats = librosa.beat.beat_track(
|
|
192
|
+
y=y, sr=sr, units="time", trim=False, start_bpm=prior if prior else 120.0
|
|
193
|
+
)
|
|
194
|
+
tempo = float(np.atleast_1d(tempo)[0]) if tempo is not None else 0.0
|
|
195
|
+
beats = [round(float(b), 3) for b in beats]
|
|
196
|
+
|
|
197
|
+
chroma = librosa.feature.chroma_cqt(y=y, sr=sr)
|
|
198
|
+
key = _key(chroma.mean(axis=1))
|
|
199
|
+
|
|
200
|
+
rms = librosa.feature.rms(y=y)[0]
|
|
201
|
+
dbfs = float(20 * math.log10(max(float(rms.mean()), 1e-9)))
|
|
202
|
+
peak = float(20 * math.log10(max(float(np.abs(y).max()), 1e-9)))
|
|
203
|
+
|
|
204
|
+
result = {
|
|
205
|
+
"path": str(p),
|
|
206
|
+
"duration": round(duration, 3),
|
|
207
|
+
"sample_rate": 22050,
|
|
208
|
+
"tempo_bpm": round(tempo, 2),
|
|
209
|
+
"key": key,
|
|
210
|
+
"loudness_dbfs": round(dbfs, 2),
|
|
211
|
+
"peak_dbfs": round(peak, 2),
|
|
212
|
+
"beat_count": len(beats),
|
|
213
|
+
"beats": beats if with_beats else beats[:0],
|
|
214
|
+
"engine": engine,
|
|
215
|
+
"tempo_prior": round(prior, 2) if engine == "librosa" and prior else None,
|
|
216
|
+
"downbeats": downbeats or [],
|
|
217
|
+
"sections": _sections(y, sr),
|
|
218
|
+
"note": "sections 是按能量突变粗分的近似段落,不是乐谱级结构",
|
|
219
|
+
}
|
|
220
|
+
if with_chords:
|
|
221
|
+
result["chords"] = estimate_chords(y, sr, beats)
|
|
222
|
+
result["chord_note"] = "按拍做模板匹配(大三/小三),不是深度学习和弦识别,转位与七和弦会判错"
|
|
223
|
+
if with_melody:
|
|
224
|
+
result["melody"] = estimate_melody(y, sr, melody_max)
|
|
225
|
+
result["melody_note"] = f"pyin 单音高跟踪,只取前 {melody_max:g} 秒;复音/和声会串"
|
|
226
|
+
return result
|
mediakit_cli/ocr.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
import tempfile
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from .probe import probe
|
|
8
|
+
from .util import ensure_dir, ffmpeg
|
|
9
|
+
|
|
10
|
+
_ENGINE = None
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _engine():
|
|
14
|
+
global _ENGINE
|
|
15
|
+
if _ENGINE is None:
|
|
16
|
+
from rapidocr_onnxruntime import RapidOCR
|
|
17
|
+
|
|
18
|
+
_ENGINE = RapidOCR()
|
|
19
|
+
return _ENGINE
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def ocr_image(path, min_score: float = 0.5) -> list:
|
|
23
|
+
out = _engine()(str(path))
|
|
24
|
+
rows = out[0] if isinstance(out, tuple) else out
|
|
25
|
+
items = []
|
|
26
|
+
for row in rows or []:
|
|
27
|
+
try:
|
|
28
|
+
box, text, score = row[0], row[1], float(row[2])
|
|
29
|
+
except Exception:
|
|
30
|
+
continue
|
|
31
|
+
if score < min_score or not str(text).strip():
|
|
32
|
+
continue
|
|
33
|
+
xs = [p[0] for p in box]
|
|
34
|
+
ys = [p[1] for p in box]
|
|
35
|
+
items.append(
|
|
36
|
+
{
|
|
37
|
+
"text": str(text).strip(),
|
|
38
|
+
"score": round(score, 3),
|
|
39
|
+
"box": [round(min(xs)), round(min(ys)), round(max(xs)), round(max(ys))],
|
|
40
|
+
}
|
|
41
|
+
)
|
|
42
|
+
items.sort(key=lambda x: (x["box"][1], x["box"][0]))
|
|
43
|
+
return items
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _norm(text: str) -> str:
|
|
47
|
+
return re.sub(r"[\s,。!?、,.!?::;;]+", "", text or "")
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def ocr_video(
|
|
51
|
+
path,
|
|
52
|
+
every: float = 2.0,
|
|
53
|
+
max_frames: int = 60,
|
|
54
|
+
min_score: float = 0.5,
|
|
55
|
+
progress=None,
|
|
56
|
+
) -> dict:
|
|
57
|
+
note = progress or (lambda *_: None)
|
|
58
|
+
info = probe(path)
|
|
59
|
+
duration = float(info.get("duration") or 0)
|
|
60
|
+
if duration <= 0:
|
|
61
|
+
raise RuntimeError("读不到视频时长")
|
|
62
|
+
|
|
63
|
+
every = max(float(every), 0.2)
|
|
64
|
+
tmp = Path(tempfile.mkdtemp(prefix="mkocr_"))
|
|
65
|
+
try:
|
|
66
|
+
ffmpeg(
|
|
67
|
+
"-i", path,
|
|
68
|
+
"-vf", f"fps={1.0 / every:.6f}",
|
|
69
|
+
"-frames:v", str(max_frames),
|
|
70
|
+
"-q:v", "3",
|
|
71
|
+
str(tmp / "%05d.jpg"),
|
|
72
|
+
)
|
|
73
|
+
frames = sorted(tmp.glob("*.jpg"))
|
|
74
|
+
note(f"抽了 {len(frames)} 帧(每 {every}s)")
|
|
75
|
+
result_frames = []
|
|
76
|
+
for i, f in enumerate(frames):
|
|
77
|
+
t = round(i * every, 3)
|
|
78
|
+
texts = ocr_image(f, min_score=min_score)
|
|
79
|
+
result_frames.append({"t": t, "texts": texts})
|
|
80
|
+
if i % 10 == 0:
|
|
81
|
+
note(f"OCR {i + 1}/{len(frames)}")
|
|
82
|
+
finally:
|
|
83
|
+
for f in tmp.glob("*"):
|
|
84
|
+
try:
|
|
85
|
+
f.unlink()
|
|
86
|
+
except OSError:
|
|
87
|
+
pass
|
|
88
|
+
try:
|
|
89
|
+
tmp.rmdir()
|
|
90
|
+
except OSError:
|
|
91
|
+
pass
|
|
92
|
+
|
|
93
|
+
segments = []
|
|
94
|
+
for item in result_frames:
|
|
95
|
+
text = " ".join(x["text"] for x in item["texts"]).strip()
|
|
96
|
+
if not text:
|
|
97
|
+
continue
|
|
98
|
+
start = item["t"]
|
|
99
|
+
end = min(start + every, duration)
|
|
100
|
+
if segments and _norm(segments[-1]["text"]) == _norm(text):
|
|
101
|
+
segments[-1]["end"] = round(end, 3)
|
|
102
|
+
else:
|
|
103
|
+
segments.append({"start": round(start, 3), "end": round(end, 3), "text": text})
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
"path": str(Path(path).resolve()),
|
|
107
|
+
"duration": round(duration, 3),
|
|
108
|
+
"every": every,
|
|
109
|
+
"frames": result_frames,
|
|
110
|
+
"segments": segments,
|
|
111
|
+
"engine": "RapidOCR",
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def to_lines(segments: list) -> list:
|
|
116
|
+
return [
|
|
117
|
+
{"text": s["text"], "start": s["start"], "end": s["end"], "words": []}
|
|
118
|
+
for s in segments
|
|
119
|
+
if s.get("text")
|
|
120
|
+
]
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def write_ocr_srt(result: dict, out_path) -> str:
|
|
124
|
+
from .subs import write_subtitles
|
|
125
|
+
|
|
126
|
+
out = Path(out_path).resolve()
|
|
127
|
+
ensure_dir(out.parent)
|
|
128
|
+
write_subtitles(to_lines(result["segments"]), out.parent, stem=out.stem, formats=("srt",))
|
|
129
|
+
return str(out)
|
mediakit_cli/polish.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import subprocess
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from .config import find_tool, ollama_base, vlm_model
|
|
8
|
+
from .util import media_path
|
|
9
|
+
|
|
10
|
+
# Local loopback calls must not go through a proxy: some environments set a
|
|
11
|
+
# NO_PROXY entry that httpx cannot parse ("Invalid port").
|
|
12
|
+
_PROXY_KEYS = {"http_proxy", "https_proxy", "all_proxy", "no_proxy"}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _clean_env() -> dict:
|
|
16
|
+
return {k: v for k, v in os.environ.items() if k.lower() not in _PROXY_KEYS}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _exe():
|
|
20
|
+
return find_tool("videocaptioner", "MEDIAKIT_VIDEOCAPTIONER")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def available() -> bool:
|
|
24
|
+
return _exe() is not None
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def polish(
|
|
28
|
+
src,
|
|
29
|
+
out,
|
|
30
|
+
model: str | None = None,
|
|
31
|
+
endpoint: str | None = None,
|
|
32
|
+
api_key: str = "ollama",
|
|
33
|
+
max_cjk: int = 16,
|
|
34
|
+
split: bool = True,
|
|
35
|
+
optimize: bool = True,
|
|
36
|
+
) -> dict:
|
|
37
|
+
exe = _exe()
|
|
38
|
+
if not exe:
|
|
39
|
+
raise RuntimeError(
|
|
40
|
+
"没找到 VideoCaptioner:装好它,或把 MEDIAKIT_VIDEOCAPTIONER 指向 videocaptioner 可执行文件"
|
|
41
|
+
)
|
|
42
|
+
model = model or vlm_model()
|
|
43
|
+
endpoint = endpoint or ollama_base()
|
|
44
|
+
src_path = media_path(src)
|
|
45
|
+
out_path = Path(out).resolve()
|
|
46
|
+
out_path.parent.mkdir(parents=True, exist_ok=True)
|
|
47
|
+
cmd = [
|
|
48
|
+
exe,
|
|
49
|
+
"subtitle",
|
|
50
|
+
str(src_path),
|
|
51
|
+
"--api-base", endpoint,
|
|
52
|
+
"--api-key", api_key,
|
|
53
|
+
"--model", model,
|
|
54
|
+
"-o", str(out_path),
|
|
55
|
+
"--max-cjk", str(max_cjk),
|
|
56
|
+
]
|
|
57
|
+
if not split:
|
|
58
|
+
cmd.append("--no-split")
|
|
59
|
+
if not optimize:
|
|
60
|
+
cmd.append("--no-optimize")
|
|
61
|
+
|
|
62
|
+
p = subprocess.run(
|
|
63
|
+
cmd,
|
|
64
|
+
env=_clean_env(),
|
|
65
|
+
capture_output=True,
|
|
66
|
+
text=True,
|
|
67
|
+
encoding="utf-8",
|
|
68
|
+
errors="replace",
|
|
69
|
+
timeout=1800,
|
|
70
|
+
)
|
|
71
|
+
if p.returncode != 0 or not out_path.exists():
|
|
72
|
+
tail = (p.stderr or p.stdout or "").strip().splitlines()[-4:]
|
|
73
|
+
raise RuntimeError("VideoCaptioner 失败: " + " | ".join(tail))
|
|
74
|
+
|
|
75
|
+
text = out_path.read_text(encoding="utf-8", errors="replace")
|
|
76
|
+
blocks = [b for b in text.replace("\r\n", "\n").split("\n\n") if b.strip() and "-->" in b]
|
|
77
|
+
return {
|
|
78
|
+
"in": str(src_path),
|
|
79
|
+
"out": str(out_path),
|
|
80
|
+
"model": model,
|
|
81
|
+
"endpoint": endpoint,
|
|
82
|
+
"lines": len(blocks),
|
|
83
|
+
"bytes": out_path.stat().st_size,
|
|
84
|
+
"stderr_tail": (p.stderr or "").strip().splitlines()[-2:],
|
|
85
|
+
}
|