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/config.py ADDED
@@ -0,0 +1,68 @@
1
+ """Runtime configuration: tool discovery and local model endpoints.
2
+
3
+ Everything is overridable by environment variable so the package does not
4
+ depend on any particular machine layout.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import os
10
+ import shutil
11
+ from pathlib import Path
12
+
13
+ # tool name -> environment variable that points at the executable
14
+ ENV_VARS = {
15
+ "ffmpeg": "MEDIAKIT_FFMPEG",
16
+ "ffprobe": "MEDIAKIT_FFPROBE",
17
+ "audio-separator": "MEDIAKIT_AUDIO_SEPARATOR",
18
+ "videocaptioner": "MEDIAKIT_VIDEOCAPTIONER",
19
+ }
20
+
21
+
22
+ def find_tool(name: str, env_var: str | None = None):
23
+ """Locate an executable: explicit env var, PATH, then a few common dirs."""
24
+ env_var = env_var or ENV_VARS.get(name)
25
+ if env_var:
26
+ raw = os.environ.get(env_var)
27
+ if raw:
28
+ p = Path(raw).expanduser()
29
+ if p.is_file():
30
+ return str(p)
31
+ # allow pointing at a bin directory
32
+ for candidate in (p / name, p / (name + ".exe")):
33
+ if candidate.is_file():
34
+ return str(candidate)
35
+
36
+ exe = name if name.lower().endswith(".exe") else name + ".exe"
37
+ found = shutil.which(name) or shutil.which(exe)
38
+ if found:
39
+ return found
40
+
41
+ home = Path.home()
42
+ candidates = [
43
+ home / ".dsh" / "bin" / exe,
44
+ home / ".local" / "bin" / name,
45
+ Path("/usr/local/bin") / name,
46
+ Path("/opt/homebrew/bin") / name,
47
+ ]
48
+ for candidate in candidates:
49
+ if candidate.is_file():
50
+ return str(candidate)
51
+ return None
52
+
53
+
54
+ def ollama_base() -> str:
55
+ """OpenAI-compatible base URL, without the trailing /chat/completions."""
56
+ return os.environ.get("MEDIAKIT_OLLAMA_BASE", "http://127.0.0.1:11434/v1").rstrip("/")
57
+
58
+
59
+ def ollama_chat_url() -> str:
60
+ return ollama_base() + "/chat/completions"
61
+
62
+
63
+ def vlm_model() -> str:
64
+ return os.environ.get("MEDIAKIT_VLM_MODEL", "qwen3-vl:4b-instruct")
65
+
66
+
67
+ def whisper_model() -> str:
68
+ return os.environ.get("MEDIAKIT_WHISPER_MODEL", "small")
mediakit_cli/dub.py ADDED
@@ -0,0 +1,174 @@
1
+ from __future__ import annotations
2
+
3
+ import sys
4
+ import tempfile
5
+ import wave
6
+ from pathlib import Path
7
+
8
+ import pysubs2
9
+
10
+ from .util import ffmpeg, media_path
11
+
12
+
13
+ def _require_windows() -> None:
14
+ if sys.platform != "win32":
15
+ raise RuntimeError("dub 依赖 Windows SAPI,只能在 Windows 上运行")
16
+
17
+
18
+ def _wav_duration(path: Path) -> float:
19
+ try:
20
+ with wave.open(str(path), "rb") as w:
21
+ return w.getnframes() / float(w.getframerate() or 1)
22
+ except Exception:
23
+ return 0.0
24
+
25
+
26
+ def list_voices() -> list:
27
+ _require_windows()
28
+ import win32com.client
29
+
30
+ voice = win32com.client.Dispatch("SAPI.SpVoice")
31
+ return [v.GetDescription() for v in voice.GetVoices()]
32
+
33
+
34
+ def _synth(text: str, wav_path: Path, voice_name: str | None = None, rate: int = 0) -> None:
35
+ import win32com.client
36
+
37
+ stream = win32com.client.Dispatch("SAPI.SpFileStream")
38
+ stream.Open(str(wav_path), 3, False) # 3 = SSFMCreateForWrite
39
+ voice = win32com.client.Dispatch("SAPI.SpVoice")
40
+ if voice_name:
41
+ for v in voice.GetVoices():
42
+ if voice_name.lower() in v.GetDescription().lower():
43
+ voice.Voice = v
44
+ break
45
+ voice.Rate = int(max(-10, min(10, rate)))
46
+ voice.AudioOutputStream = stream
47
+ try:
48
+ voice.Speak(text)
49
+ finally:
50
+ stream.Close()
51
+
52
+
53
+ def dub(
54
+ video,
55
+ srt,
56
+ out,
57
+ voice_name: str | None = None,
58
+ rate: int = 0,
59
+ mix_original: bool = False,
60
+ fit: bool = True,
61
+ progress=None,
62
+ ) -> dict:
63
+ note = progress or (lambda *_: None)
64
+ _require_windows()
65
+ src = media_path(video)
66
+ sub_path = media_path(srt)
67
+ subs = pysubs2.load(str(sub_path))
68
+ events = [e for e in subs if e.plaintext.strip()]
69
+ if not events:
70
+ raise RuntimeError("字幕里没有可用文本")
71
+
72
+ tmp = Path(tempfile.mkdtemp(prefix="mkdub_"))
73
+ wavs = []
74
+ squeezed = 0
75
+ max_tempo = 1.0
76
+ try:
77
+ for i, ev in enumerate(events):
78
+ wav = tmp / f"{i:03d}.wav"
79
+ _synth(ev.plaintext.strip(), wav, voice_name, rate)
80
+ slot = max((ev.end - ev.start) / 1000.0, 0.2)
81
+ dur = _wav_duration(wav)
82
+ tempo = 1.0
83
+ if fit and dur > slot * 1.02:
84
+ tempo = min(2.0, max(0.5, dur / slot))
85
+ if tempo > 1.001:
86
+ squeezed += 1
87
+ max_tempo = max(max_tempo, tempo)
88
+ wavs.append((max(ev.start, 0), wav, tempo))
89
+ if i % 10 == 0:
90
+ note(f"合成 {i + 1}/{len(events)}")
91
+
92
+ args = ["-i", str(src)]
93
+ for _, wav, _ in wavs:
94
+ args += ["-i", str(wav)]
95
+
96
+ filters = []
97
+ labels = []
98
+ for i, (start_ms, _, tempo) in enumerate(wavs):
99
+ idx = i + 1
100
+ stretch = f"atempo={tempo:.3f}," if tempo > 1.001 else ""
101
+ filters.append(
102
+ f"[{idx}:a]{stretch}adelay={start_ms}|{start_ms},aresample=48000,"
103
+ f"aformat=sample_fmts=fltp:channel_layouts=stereo[d{i}]"
104
+ )
105
+ labels.append(f"[d{i}]")
106
+
107
+ counter = {"n": 0}
108
+
109
+ def mix(group: list) -> str:
110
+ if len(group) == 1:
111
+ return group[0]
112
+ merged = []
113
+ for gi in range(0, len(group), 20):
114
+ part = group[gi:gi + 20]
115
+ if len(part) == 1:
116
+ merged.append(part[0])
117
+ continue
118
+ counter["n"] += 1
119
+ label = f"[mx{counter['n']}]"
120
+ filters.append(
121
+ f"{''.join(part)}amix=inputs={len(part)}:normalize=0:dropout_transition=0{label}"
122
+ )
123
+ merged.append(label)
124
+ return mix(merged)
125
+
126
+ audio_label = mix(labels)
127
+ if mix_original:
128
+ filters.append(
129
+ "[0:a]volume=0.25,aresample=48000,"
130
+ "aformat=sample_fmts=fltp:channel_layouts=stereo[orig]"
131
+ )
132
+ filters.append(f"{audio_label}[orig]amix=inputs=2:normalize=0[aout]")
133
+ audio_label = "[aout]"
134
+
135
+ from .probe import probe
136
+
137
+ video_dur = float(probe(src).get("duration") or 0)
138
+ if video_dur > 0:
139
+ filters.append(f"{audio_label}apad=whole_dur={video_dur:.3f}[apad]")
140
+ audio_label = "[apad]"
141
+
142
+ cmd = args + [
143
+ "-filter_complex", ";".join(filters),
144
+ "-map", "0:v",
145
+ "-map", audio_label,
146
+ "-c:v", "copy",
147
+ "-c:a", "aac",
148
+ "-b:a", "192k",
149
+ "-movflags", "+faststart",
150
+ str(Path(out).resolve()),
151
+ ]
152
+ note("混音")
153
+ ffmpeg(*cmd, timeout=3600)
154
+ finally:
155
+ for f in tmp.glob("*"):
156
+ try:
157
+ f.unlink()
158
+ except OSError:
159
+ pass
160
+ try:
161
+ tmp.rmdir()
162
+ except OSError:
163
+ pass
164
+
165
+ return {
166
+ "out": str(Path(out).resolve()),
167
+ "lines": len(events),
168
+ "voice": voice_name or "默认",
169
+ "rate": rate,
170
+ "mix_original": mix_original,
171
+ "fit": fit,
172
+ "squeezed": squeezed,
173
+ "max_tempo": round(max_tempo, 2),
174
+ }
mediakit_cli/edit.py ADDED
@@ -0,0 +1,280 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ from pathlib import Path
5
+
6
+ from .probe import probe
7
+ from .util import ffmpeg, media_path
8
+
9
+
10
+ def _prepare_ass(src, width: int, height: int, dst) -> None:
11
+ """把任意 PlayRes 的 ASS 重新按目标尺寸标定,字号随高度等比缩放。"""
12
+ import pysubs2
13
+
14
+ subs = pysubs2.load(str(src))
15
+ try:
16
+ pry = float(subs.info.get("PlayResY") or 0) or 288.0
17
+ except Exception:
18
+ pry = 288.0
19
+ scale = height / pry if pry else 1.0
20
+ if abs(scale - 1.0) > 0.01:
21
+ for st in subs.styles.values():
22
+ st.fontsize = max(12.0, st.fontsize * scale)
23
+ subs.info["PlayResX"] = int(width)
24
+ subs.info["PlayResY"] = int(height)
25
+ subs.save(str(dst))
26
+
27
+
28
+ def load_plan(path) -> dict:
29
+ p = media_path(path)
30
+ plan = json.loads(p.read_text(encoding="utf-8"))
31
+ if not isinstance(plan, dict) or not plan.get("clips"):
32
+ raise ValueError("edit 计划里必须有非空的 clips 数组")
33
+ return plan
34
+
35
+
36
+ def _resolve_clips(clips: list) -> list:
37
+ out = []
38
+ for c in clips:
39
+ src = media_path(c["src"])
40
+ info = probe(src)
41
+ src_dur = float(info.get("duration") or 0)
42
+ start = max(float(c.get("in", 0)), 0.0)
43
+ end = c.get("out")
44
+ end = src_dur if end is None else min(float(end), src_dur)
45
+ out.append(
46
+ {
47
+ "src": str(src),
48
+ "info": info,
49
+ "in": start,
50
+ "out": end,
51
+ "dur": max(end - start, 0.05),
52
+ }
53
+ )
54
+ return out
55
+
56
+
57
+ def _nearest(value: float, beats: list):
58
+ if not beats:
59
+ return None
60
+ best = beats[0]
61
+ for b in beats:
62
+ if abs(b - value) < abs(best - value):
63
+ best = b
64
+ return best
65
+
66
+
67
+ def _snap_to_beats(clips: list, beats: list, tolerance: float = 0.35):
68
+ """把每个片段之间的切点挪到最近的拍上;挪不动(素材不够)就保持原样。"""
69
+ durs = [c["dur"] for c in clips]
70
+ new_durs, cuts, snapped, clamped = [], [0.0], 0, 0
71
+ cum = 0.0
72
+ for i, d in enumerate(durs):
73
+ if i == len(durs) - 1:
74
+ new_durs.append(d)
75
+ cum += d
76
+ cuts.append(round(cum, 3))
77
+ continue
78
+ target = cum + d
79
+ beat = _nearest(target, beats)
80
+ cand = d
81
+ if beat is not None and abs(beat - target) <= tolerance and beat - cum >= 0.4:
82
+ cand = beat - cum
83
+ avail = float(clips[i]["info"].get("duration") or 0) - clips[i]["in"] - 0.02
84
+ if avail > 0 and cand > avail:
85
+ cand = avail
86
+ clamped += 1
87
+ cand = max(cand, 0.2)
88
+ if abs(cand - d) > 1e-6:
89
+ snapped += 1
90
+ new_durs.append(cand)
91
+ cum += cand
92
+ cuts.append(round(cum, 3))
93
+ return new_durs, cuts, snapped, clamped
94
+
95
+
96
+ def render(plan: dict, out_path) -> dict:
97
+ clips = _resolve_clips(plan["clips"])
98
+ width, height = plan.get("size", [1080, 1920])
99
+ fps = int(plan.get("fps", 30))
100
+ crf = int(plan.get("crf", 20))
101
+ preset = plan.get("preset", "medium")
102
+ keep_audio = bool(plan.get("keep_audio", False))
103
+ music = plan.get("music") or None
104
+ subtitle = plan.get("subtitle")
105
+ fit = plan.get("fit", "pad")
106
+ beat_sync = bool(plan.get("beat_sync") or (music or {}).get("beat_sync"))
107
+ log = []
108
+
109
+ beat_info = None
110
+ if beat_sync:
111
+ if not music:
112
+ log.append("beat_sync 需要同时给 music,已忽略")
113
+ else:
114
+ from .music import analyze
115
+
116
+ mres = analyze(music["src"], with_beats=True)
117
+ beats = mres.get("beats") or []
118
+ durs, cuts, snapped, clamped = _snap_to_beats(
119
+ clips, beats, float(plan.get("beat_tolerance", 0.35))
120
+ )
121
+ for clip, d in zip(clips, durs):
122
+ clip["dur"] = d
123
+ beat_info = {
124
+ "source_bpm": mres.get("tempo_bpm"),
125
+ "beats": len(beats),
126
+ "snapped": snapped,
127
+ "clamped": clamped,
128
+ "cuts": cuts,
129
+ }
130
+ log.append(
131
+ f"卡点:{snapped}/{max(len(clips) - 1, 1)} 个切点落在拍上"
132
+ + (f",{clamped} 个因素材不够未能延长" if clamped else "")
133
+ )
134
+
135
+ args: list = []
136
+ filters: list = []
137
+ total = 0.0
138
+ audios_ok = True
139
+
140
+ for i, clip in enumerate(clips):
141
+ dur = clip["dur"]
142
+ total += dur
143
+ args += ["-ss", f"{clip['in']:.3f}", "-t", f"{dur:.3f}", "-i", clip["src"]]
144
+ if fit == "crop":
145
+ filters.append(
146
+ f"[{i}:v]scale={width}:{height}:force_original_aspect_ratio=increase:flags=lanczos,"
147
+ f"crop={width}:{height},setsar=1,fps={fps},format=yuv420p[v{i}]"
148
+ )
149
+ else:
150
+ filters.append(
151
+ f"[{i}:v]scale={width}:{height}:force_original_aspect_ratio=decrease:flags=lanczos,"
152
+ f"pad={width}:{height}:(ow-iw)/2:(oh-ih)/2,setsar=1,fps={fps},format=yuv420p[v{i}]"
153
+ )
154
+ if not clip["info"].get("audio"):
155
+ audios_ok = False
156
+
157
+ n = len(clips)
158
+ durs = [c["dur"] for c in clips]
159
+ transition = plan.get("transition") or {}
160
+ ttype = (transition.get("type") or "").strip().lower()
161
+ tdur = float(transition.get("duration", 0.3) or 0)
162
+ use_xfade = (
163
+ bool(ttype)
164
+ and ttype != "none"
165
+ and n > 1
166
+ and tdur > 0
167
+ and min(durs) > tdur + 0.1
168
+ )
169
+ if ttype and ttype != "none" and not use_xfade:
170
+ log.append(f"转场 {ttype} 未启用:片段太短或参数不合法")
171
+
172
+ if use_xfade:
173
+ offset = durs[0] - tdur
174
+ cur = "[v0]"
175
+ for i in range(1, n):
176
+ label = f"[vx{i}]"
177
+ filters.append(
178
+ f"{cur}[v{i}]xfade=transition={ttype}:duration={tdur:.3f}:offset={offset:.3f}{label}"
179
+ )
180
+ cur = label
181
+ offset += durs[i] - tdur
182
+ vcat = cur
183
+ total = sum(durs) - (n - 1) * tdur
184
+ else:
185
+ filters.append("".join(f"[v{i}]" for i in range(n)) + f"concat=n={n}:v=1:a=0[vcat]")
186
+ vcat = "[vcat]"
187
+ total = sum(durs)
188
+
189
+ if keep_audio and not audios_ok:
190
+ log.append("有片段没有音轨,已忽略 keep_audio")
191
+ keep_audio = False
192
+
193
+ audio_out = None
194
+ if keep_audio:
195
+ for i in range(n):
196
+ filters.append(
197
+ f"[{i}:a]aresample=48000,aformat=sample_fmts=fltp:channel_layouts=stereo[a{i}]"
198
+ )
199
+ if use_xfade:
200
+ cur = "[a0]"
201
+ for i in range(1, n):
202
+ label = f"[ax{i}]"
203
+ filters.append(f"{cur}[a{i}]acrossfade=d={tdur:.3f}:c1=tri:c2=tri{label}")
204
+ cur = label
205
+ audio_out = cur
206
+ else:
207
+ filters.append("".join(f"[a{i}]" for i in range(n)) + f"concat=n={n}:v=0:a=1[acat]")
208
+ audio_out = "[acat]"
209
+
210
+ music_index = n
211
+ if music:
212
+ m_src = media_path(music["src"])
213
+ if music.get("loop", True):
214
+ args += ["-stream_loop", "-1"]
215
+ args += ["-i", str(m_src)]
216
+ gain = float(music.get("gain_db", -14))
217
+ filters.append(
218
+ f"[{music_index}:a]volume={gain}dB,atrim=0:{total:.3f},asetpts=PTS-STARTPTS[ma]"
219
+ )
220
+ if audio_out:
221
+ filters.append(f"{audio_out}[ma]amix=inputs=2:duration=first:dropout_transition=0[aout]")
222
+ else:
223
+ filters.append("[ma]anull[aout]")
224
+ audio_out = "[aout]"
225
+
226
+ vout = vcat
227
+ workdir = None
228
+ if subtitle:
229
+ import shutil
230
+ import tempfile
231
+
232
+ src_sub = media_path(subtitle)
233
+ out_dir = Path(out_path).resolve().parent
234
+ out_dir.mkdir(parents=True, exist_ok=True)
235
+ workdir = tempfile.mkdtemp(prefix="mkburn_", dir=str(out_dir))
236
+ suffix = src_sub.suffix.lower()
237
+ if suffix == ".ass":
238
+ _prepare_ass(src_sub, width, height, Path(workdir) / "burn.ass")
239
+ filters.append(f"{vcat}ass=burn.ass[vsub]")
240
+ else:
241
+ shutil.copy2(src_sub, Path(workdir) / f"burn{suffix}")
242
+ filters.append(f"{vcat}subtitles=burn" + suffix + "[vsub]")
243
+ vout = "[vsub]"
244
+
245
+ cmd = args + ["-filter_complex", ";".join(filters), "-map", vout]
246
+ if audio_out:
247
+ cmd += ["-map", audio_out, "-c:a", "aac", "-b:a", "192k"]
248
+ else:
249
+ cmd += ["-an"]
250
+ cmd += [
251
+ "-c:v", "libx264",
252
+ "-preset", preset,
253
+ "-crf", str(crf),
254
+ "-pix_fmt", "yuv420p",
255
+ "-t", f"{total:.3f}",
256
+ "-movflags", "+faststart",
257
+ str(Path(out_path).resolve()),
258
+ ]
259
+ try:
260
+ ffmpeg(*cmd, timeout=7200, cwd=workdir)
261
+ finally:
262
+ if workdir:
263
+ import shutil as _shutil
264
+
265
+ _shutil.rmtree(workdir, ignore_errors=True)
266
+
267
+ return {
268
+ "out": str(Path(out_path).resolve()),
269
+ "clips": len(clips),
270
+ "duration": round(total, 3),
271
+ "size": [width, height],
272
+ "fps": fps,
273
+ "fit": fit,
274
+ "transition": ttype if use_xfade else None,
275
+ "subtitled": bool(subtitle),
276
+ "music": bool(music),
277
+ "keep_audio": keep_audio,
278
+ "beat_sync": beat_info,
279
+ "notes": log,
280
+ }
mediakit_cli/index.py ADDED
@@ -0,0 +1,101 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import tempfile
5
+ from pathlib import Path
6
+
7
+ import numpy as np
8
+
9
+ from .util import ensure_dir, ffmpeg
10
+
11
+ _MODEL = None
12
+ _PREPROCESS = None
13
+ _TOKENIZER = None
14
+
15
+
16
+ def _load_model():
17
+ global _MODEL, _PREPROCESS, _TOKENIZER
18
+ if _MODEL is None:
19
+ import open_clip
20
+
21
+ model, _, preprocess = open_clip.create_model_and_transforms(
22
+ "ViT-B-32", pretrained="laion2b_s34b_b79k"
23
+ )
24
+ model.eval()
25
+ _MODEL = model
26
+ _PREPROCESS = preprocess
27
+ _TOKENIZER = open_clip.get_tokenizer("ViT-B-32")
28
+ return _MODEL, _PREPROCESS, _TOKENIZER
29
+
30
+
31
+ def build_index(video, out_dir, every: float = 2.0, max_frames: int = 150, progress=None) -> dict:
32
+ import torch
33
+ from PIL import Image
34
+
35
+ note = progress or (lambda *_: None)
36
+ out = ensure_dir(out_dir)
37
+ frames_dir = ensure_dir(out / "frames")
38
+ every = max(float(every), 0.5)
39
+ note(f"抽帧:每 {every}s 一帧,最多 {max_frames} 帧")
40
+ ffmpeg(
41
+ "-i", video,
42
+ "-vf", f"fps={1.0 / every:.6f}",
43
+ "-frames:v", str(max_frames),
44
+ "-q:v", "3",
45
+ str(frames_dir / "%05d.jpg"),
46
+ )
47
+ paths = sorted(frames_dir.glob("*.jpg"))
48
+ if not paths:
49
+ raise RuntimeError("没有抽到帧")
50
+
51
+ note(f"加载 CLIP 模型(首次会下载约 350MB)")
52
+ model, preprocess, _ = _load_model()
53
+ note(f"编码 {len(paths)} 帧")
54
+ chunks = []
55
+ with torch.no_grad():
56
+ for i in range(0, len(paths), 8):
57
+ batch = [preprocess(Image.open(p).convert("RGB")) for p in paths[i:i + 8]]
58
+ emb = model.encode_image(torch.stack(batch))
59
+ emb = emb / emb.norm(dim=-1, keepdim=True)
60
+ chunks.append(emb.cpu().numpy().astype(np.float32))
61
+ if i % 32 == 0:
62
+ note(f"编码 {min(i + 8, len(paths))}/{len(paths)}")
63
+ embeds = np.concatenate(chunks, axis=0)
64
+ times = np.array([round(i * every, 3) for i in range(len(paths))], dtype=np.float32)
65
+
66
+ np.savez_compressed(out / "index.npz", embeds=embeds, times=times)
67
+ meta = {
68
+ "video": str(Path(video).resolve()),
69
+ "frames": len(paths),
70
+ "every": every,
71
+ "model": "ViT-B-32/laion2b_s34b_b79k",
72
+ "thumbs": [str(p) for p in paths],
73
+ }
74
+ (out / "index.json").write_text(json.dumps(meta, ensure_ascii=False, indent=2), encoding="utf-8")
75
+ return {"index": str(out / "index.npz"), "frames": len(paths), "every": every, "model": meta["model"]}
76
+
77
+
78
+ def search(index_dir, query: str, topk: int = 5) -> dict:
79
+ import torch
80
+
81
+ d = Path(index_dir)
82
+ data = np.load(d / "index.npz")
83
+ meta = json.loads((d / "index.json").read_text(encoding="utf-8"))
84
+ model, _, tokenizer = _load_model()
85
+ with torch.no_grad():
86
+ tokens = tokenizer([query])
87
+ text = model.encode_text(tokens)
88
+ text = text / text.norm(dim=-1, keepdim=True)
89
+ sims = data["embeds"] @ text.cpu().numpy().T
90
+ sims = sims.reshape(-1)
91
+ order = np.argsort(-sims)[: max(1, topk)]
92
+ hits = []
93
+ for idx in order:
94
+ hits.append(
95
+ {
96
+ "t": round(float(data["times"][idx]), 2),
97
+ "score": round(float(sims[idx]), 4),
98
+ "thumb": meta["thumbs"][int(idx)] if int(idx) < len(meta["thumbs"]) else None,
99
+ }
100
+ )
101
+ return {"query": query, "video": meta.get("video"), "hits": hits, "frames": meta.get("frames")}