48 lines
1.9 KiB
Python
48 lines
1.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""用真实OCR数据生成一份【已过滤水印】的测试SRT。跳过读图(monkeypatch)。
|
|
用法: python temp/gen_test_srt.py <composite目录> <输出srt>
|
|
"""
|
|
import sys, os, json
|
|
sys.path.insert(0, os.path.abspath("."))
|
|
import tool.getsrt as G
|
|
from tool.log.log_manager import UnifiedLogger
|
|
|
|
G.find_best_ocr_match = lambda *a, **k: (None, 0) # 跳过首尾音频匹配的读图
|
|
|
|
D = sys.argv[1] if len(sys.argv) > 1 else "test_srt/BV1BamdYLExJ"
|
|
OUT = sys.argv[2] if len(sys.argv) > 2 else "test_srt/BV1BamdYLExJ.filtered.srt"
|
|
META = os.path.join(D, "metadata.json")
|
|
logger = UnifiedLogger(); logger.reset(); logger.init_logging("rerun_log.json"); logger.start_program("rerun")
|
|
|
|
pairs = []
|
|
lines = open(os.path.join(D, "ocr_result.jsonl"), encoding="utf-8").readlines()
|
|
i = 0
|
|
while i < len(lines):
|
|
if i + 1 < len(lines) and lines[i].startswith("pic_path: "):
|
|
fn = os.path.basename(lines[i].strip()[len("pic_path: "):])
|
|
try:
|
|
pairs.append([os.path.join(D, fn), json.loads(lines[i + 1])]); i += 2; continue
|
|
except json.JSONDecodeError:
|
|
pass
|
|
i += 1
|
|
|
|
# 坐标水印过滤(就是接进 getsrt 的那个)
|
|
G._filter_watermark_by_position(pairs, logger)
|
|
|
|
best = G.analyze_subtitle_blocks(pairs, META, logger)
|
|
h = G.analyze_h(pairs, META)
|
|
results = []
|
|
for idx in range(len(pairs)):
|
|
try:
|
|
text, conf, flag, sim = G.extract_highest_subtitle(idx, pairs, best, META, h, logger)
|
|
except Exception:
|
|
text = ""
|
|
if text and not G.check_subtitle_quality_enhanced(text) and conf <= 90:
|
|
text = ""
|
|
results.append((idx, text or ""))
|
|
# 静态文字过滤(频率, 兜底)
|
|
results = G._filter_static_text(results, logger)
|
|
G.generate_srt_from_results(results, OUT, interval_seconds=0.33)
|
|
n = sum(1 for l in open(OUT, encoding="utf-8") if "-->" in l)
|
|
print("生成: %s 共 %d 条字幕" % (OUT, n))
|