Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(voice): carry rounded subtitle seconds into minutes

Rudy Celekli4ffe008f0669
diff --git a/app/services/voice.py b/app/services/voice.py
index 36010e30..688e14bf 100644
--- a/app/services/voice.py
+++ b/app/services/voice.py
@@ -117,10 +117,13 @@ def mktimestamp(time_unit: float) -> str:
还需要这个格式化函数来兼容 Azure v2、Gemini、SiliconFlow 这些
手工构造的字幕时间轴,因此这里内置一个等价实现。
"""
- hour = math.floor(time_unit / 10**7 / 3600)
- minute = math.floor((time_unit / 10**7 / 60) % 60)
- seconds = (time_unit / 10**7) % 60
- return f"{hour:02d}:{minute:02d}:{seconds:06.3f}"
+ # 先统一取整到毫秒再拆分时分秒。直接用 `:06.3f` 格式化取模后的秒数,
+ # 59.9996 秒会显示成 `00:00:60.000`,严格的 SRT 解析器会拒绝该时间戳。
+ total_milliseconds = int(round(time_unit / 10**4))
+ hour, remainder = divmod(total_milliseconds, 3_600_000)
+ minute, remainder = divmod(remainder, 60_000)
+ seconds, milliseconds = divmod(remainder, 1000)
+ return f"{hour:02d}:{minute:02d}:{seconds:02d}.{milliseconds:03d}"
def get_siliconflow_voices() -> list[str]:
diff --git a/test/services/test_voice_timestamp_carry.py b/test/services/test_voice_timestamp_carry.py
new file mode 100644
index 00000000..2025ef03
--- /dev/null
+++ b/test/services/test_voice_timestamp_carry.py
@@ -0,0 +1,26 @@
+import pytest
+
+from app.services import voice
+
+
+ "seconds, expected",
+ [
+ (59.9996, "00:01:00.000"),
+ (119.9999, "00:02:00.000"),
+ (3599.9996, "01:00:00.000"),
+ (1.2345, "00:00:01.234"),
+ (61.5, "00:01:01.500"),
+ (0, "00:00:00.000"),
+ ],
+)
+def test_timestamp_rounding_carries_into_minutes_and_hours(seconds, expected):
+ assert voice.mktimestamp(seconds * 10**7) == expected
+
+
+def test_subtitle_formatter_never_writes_sixty_seconds():
+ formatter = voice._build_subtitle_formatter()
+
+ cue = formatter(1, 0, 59.9996 * 10**7, "Hello")
+
+ assert cue == "1\n00:00:00,000 --> 00:01:00,000\nHello\n"