feat: align subtitles from first 10 seconds audio

This commit is contained in:
TTlaji committed 2026-06-03 15:10:49 +08:00
1 parent 93e06ca40a
commit 5228765473
11 files changed
+248 -22

No files matched your search

+17 -1
View File
@@ -13,7 +13,7 @@ def test_ai_status_reports_missing_key(monkeypatch):
status = get_ai_status()
assert status["configured"] is False
assert "DEEPSEEK_API_KEY" in status["message"]
assert "AI_API_KEY" in status["message"]
def test_load_deepseek_config_uses_defaults(monkeypatch):
@@ -28,6 +28,20 @@ def test_load_deepseek_config_uses_defaults(monkeypatch):
assert config.model == "deepseek-chat"
def test_load_deepseek_config_accepts_generic_ai_names(monkeypatch):
monkeypatch.setenv("AI_API_KEY", "generic-key")
monkeypatch.setenv("AI_BASE_URL", "https://coding.simgreek.com/v1")
monkeypatch.setenv("AI_TEXT_MODEL", "qwen-plus")
monkeypatch.setenv("AI_AUDIO_MODEL", "qwen-omni-turbo")
config = load_deepseek_config()
assert config.api_key == "generic-key"
assert config.base_url == "https://coding.simgreek.com/v1"
assert config.model == "qwen-plus"
assert config.audio_model == "qwen-omni-turbo"
class EnvPatch:
def __init__(self):
self.originals = {}
@@ -65,6 +79,8 @@ if __name__ == "__main__":
test_ai_status_reports_missing_key(patch)
patch.restore()
test_load_deepseek_config_uses_defaults(patch)
patch.restore()
test_load_deepseek_config_accepts_generic_ai_names(patch)
finally:
patch.restore()
ai_config.read_env_file = original_read_env_file
+5 -4
View File
@@ -33,16 +33,17 @@ My first job as an investor was when I was 24 years old,
My first job as an investor was when I was 24 years old,
"""
result = process_subtitles_with_ai(raw, FakeClient())
result = process_subtitles_with_ai(raw, FakeClient(), timestamp_offset_seconds=0.2)
assert "My first job as an investor" in result["subtitle_en_content"]
assert "我第一次做投资人是在 24 岁。" in result["subtitle_zh_content"]
assert result["report"]["duplicate_count"] == 1
assert result["report"]["translation_count"] == 1
assert "00:00:00.872 --> 00:00:04.959" in result["subtitle_en_content"]
assert "00:00:00.872 --> 00:00:04.959" in result["subtitle_zh_content"]
assert "00:00:01.072 --> 00:00:05.159" in result["subtitle_en_content"]
assert "00:00:01.072 --> 00:00:05.159" in result["subtitle_zh_content"]
assert "timestamp_calibrated" in result["report"]
assert result["report"]["timestamp_calibrated"] is False
assert result["report"]["timestamp_calibrated"] is True
assert result["report"]["timestamp_offset_seconds"] == 0.2
if __name__ == "__main__":
+16 -1
View File
@@ -3,7 +3,7 @@ from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from backend.subtitle_tools import build_vtt, clean_english_subtitle
from backend.subtitle_tools import build_vtt, clean_english_subtitle, shift_cues
def test_clean_english_subtitle_removes_empty_and_duplicate_cues():
@@ -43,6 +43,21 @@ def test_build_vtt_outputs_numbered_blocks():
assert result == "WEBVTT\n\n1\n00:00:00.872 --> 00:00:04.959\nMy first job as an investor\n"
def test_shift_cues_moves_time_without_touching_text():
shifted = shift_cues([
{
"start": "00:00:00.200",
"end": "00:00:01.200",
"text": "First line",
}
], -0.5)
assert shifted[0].start == "00:00:00.000"
assert shifted[0].end == "00:00:00.700"
assert shifted[0].text == "First line"
if __name__ == "__main__":
test_clean_english_subtitle_removes_empty_and_duplicate_cues()
test_build_vtt_outputs_numbered_blocks()
test_shift_cues_moves_time_without_touching_text()