Files
vrsub/nodes/srt.py
cat-shark 8f6083f8cf feat: 全系统统一 Whisper V2 权重并移除 demo 工作流
确认所有运行时引用均使用 V2(V3 已停用),并修复一处真实不一致:

- 工作流数据文件:learn-translate 用 faster-whisper-large-v2、
  zh-direct 用 whisper-large-v2-translate-zh-v0.2-st-ct2(原本即 V2);
- 本地库的 demo 最新版本仍指向 large-v3:workflows/demo.json 早已改为 V2,
  但 seed 对已存在工作流刻意跳过,导致旧库停留在历史上用 V3 保存的定义,
  即本机跑 demo 实际加载 V3 权重。按用户决定移除 demo 工作流及其关联的
  6 个 run、1 个批量任务与 431 条明细(媒体库中已放置的 6 个字幕成品保留);
- nodes/whisper.py 候选与远端兜底本就是 large-v2;
- V3 权重目录保留在盘上仅作对照实验,文档标注为废弃;
  scripts/compare_whisper_v2_vs_v3.py 保留用于对照。

顺带修复与清理:
- src/wov_app/scheduler.py:_file_size 补捕 ValueError(见上一条提交说明
  的真实缺陷,此处为同一批改动);
- .gitignore:data/ 改为 /data/,避免连带忽略 tests/**/data/;
- scripts/*:评测集路径改到 scripts/data/translate_eval/;
- 代码注释与文档同步移除 demo 引用(历史调研文档保留说明性引用)。

验证:全量 477 passed;新库 seed 只创建 3 个 V2 工作流。
2026-09-13 15:41:58 +08:00

59 lines
2.1 KiB
Python

"""SRT 条目解析与序列化:正文可多行或为空,保留原始毫秒时间戳。"""
from __future__ import annotations
import re
from dataclasses import dataclass
_TIMESTAMP = re.compile(r"^(\d{2,}:\d{2}:\d{2},\d{3})\s*-->\s*(\d{2,}:\d{2}:\d{2},\d{3})$")
@dataclass(frozen=True)
class Cue:
"""一个字幕条目;序号在输出时重排,时间戳与多行正文独立保存。"""
start: str
end: str
text: str
def parse_srt(text: str) -> list[Cue]:
"""解析合法 SRT,兼容 BOM、CRLF、多余空行、空 cue 和末尾无空行。
非空的坏条目明确报错,避免静默漏字幕;空文件返回空列表。
"""
lines = text.lstrip("\ufeff").splitlines()
entries = []
index = 0
while index < len(lines):
if not lines[index].strip():
index += 1
continue
if not lines[index].strip().isdigit() or index + 1 >= len(lines):
raise ValueError(f"invalid SRT index at line {index + 1}")
match = _TIMESTAMP.fullmatch(lines[index + 1].strip())
if match is None:
raise ValueError(f"invalid SRT timestamp at line {index + 2}")
index += 2
body = []
while index < len(lines) and lines[index].strip():
# 正文行不允许是时间戳行:出现即说明条目之间缺少空行分隔。
# 若不报错,下一条的序号与时间轴会被当成上一条正文吞掉,
# 静默产出时间与文本错位的字幕(同“静默错位”类缺陷)。
if _TIMESTAMP.fullmatch(lines[index].strip()):
raise ValueError(
f"missing blank line before cue at line {index + 1}"
)
body.append(lines[index])
index += 1
entries.append(Cue(match[1], match[2], "\n".join(body)))
return entries
def serialize_srt(entries: list[Cue]) -> str:
"""按条目输出连续序号,空正文仍保留该条目的时间轴。"""
return "\n".join(
f"{i}\n{cue.start} --> {cue.end}\n{cue.text}\n"
for i, cue in enumerate(entries, 1)
)