From ae4791143c1e8291e28edecb8fe76ceaba11a682 Mon Sep 17 00:00:00 2001 From: TTlaji Date: Wed, 3 Jun 2026 14:07:33 +0800 Subject: [PATCH] fix: constrain subtitle AI to preserve timestamps --- backend/subtitle_ai.py | 7 ++++++- tests/subtitle_ai_test.py | 14 +++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/backend/subtitle_ai.py b/backend/subtitle_ai.py index cbfc254..260edc6 100644 --- a/backend/subtitle_ai.py +++ b/backend/subtitle_ai.py @@ -6,7 +6,11 @@ from .subtitle_tools import build_vtt, clean_english_subtitle TRANSLATION_SYSTEM_PROMPT = """ 你是英语学习产品的字幕翻译助手。输入是按顺序排列的英文字幕。 请只返回 JSON,格式为 {"translations":[{"index":1,"zh":"中文翻译"}]}。 -要求:中文自然,适合英语学习;不要增删字幕条数;不要解释。 +要求: +1. 只翻译英文文本,不要改写原文含义。 +2. 不要修改时间轴,不要生成任何 start/end/time 字段。 +3. 不要增删字幕条数,每个 index 必须返回一条中文。 +4. 中文自然,适合英语学习;不要解释。 """.strip() @@ -45,5 +49,6 @@ def process_subtitles_with_ai(english_content: str, client) -> dict[str, object] "empty_count": cleaned.empty_count, "translation_count": len(zh_cues), "timestamp_calibrated": False, + "timestamp_note": "AI 仅翻译文本,中文字幕时间轴从英文字幕原样复制。", }, } diff --git a/tests/subtitle_ai_test.py b/tests/subtitle_ai_test.py index 72b27af..7b7ae77 100644 --- a/tests/subtitle_ai_test.py +++ b/tests/subtitle_ai_test.py @@ -8,9 +8,17 @@ from backend.subtitle_ai import process_subtitles_with_ai class FakeClient: def chat_json(self, system_prompt, user_payload): + assert "不要修改时间轴" in system_prompt + assert "start" not in user_payload["cues"][0] + assert "end" not in user_payload["cues"][0] return { "translations": [ - {"index": 1, "zh": "我第一次做投资人是在 24 岁。"} + { + "index": 1, + "start": "00:00:99.000", + "end": "00:00:99.999", + "zh": "我第一次做投资人是在 24 岁。", + } ] } @@ -31,6 +39,10 @@ My first job as an investor was when I was 24 years old, 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 "timestamp_calibrated" in result["report"] + assert result["report"]["timestamp_calibrated"] is False if __name__ == "__main__":