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/__init__.py
ADDED
mediakit_cli/__main__.py
ADDED
mediakit_cli/auto.py
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import random
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from .edit import render
|
|
8
|
+
from .music import analyze
|
|
9
|
+
from .util import ensure_dir, media_path
|
|
10
|
+
|
|
11
|
+
VIDEO_EXT = {".mp4", ".mov", ".mkv", ".webm", ".avi", ".m4v"}
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def collect_sources(srcs) -> list:
|
|
15
|
+
files = []
|
|
16
|
+
for raw in srcs:
|
|
17
|
+
p = Path(raw).expanduser()
|
|
18
|
+
if p.is_dir():
|
|
19
|
+
files += [f for f in sorted(p.rglob("*")) if f.suffix.lower() in VIDEO_EXT]
|
|
20
|
+
elif p.suffix.lower() in VIDEO_EXT:
|
|
21
|
+
files.append(media_path(p))
|
|
22
|
+
# 去重且保持顺序
|
|
23
|
+
seen, out = set(), []
|
|
24
|
+
for f in files:
|
|
25
|
+
key = str(f.resolve()).lower()
|
|
26
|
+
if key not in seen:
|
|
27
|
+
seen.add(key)
|
|
28
|
+
out.append(f.resolve())
|
|
29
|
+
return out
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def pace_map(sections: list, default_k: int = 4, k_fast: int = 3, k_slow: int = 8) -> list:
|
|
33
|
+
"""按段落能量定每段的“几拍一切”:能量越高切得越密。
|
|
34
|
+
|
|
35
|
+
能量差小于 15% 时视为同一档,避免两段几乎一样响却切出 6 拍 vs 2 拍。
|
|
36
|
+
"""
|
|
37
|
+
if not sections or len(sections) < 2:
|
|
38
|
+
return []
|
|
39
|
+
energies = [float(s.get("energy") or 0) for s in sections]
|
|
40
|
+
lo, hi = min(energies), max(energies)
|
|
41
|
+
spread = (hi - lo) / max(hi, 1e-9)
|
|
42
|
+
uniform = spread < 0.15
|
|
43
|
+
out = []
|
|
44
|
+
for s, e in zip(sections, energies):
|
|
45
|
+
if uniform:
|
|
46
|
+
k = default_k
|
|
47
|
+
else:
|
|
48
|
+
norm = (e - lo) / (hi - lo) if hi > lo else 0.5
|
|
49
|
+
k = int(round(k_slow + (k_fast - k_slow) * norm))
|
|
50
|
+
out.append(
|
|
51
|
+
{
|
|
52
|
+
"start": s["start"],
|
|
53
|
+
"end": s["end"],
|
|
54
|
+
"energy": e,
|
|
55
|
+
"beats_per_cut": max(1, min(k, k_slow)),
|
|
56
|
+
}
|
|
57
|
+
)
|
|
58
|
+
return out
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def cut_points(
|
|
62
|
+
beats: list,
|
|
63
|
+
duration: float,
|
|
64
|
+
beats_per_cut: int,
|
|
65
|
+
pmap: list | None = None,
|
|
66
|
+
fallback: float = 2.5,
|
|
67
|
+
) -> list:
|
|
68
|
+
if not beats:
|
|
69
|
+
n = max(1, int(duration // fallback))
|
|
70
|
+
return [round(i * fallback, 3) for i in range(n + 1)] + [round(duration, 3)]
|
|
71
|
+
cuts = [0.0]
|
|
72
|
+
if pmap:
|
|
73
|
+
for sec in pmap:
|
|
74
|
+
k = int(sec["beats_per_cut"])
|
|
75
|
+
bs = [b for b in beats if sec["start"] <= b < sec["end"]]
|
|
76
|
+
for i, b in enumerate(bs):
|
|
77
|
+
if (i + 1) % k == 0 and b > cuts[-1] + 0.4 and b < duration - 0.4:
|
|
78
|
+
cuts.append(round(b, 3))
|
|
79
|
+
else:
|
|
80
|
+
for i, b in enumerate(beats):
|
|
81
|
+
if (i + 1) % max(1, beats_per_cut) == 0 and b > cuts[-1] + 0.4 and b < duration - 0.4:
|
|
82
|
+
cuts.append(round(b, 3))
|
|
83
|
+
cuts.append(round(duration, 3))
|
|
84
|
+
return cuts
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def build_pool(files: list, limit: int, min_shot: float, progress) -> list:
|
|
88
|
+
from .shots import detect_shots
|
|
89
|
+
|
|
90
|
+
pool = []
|
|
91
|
+
for idx, f in enumerate(files[:limit], 1):
|
|
92
|
+
try:
|
|
93
|
+
shots = detect_shots(f)
|
|
94
|
+
except Exception as exc:
|
|
95
|
+
progress(f"跳过 {f.name}: {exc}")
|
|
96
|
+
continue
|
|
97
|
+
kept = [s for s in shots if s["dur"] >= min_shot]
|
|
98
|
+
progress(f"[{idx}/{min(len(files), limit)}] {f.name}: {len(shots)} 镜头,{len(kept)} 个可用")
|
|
99
|
+
for s in kept:
|
|
100
|
+
pool.append({"src": str(f), "start": s["start"], "end": s["end"], "dur": s["dur"]})
|
|
101
|
+
return pool
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def select_clips(cuts: list, pool: list, seed: int, progress) -> tuple:
|
|
105
|
+
if not pool:
|
|
106
|
+
raise RuntimeError("素材里没有可用镜头:检查 --src 路径或降低 --min-shot")
|
|
107
|
+
rng = random.Random(seed)
|
|
108
|
+
clips, used, last_key = [], set(), None
|
|
109
|
+
clamped, reused = 0, 0
|
|
110
|
+
cursor = 0.0
|
|
111
|
+
for cut in cuts[1:]:
|
|
112
|
+
need = cut - cursor
|
|
113
|
+
if need < 0.4:
|
|
114
|
+
continue
|
|
115
|
+
cands = [s for s in pool if s["dur"] >= need - 1e-3]
|
|
116
|
+
if not cands:
|
|
117
|
+
cands = [max(pool, key=lambda s: s["dur"])]
|
|
118
|
+
clamped += 1
|
|
119
|
+
fresh = [s for s in cands if (s["src"], s["start"]) not in used]
|
|
120
|
+
avoid_last = [s for s in (fresh or cands) if (s["src"], s["start"]) != last_key]
|
|
121
|
+
pick_pool = avoid_last or fresh or cands
|
|
122
|
+
shot = rng.choice(pick_pool)
|
|
123
|
+
if shot["dur"] < need:
|
|
124
|
+
need = shot["dur"]
|
|
125
|
+
key = (shot["src"], shot["start"])
|
|
126
|
+
if key in used:
|
|
127
|
+
reused += 1
|
|
128
|
+
used.add(key)
|
|
129
|
+
last_key = key
|
|
130
|
+
hi = max(shot["start"], shot["end"] - need)
|
|
131
|
+
in_t = rng.uniform(shot["start"], hi)
|
|
132
|
+
clips.append(
|
|
133
|
+
{
|
|
134
|
+
"src": shot["src"],
|
|
135
|
+
"in": round(in_t, 3),
|
|
136
|
+
"out": round(in_t + need, 3),
|
|
137
|
+
}
|
|
138
|
+
)
|
|
139
|
+
cursor += need
|
|
140
|
+
progress(f"选了 {len(clips)} 段,复用 {reused} 次,素材不够被压短 {clamped} 次")
|
|
141
|
+
return clips, {"reused": reused, "clamped": clamped, "filled": round(cursor, 3)}
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def generate(
|
|
145
|
+
music,
|
|
146
|
+
srcs,
|
|
147
|
+
out,
|
|
148
|
+
size=(1080, 1920),
|
|
149
|
+
fps: int = 30,
|
|
150
|
+
fit: str = "crop",
|
|
151
|
+
beats_per_cut: int = 4,
|
|
152
|
+
pace: str = "auto",
|
|
153
|
+
transition: str = "none",
|
|
154
|
+
transition_duration: float = 0.25,
|
|
155
|
+
max_sources: int = 12,
|
|
156
|
+
min_shot: float = 0.8,
|
|
157
|
+
seed: int = 7,
|
|
158
|
+
gain_db: float = -14.0,
|
|
159
|
+
plan_only: bool = False,
|
|
160
|
+
progress=None,
|
|
161
|
+
) -> dict:
|
|
162
|
+
note = progress or (lambda *_: None)
|
|
163
|
+
m_path = media_path(music)
|
|
164
|
+
note(f"分析音乐 {m_path.name}")
|
|
165
|
+
m = analyze(m_path, with_beats=True)
|
|
166
|
+
pmap = pace_map(m.get("sections") or [], default_k=beats_per_cut) if pace == "auto" else []
|
|
167
|
+
cuts = cut_points(m["beats"], m["duration"], beats_per_cut, pmap=pmap)
|
|
168
|
+
if pace == "auto" and len(cuts) < 3:
|
|
169
|
+
cuts = cut_points(m["beats"], m["duration"], beats_per_cut)
|
|
170
|
+
pmap = []
|
|
171
|
+
note("段落变化不足,退回固定拍数")
|
|
172
|
+
note(f"{m['tempo_bpm']} BPM,{len(m['beats'])} 拍,切点 {len(cuts) - 1} 个,节奏 {pace}")
|
|
173
|
+
|
|
174
|
+
files = collect_sources(srcs)
|
|
175
|
+
note(f"素材 {len(files)} 个文件,本次扫描前 {min(len(files), max_sources)} 个")
|
|
176
|
+
pool = build_pool(files, max_sources, min_shot, note)
|
|
177
|
+
clips, stats = select_clips(cuts, pool, seed, note)
|
|
178
|
+
|
|
179
|
+
plan = {
|
|
180
|
+
"size": list(size),
|
|
181
|
+
"fps": fps,
|
|
182
|
+
"fit": fit,
|
|
183
|
+
"crf": 20,
|
|
184
|
+
"clips": clips,
|
|
185
|
+
"music": {"src": str(m_path), "gain_db": gain_db, "loop": True},
|
|
186
|
+
"keep_audio": False,
|
|
187
|
+
}
|
|
188
|
+
if transition and transition != "none":
|
|
189
|
+
plan["transition"] = {"type": transition, "duration": transition_duration}
|
|
190
|
+
out_path = Path(out).resolve()
|
|
191
|
+
ensure_dir(out_path.parent)
|
|
192
|
+
plan_path = out_path.with_suffix(out_path.suffix + ".plan.json")
|
|
193
|
+
plan_path.write_text(json.dumps(plan, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
194
|
+
note(f"计划已写入 {plan_path}")
|
|
195
|
+
|
|
196
|
+
result = {
|
|
197
|
+
"out": None,
|
|
198
|
+
"plan": str(plan_path),
|
|
199
|
+
"duration": m["duration"],
|
|
200
|
+
"tempo_bpm": m["tempo_bpm"],
|
|
201
|
+
"beats": len(m["beats"]),
|
|
202
|
+
"cuts": cuts,
|
|
203
|
+
"segments": len(clips),
|
|
204
|
+
"sources": len(files),
|
|
205
|
+
"pool_shots": len(pool),
|
|
206
|
+
"seed": seed,
|
|
207
|
+
"stats": stats,
|
|
208
|
+
"pace": {"mode": pace, "sections": pmap},
|
|
209
|
+
"transition": transition if transition != "none" else None,
|
|
210
|
+
}
|
|
211
|
+
if plan_only:
|
|
212
|
+
return result
|
|
213
|
+
|
|
214
|
+
note("开始渲染")
|
|
215
|
+
rendered = render(plan, out_path)
|
|
216
|
+
result.update({"out": rendered["out"], "duration": rendered["duration"]})
|
|
217
|
+
return result
|