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/probe.py ADDED
@@ -0,0 +1,89 @@
1
+ from __future__ import annotations
2
+
3
+ from pathlib import Path
4
+
5
+ from .util import ffprobe_json, media_path
6
+
7
+
8
+ def _fraction_to_float(text: str) -> float:
9
+ try:
10
+ num, _, den = (text or "0/1").partition("/")
11
+ n, d = float(num or 0), float(den or 1)
12
+ return n / d if d else 0.0
13
+ except Exception:
14
+ return 0.0
15
+
16
+
17
+ def _probe_av(path: Path) -> dict:
18
+ import av
19
+
20
+ with av.open(str(path)) as container:
21
+ duration = float(container.duration / av.time_base) if container.duration else 0.0
22
+ video = audio = None
23
+ for st in container.streams:
24
+ if st.type == "video" and video is None:
25
+ video = {
26
+ "codec": st.codec_context.name,
27
+ "width": st.codec_context.width,
28
+ "height": st.codec_context.height,
29
+ "fps": round(float(st.average_rate or 0), 3),
30
+ "pix_fmt": st.codec_context.pix_fmt,
31
+ "frames": int(st.frames or 0),
32
+ }
33
+ elif st.type == "audio" and audio is None:
34
+ audio = {
35
+ "codec": st.codec_context.name,
36
+ "sample_rate": st.codec_context.sample_rate,
37
+ "channels": st.codec_context.channels,
38
+ }
39
+ return {
40
+ "path": str(path),
41
+ "duration": round(duration, 3),
42
+ "size_bytes": path.stat().st_size,
43
+ "format": None,
44
+ "bit_rate": 0,
45
+ "video": video,
46
+ "audio": audio,
47
+ }
48
+
49
+
50
+ def probe(path) -> dict:
51
+ p = media_path(path)
52
+ data = ffprobe_json(p)
53
+ if not data:
54
+ return _probe_av(p)
55
+
56
+ fmt = data.get("format", {}) or {}
57
+ streams = data.get("streams", []) or []
58
+ video = next((s for s in streams if s.get("codec_type") == "video"), None)
59
+ audio = next((s for s in streams if s.get("codec_type") == "audio"), None)
60
+
61
+ result = {
62
+ "path": str(p),
63
+ "duration": round(float(fmt.get("duration") or 0), 3),
64
+ "size_bytes": int(fmt.get("size") or p.stat().st_size),
65
+ "format": fmt.get("format_name"),
66
+ "bit_rate": int(fmt.get("bit_rate") or 0),
67
+ "video": None,
68
+ "audio": None,
69
+ }
70
+ if video:
71
+ result["video"] = {
72
+ "codec": video.get("codec_name"),
73
+ "width": video.get("width"),
74
+ "height": video.get("height"),
75
+ "fps": round(_fraction_to_float(video.get("avg_frame_rate") or video.get("r_frame_rate")), 3),
76
+ "pix_fmt": video.get("pix_fmt"),
77
+ "frames": int(video.get("nb_frames") or 0),
78
+ }
79
+ if audio:
80
+ result["audio"] = {
81
+ "codec": audio.get("codec_name"),
82
+ "sample_rate": int(audio.get("sample_rate") or 0),
83
+ "channels": int(audio.get("channels") or 0),
84
+ }
85
+ return result
86
+
87
+
88
+ def has_audio(info: dict) -> bool:
89
+ return bool(info.get("audio"))
@@ -0,0 +1,133 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import math
5
+ from pathlib import Path
6
+
7
+ import cv2
8
+ import numpy as np
9
+
10
+ from .probe import probe
11
+ from .shots import detect_shots
12
+ from .util import ensure_dir
13
+
14
+
15
+ def _frames_at(cap, times: list) -> list:
16
+ out = []
17
+ for t in times:
18
+ cap.set(cv2.CAP_PROP_POS_MSEC, max(t, 0) * 1000.0)
19
+ ok, frame = cap.read()
20
+ if not ok:
21
+ continue
22
+ out.append((t, cv2.resize(frame, (320, 180), interpolation=cv2.INTER_AREA)))
23
+ return out
24
+
25
+
26
+ def _dominant_colors(frame_bgr, k: int = 3) -> list:
27
+ small = cv2.resize(frame_bgr, (80, 45), interpolation=cv2.INTER_AREA)
28
+ pixels = small.reshape(-1, 3).astype(np.float32)
29
+ if len(pixels) < k:
30
+ return []
31
+ criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 12, 1.0)
32
+ _, labels, centers = cv2.kmeans(pixels, k, None, criteria, 3, cv2.KMEANS_PP_CENTERS)
33
+ counts = np.bincount(labels.flatten(), minlength=k)
34
+ order = np.argsort(-counts)
35
+ colors = []
36
+ for idx in order:
37
+ b, g, r = [int(round(v)) for v in centers[idx]]
38
+ colors.append({"hex": f"#{r:02x}{g:02x}{b:02x}", "ratio": round(float(counts[idx]) / counts.sum(), 3)})
39
+ return colors
40
+
41
+
42
+ def _motion(frames: list) -> dict:
43
+ if len(frames) < 2:
44
+ return {"magnitude": 0.0, "angle": 0.0, "label": "static"}
45
+ mags, angles = [], []
46
+ prev = cv2.cvtColor(frames[0][1], cv2.COLOR_BGR2GRAY)
47
+ for _, frame in frames[1:]:
48
+ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
49
+ flow = cv2.calcOpticalFlowFarneback(prev, gray, None, 0.5, 3, 15, 3, 5, 1.2, 0)
50
+ dx, dy = flow[..., 0], flow[..., 1]
51
+ mag = float(np.mean(np.sqrt(dx * dx + dy * dy)))
52
+ ang = float(math.degrees(math.atan2(float(np.mean(dy)), float(np.mean(dx)))))
53
+ mags.append(mag)
54
+ angles.append(ang)
55
+ prev = gray
56
+ m = float(np.mean(mags)) if mags else 0.0
57
+ label = "static" if m < 0.35 else ("slow" if m < 1.6 else "fast")
58
+ return {"magnitude": round(m, 2), "angle": round(float(np.mean(angles)), 1), "label": label}
59
+
60
+
61
+ def analyze(
62
+ path,
63
+ out_dir,
64
+ max_shots: int | None = None,
65
+ samples: int = 5,
66
+ use_vlm: bool = False,
67
+ vlm_max: int = 12,
68
+ vlm_model: str = "qwen3-vl:4b-instruct",
69
+ progress=None,
70
+ ) -> dict:
71
+ note = progress or (lambda *_: None)
72
+ info = probe(path)
73
+ shots = detect_shots(path)
74
+ if max_shots:
75
+ shots = shots[:max_shots]
76
+ out = ensure_dir(out_dir)
77
+ thumb_dir = ensure_dir(out / "shots")
78
+ cap = cv2.VideoCapture(str(path))
79
+ results = []
80
+ try:
81
+ for shot in shots:
82
+ start, end = shot["start"], shot["end"]
83
+ span = max(end - start, 0.05)
84
+ times = [start + span * (i + 0.5) / samples for i in range(samples)]
85
+ frames = _frames_at(cap, times)
86
+ if not frames:
87
+ continue
88
+ mid = frames[len(frames) // 2][1]
89
+ hsv = cv2.cvtColor(mid, cv2.COLOR_BGR2HSV)
90
+ h, s, v = cv2.split(hsv)
91
+ thumb_path = thumb_dir / f"shot{shot['index']:03d}.jpg"
92
+ cv2.imwrite(str(thumb_path), cv2.resize(mid, (640, 360)), [int(cv2.IMWRITE_JPEG_QUALITY), 85])
93
+ entry = {
94
+ "index": shot["index"],
95
+ "start": start,
96
+ "end": end,
97
+ "dur": round(span, 3),
98
+ "brightness": round(float(v.mean()) / 255.0, 3),
99
+ "saturation": round(float(s.mean()) / 255.0, 3),
100
+ "contrast": round(float(v.std()) / 255.0, 3),
101
+ "colors": _dominant_colors(mid),
102
+ "motion": _motion(frames),
103
+ "thumb": str(thumb_path),
104
+ }
105
+ if use_vlm and len(results) < vlm_max:
106
+ from .vlm import describe_image
107
+
108
+ try:
109
+ entry["vlm"] = describe_image(thumb_path, model=vlm_model)
110
+ note(f"shot {shot['index']}: {entry['vlm'].get('caption', entry['vlm'].get('raw', ''))}")
111
+ except Exception as exc:
112
+ entry["vlm"] = {"error": str(exc)}
113
+ note(f"shot {shot['index']} VLM 失败: {exc}")
114
+ results.append(entry)
115
+ note(f"shot {shot['index']}/{len(shots)}")
116
+ finally:
117
+ cap.release()
118
+
119
+ card = {
120
+ "path": str(Path(path).resolve()),
121
+ "duration": info.get("duration"),
122
+ "count": len(results),
123
+ "summary": {
124
+ "avg_brightness": round(float(np.mean([r["brightness"] for r in results])), 3) if results else 0,
125
+ "avg_saturation": round(float(np.mean([r["saturation"] for r in results])), 3) if results else 0,
126
+ "static_shots": sum(1 for r in results if r["motion"]["label"] == "static"),
127
+ "moving_shots": sum(1 for r in results if r["motion"]["label"] != "static"),
128
+ },
129
+ "shots": results,
130
+ "note": "数值来自像素统计;景别/机位/情绪这类语义需要多模态模型看 shots/*.jpg",
131
+ }
132
+ (out / "shotcard.json").write_text(json.dumps(card, ensure_ascii=False, indent=2), encoding="utf-8")
133
+ return card
mediakit_cli/shots.py ADDED
@@ -0,0 +1,109 @@
1
+ from __future__ import annotations
2
+
3
+ import math
4
+ from pathlib import Path
5
+
6
+ from .probe import probe
7
+ from .util import ensure_dir, grab_frame
8
+
9
+ import cv2
10
+ import numpy as np
11
+
12
+
13
+ def detect_shots(path, threshold: float = 27.0, min_seconds: float = 0.5) -> list:
14
+ from scenedetect import ContentDetector, SceneManager, open_video
15
+
16
+ info = probe(path)
17
+ fps = (info.get("video") or {}).get("fps") or 30.0
18
+ duration = info.get("duration") or 0.0
19
+
20
+ video = open_video(str(path))
21
+ manager = SceneManager()
22
+ manager.add_detector(
23
+ ContentDetector(threshold=float(threshold), min_scene_len=max(1, int(fps * min_seconds)))
24
+ )
25
+ manager.detect_scenes(video)
26
+ scene_list = manager.get_scene_list()
27
+
28
+ shots = []
29
+ for i, (start, end) in enumerate(scene_list, 1):
30
+ s = round(start.get_seconds(), 3)
31
+ e = round(end.get_seconds(), 3)
32
+ shots.append({"index": i, "start": s, "end": e, "dur": round(max(e - s, 0.0), 3)})
33
+
34
+ if not shots:
35
+ shots = [{"index": 1, "start": 0.0, "end": round(duration, 3), "dur": round(duration, 3)}]
36
+ return shots
37
+
38
+
39
+ def _label(img, text: str) -> None:
40
+ cv2.rectangle(img, (0, 0), (img.shape[1], 26), (0, 0, 0), -1)
41
+ cv2.putText(img, text, (6, 18), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
42
+
43
+
44
+ def contact_sheet(path, shots: list, dst, limit: int = 9, thumb_w: int = 320) -> Path:
45
+ dst = Path(dst)
46
+ ensure_dir(dst.parent)
47
+ picked = shots
48
+ if limit and len(shots) > limit:
49
+ step = len(shots) / float(limit)
50
+ picked = [shots[min(int(i * step), len(shots) - 1)] for i in range(limit)]
51
+
52
+ thumbs = []
53
+ tmp_dir = ensure_dir(dst.parent / "_tmp_frames")
54
+ for shot in picked:
55
+ mid = (shot["start"] + shot["end"]) / 2.0
56
+ frame_path = tmp_dir / f"f{shot['index']:04d}.jpg"
57
+ try:
58
+ grab_frame(path, mid, frame_path, width=thumb_w)
59
+ img = cv2.imread(str(frame_path))
60
+ except Exception:
61
+ img = None
62
+ if img is None:
63
+ img = np.zeros((int(thumb_w * 9 / 16), thumb_w, 3), dtype=np.uint8)
64
+ img = cv2.resize(img, (thumb_w, int(thumb_w * 9 / 16)), interpolation=cv2.INTER_AREA)
65
+ _label(img, f"{shot['index']:02d} {shot['start']:.2f}-{shot['end']:.2f}s")
66
+ thumbs.append(img)
67
+
68
+ if not thumbs:
69
+ raise RuntimeError("没有可用帧,无法生成联系表")
70
+
71
+ cols = 3
72
+ rows = math.ceil(len(thumbs) / cols)
73
+ h, w = thumbs[0].shape[:2]
74
+ sheet = np.full((rows * h, cols * w, 3), 24, dtype=np.uint8)
75
+ for i, img in enumerate(thumbs):
76
+ r, c = divmod(i, cols)
77
+ sheet[r * h:(r + 1) * h, c * w:(c + 1) * w] = img[:h, :w]
78
+ cv2.imwrite(str(dst), sheet, [int(cv2.IMWRITE_JPEG_QUALITY), 88])
79
+ for f in tmp_dir.glob("*.jpg"):
80
+ try:
81
+ f.unlink()
82
+ except OSError:
83
+ pass
84
+ try:
85
+ tmp_dir.rmdir()
86
+ except OSError:
87
+ pass
88
+ return dst
89
+
90
+
91
+ def write_otio(path, shots: list, dst) -> Path:
92
+ import opentimelineio as otio
93
+
94
+ dst = Path(dst)
95
+ timeline = otio.schema.Timeline(name=Path(path).stem)
96
+ track = otio.schema.Track(kind=otio.schema.TrackKind.Video)
97
+ for shot in shots:
98
+ clip = otio.schema.Clip(
99
+ name=f"shot{shot['index']:02d}",
100
+ source_range=otio.opentime.TimeRange(
101
+ otio.opentime.RationalTime(shot["start"], 1),
102
+ otio.opentime.RationalTime(shot["dur"], 1),
103
+ ),
104
+ media_reference=otio.schema.ExternalReference(target_url=str(Path(path).resolve())),
105
+ )
106
+ track.append(clip)
107
+ timeline.tracks.append(track)
108
+ otio.adapters.write_to_file(timeline, str(dst))
109
+ return dst
mediakit_cli/subs.py ADDED
@@ -0,0 +1,349 @@
1
+ from __future__ import annotations
2
+
3
+ import math
4
+ import re
5
+ from pathlib import Path
6
+
7
+ import pysubs2
8
+
9
+ from .util import ensure_dir, media_path
10
+
11
+ _CJK = re.compile(r"[\u3400-\u9fff\uf900-\ufaff]")
12
+ _END_PUNCT = "。!?!?…"
13
+ _MID_PUNCT = ",,、;;::"
14
+
15
+ _MODELS: dict = {}
16
+ _CC = None
17
+
18
+
19
+ def _to_simplified(text: str) -> str:
20
+ global _CC
21
+ if not text:
22
+ return text
23
+ if _CC is None:
24
+ try:
25
+ from opencc import OpenCC
26
+
27
+ _CC = OpenCC("t2s")
28
+ except Exception:
29
+ _CC = False
30
+ if not _CC:
31
+ return text
32
+ try:
33
+ return _CC.convert(text)
34
+ except Exception:
35
+ return text
36
+
37
+
38
+ def display_len(text: str) -> float:
39
+ n = 0.0
40
+ for ch in text or "":
41
+ if ch.isspace():
42
+ continue
43
+ if _CJK.match(ch):
44
+ n += 1.0
45
+ elif ch in _END_PUNCT or ch in _MID_PUNCT:
46
+ n += 0.3
47
+ else:
48
+ n += 0.55
49
+ return n
50
+
51
+
52
+ def _word_text(word) -> str:
53
+ return getattr(word, "word", "") or getattr(word, "text", "") or ""
54
+
55
+
56
+ def get_model(size: str = "small", device: str = "cpu"):
57
+ key = (size, device)
58
+ if key not in _MODELS:
59
+ from faster_whisper import WhisperModel
60
+
61
+ _MODELS[key] = WhisperModel(size, device=device, compute_type="int8")
62
+ return _MODELS[key]
63
+
64
+
65
+ def transcribe(path, language: str | None = None, model: str = "small", vad: bool = True) -> dict:
66
+ p = media_path(path)
67
+ m = get_model(model)
68
+ prompt = None
69
+ if language and language.lower().startswith("zh"):
70
+ prompt = "以下是简体中文普通话的句子。"
71
+ segments, info = m.transcribe(
72
+ str(p),
73
+ language=language,
74
+ vad_filter=vad,
75
+ beam_size=5,
76
+ word_timestamps=True,
77
+ initial_prompt=prompt,
78
+ )
79
+ seg_list, words = [], []
80
+ for seg in segments:
81
+ text = _to_simplified((seg.text or "").strip())
82
+ seg_list.append({"start": round(seg.start, 3), "end": round(seg.end, 3), "text": text})
83
+ for w in getattr(seg, "words", None) or []:
84
+ wt = _to_simplified(_word_text(w))
85
+ if not wt.strip():
86
+ continue
87
+ words.append({"start": round(w.start, 3), "end": round(w.end, 3), "text": wt})
88
+ return {
89
+ "path": str(p),
90
+ "language": info.language,
91
+ "language_probability": round(float(info.language_probability or 0), 3),
92
+ "duration": round(float(info.duration or 0), 3),
93
+ "segments": seg_list,
94
+ "words": words,
95
+ }
96
+
97
+
98
+ def _mk_line(words: list) -> dict:
99
+ text = "".join(w["text"] for w in words).strip()
100
+ return {
101
+ "text": text,
102
+ "start": round(words[0]["start"], 3),
103
+ "end": round(words[-1]["end"], 3),
104
+ "words": list(words),
105
+ }
106
+
107
+
108
+ def lines_from_words(
109
+ words: list,
110
+ max_chars: float = 16.0,
111
+ max_dur: float = 6.0,
112
+ min_dur: float = 0.6,
113
+ pause: float = 0.28,
114
+ min_chars: float = 4.0,
115
+ ) -> list:
116
+ if not words:
117
+ return []
118
+ lines, cur = [], []
119
+ for i, w in enumerate(words):
120
+ cur.append(w)
121
+ nxt = words[i + 1] if i + 1 < len(words) else None
122
+ text = "".join(x["text"] for x in cur)
123
+ ended = bool(re.search(r"[。!?!?…,,、;;::]$", w["text"].strip()))
124
+ gap = (nxt["start"] - w["end"]) if nxt else 999.0
125
+ if nxt is None or display_len(text) >= max_chars or gap >= pause or ended:
126
+ lines.append(_mk_line(cur))
127
+ cur = []
128
+
129
+ merged = []
130
+ for ln in lines:
131
+ if (
132
+ merged
133
+ and display_len(merged[-1]["text"]) < min_chars
134
+ and display_len(merged[-1]["text"] + ln["text"]) <= max_chars
135
+ ):
136
+ words_join = merged[-1]["words"] + ln["words"]
137
+ merged[-1] = _mk_line(words_join)
138
+ else:
139
+ merged.append(ln)
140
+
141
+ fixed = []
142
+ for ln in merged:
143
+ dur = ln["end"] - ln["start"]
144
+ if dur > max_dur and len(ln["words"]) >= 2:
145
+ cuts = max(2, int(dur // max_dur) + 1)
146
+ per = max(1, math.ceil(len(ln["words"]) / cuts))
147
+ for k in range(0, len(ln["words"]), per):
148
+ grp = ln["words"][k:k + per]
149
+ if grp:
150
+ fixed.append(_mk_line(grp))
151
+ else:
152
+ fixed.append(ln)
153
+
154
+ for i, ln in enumerate(fixed):
155
+ if i and ln["start"] < fixed[i - 1]["end"]:
156
+ ln["start"] = round(fixed[i - 1]["end"] + 0.02, 3)
157
+ if ln["end"] - ln["start"] < min_dur:
158
+ ln["end"] = round(ln["start"] + min_dur, 3)
159
+ return [ln for ln in fixed if ln["text"].strip()]
160
+
161
+
162
+ def lines_from_segments(segments: list, max_chars: float = 16.0, max_dur: float = 6.0) -> list:
163
+ out = []
164
+ for seg in segments:
165
+ text = (seg.get("text") or "").strip()
166
+ if not text:
167
+ continue
168
+ dur = max(seg["end"] - seg["start"], 0.3)
169
+ total = max(display_len(text), 0.5)
170
+ buf, acc = "", 0.0
171
+ pieces = []
172
+ for ch in text:
173
+ buf += ch
174
+ acc += display_len(ch)
175
+ if acc >= max_chars or (ch in _END_PUNCT and acc >= max_chars * 0.5):
176
+ pieces.append((buf, acc))
177
+ buf, acc = "", 0.0
178
+ if buf:
179
+ pieces.append((buf, acc))
180
+ weights = [max(p[1], 0.5) for p in pieces] or [1.0]
181
+ wsum = sum(weights)
182
+ cursor = seg["start"]
183
+ for piece, w in zip(pieces, weights):
184
+ d = dur * (w / wsum)
185
+ out.append(
186
+ {
187
+ "text": piece.strip(),
188
+ "start": round(cursor, 3),
189
+ "end": round(min(cursor + d, seg["end"]), 3),
190
+ "words": [],
191
+ }
192
+ )
193
+ cursor += d
194
+ return out
195
+
196
+
197
+ def build_lines(
198
+ asr: dict,
199
+ max_chars: float = 16.0,
200
+ max_dur: float = 6.0,
201
+ pause: float = 0.28,
202
+ min_chars: float = 4.0,
203
+ ) -> list:
204
+ if asr.get("words"):
205
+ return lines_from_words(
206
+ asr["words"], max_chars=max_chars, max_dur=max_dur, pause=pause, min_chars=min_chars
207
+ )
208
+ return lines_from_segments(asr.get("segments", []), max_chars=max_chars, max_dur=max_dur)
209
+
210
+
211
+ def _style(font_size: int = 48, karaoke: bool = False):
212
+ st = pysubs2.SSAStyle()
213
+ st.fontname = "Microsoft YaHei"
214
+ st.fontsize = font_size
215
+ st.primarycolor = pysubs2.Color(255, 255, 255, 0)
216
+ st.secondarycolor = pysubs2.Color(120, 120, 120, 0) if karaoke else pysubs2.Color(255, 255, 255, 0)
217
+ st.outlinecolor = pysubs2.Color(0, 0, 0, 0)
218
+ st.backcolor = pysubs2.Color(0, 0, 0, 128)
219
+ st.bold = False
220
+ st.outline = 2.0
221
+ st.shadow = 0.0
222
+ st.alignment = 2
223
+ st.marginv = 40
224
+ return st
225
+
226
+
227
+ def _karaoke_text(line: dict) -> str:
228
+ parts = []
229
+ for w in line.get("words") or []:
230
+ txt = w["text"].replace("\n", " ")
231
+ if not txt:
232
+ continue
233
+ cs = max(1, int(round((w["end"] - w["start"]) * 100)))
234
+ parts.append(r"{\kf%d}%s" % (cs, txt))
235
+ return "".join(parts)
236
+
237
+
238
+ def write_subtitles(
239
+ lines: list,
240
+ out_dir,
241
+ stem: str = "subs",
242
+ formats=("srt", "ass"),
243
+ font_size: int | None = None,
244
+ resolution=None,
245
+ karaoke: bool = False,
246
+ ) -> dict:
247
+ out = ensure_dir(out_dir)
248
+ res = tuple(resolution) if resolution else (1920, 1080)
249
+ fs = int(font_size) if font_size else max(20, round(res[1] * 0.042))
250
+ subs = pysubs2.SSAFile()
251
+ subs.info["Title"] = stem
252
+ subs.info["PlayResX"] = int(res[0])
253
+ subs.info["PlayResY"] = int(res[1])
254
+ subs.styles["Default"] = _style(fs, karaoke=karaoke)
255
+ for ln in lines:
256
+ if karaoke and ln.get("words"):
257
+ text = _karaoke_text(ln)
258
+ else:
259
+ text = ln["text"].replace("\n", r"\N")
260
+ subs.append(
261
+ pysubs2.SSAEvent(
262
+ start=int(round(ln["start"] * 1000)),
263
+ end=int(round(max(ln["end"], ln["start"] + 0.2) * 1000)),
264
+ text=text,
265
+ style="Default",
266
+ )
267
+ )
268
+ written = {}
269
+ for fmt in formats:
270
+ path = out / f"{stem}.{fmt}"
271
+ subs.save(str(path))
272
+ written[fmt] = str(path)
273
+ return written
274
+
275
+
276
+ def quality_report(
277
+ lines: list,
278
+ max_cps_cjk: float = 9.0,
279
+ max_cps_latin: float = 20.0,
280
+ max_chars: float = 16.0,
281
+ min_dur: float = 0.7,
282
+ max_dur: float = 7.0,
283
+ ) -> dict:
284
+ issues = []
285
+ cps_values = []
286
+ for i, ln in enumerate(lines, 1):
287
+ text = (ln.get("text") or "").strip()
288
+ if not text:
289
+ continue
290
+ dur = max(ln["end"] - ln["start"], 1e-3)
291
+ chars = len(re.sub(r"\s", "", text))
292
+ cjk_ratio = sum(1 for ch in text if _CJK.match(ch)) / max(len(text), 1)
293
+ limit = max_cps_cjk if cjk_ratio > 0.3 else max_cps_latin
294
+ cps = chars / dur
295
+ cps_values.append(cps)
296
+ if cps > limit:
297
+ issues.append(
298
+ {
299
+ "line": i,
300
+ "type": "cps",
301
+ "time": round(ln["start"], 2),
302
+ "detail": f"{cps:.1f} 字/秒,超过 {limit:.0f}",
303
+ }
304
+ )
305
+ if display_len(text) > max_chars * 1.05:
306
+ issues.append(
307
+ {
308
+ "line": i,
309
+ "type": "width",
310
+ "time": round(ln["start"], 2),
311
+ "detail": f"显示宽度 {display_len(text):.0f},超过 {max_chars:.0f}",
312
+ }
313
+ )
314
+ if dur < min_dur:
315
+ issues.append(
316
+ {
317
+ "line": i,
318
+ "type": "short",
319
+ "time": round(ln["start"], 2),
320
+ "detail": f"只停 {dur:.2f}s,短于 {min_dur}s",
321
+ }
322
+ )
323
+ if dur > max_dur:
324
+ issues.append(
325
+ {
326
+ "line": i,
327
+ "type": "long",
328
+ "time": round(ln["start"], 2),
329
+ "detail": f"停 {dur:.2f}s,长于 {max_dur}s",
330
+ }
331
+ )
332
+ if i > 1 and ln["start"] < lines[i - 2]["end"] - 0.01:
333
+ issues.append(
334
+ {
335
+ "line": i,
336
+ "type": "overlap",
337
+ "time": round(ln["start"], 2),
338
+ "detail": f"与上一条重叠 {lines[i - 2]['end'] - ln['start']:.2f}s",
339
+ }
340
+ )
341
+
342
+ return {
343
+ "lines": len(lines),
344
+ "avg_cps": round(sum(cps_values) / len(cps_values), 2) if cps_values else 0.0,
345
+ "max_cps": round(max(cps_values), 2) if cps_values else 0.0,
346
+ "issues": issues,
347
+ "issue_count": len(issues),
348
+ "ok": not issues,
349
+ }