利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.
Python ai-video-generatorcontent-creationffmpeginstagram-reelsllmpythonshort-videosubtitlestext-to-speechtiktokvideo-automationvideo-workflowworkflow-automationyoutube-shorts
fix(subtitle): round SRT timestamps to the nearest millisecond
Rudy Celekli290e8011249b
diff --git a/app/utils/utils.py b/app/utils/utils.pyindex ab33e648..ae5066ff 100644--- a/app/utils/utils.py+++ b/app/utils/utils.py@@ -242,11 +242,13 @@ def run_in_background(func, *args, **kwargs):def time_convert_seconds_to_hmsm(seconds) -> str:- hours = int(seconds // 3600)- seconds = seconds % 3600- minutes = int(seconds // 60)- milliseconds = int(seconds * 1000) % 1000- seconds = int(seconds % 60)+ # Round once to whole milliseconds and derive every field from that value.+ # Truncating float products such as 2.01 * 1000 == 2009.999... wrote cue+ # boundaries one millisecond early.+ total_milliseconds = int(round(seconds * 1000))+ hours, remainder = divmod(total_milliseconds, 3_600_000)+ minutes, remainder = divmod(remainder, 60_000)+ seconds, milliseconds = divmod(remainder, 1000)return "{:02d}:{:02d}:{:02d},{:03d}".format(hours, minutes, seconds, milliseconds)diff --git a/test/services/test_srt_timestamp_rounding.py b/test/services/test_srt_timestamp_rounding.pynew file mode 100644index 00000000..106a7882--- /dev/null+++ b/test/services/test_srt_timestamp_rounding.py@@ -0,0 +1,32 @@+import pytest++from app.utils import utils+++ "seconds, expected",+ [+ (1.001, "00:00:01,001"),+ (2.01, "00:00:02,010"),+ (4.02, "00:00:04,020"),+ (59.9996, "00:01:00,000"),+ (3725.5, "01:02:05,500"),+ (0, "00:00:00,000"),+ ],+)+def test_srt_timestamps_round_to_the_nearest_millisecond(seconds, expected):+ assert utils.time_convert_seconds_to_hmsm(seconds) == expected+++def test_every_exact_millisecond_round_trips():+ # faster-whisper word times are decimal values; none of them may be+ # written one millisecond early because of binary float truncation.+ for total_ms in range(0, 120_000):+ formatted = utils.time_convert_seconds_to_hmsm(total_ms / 1000)+ assert int(formatted[-3:]) == total_ms % 1000, formatted+++def test_text_to_srt_writes_rounded_cue_boundaries():+ cue = utils.text_to_srt(1, "Hi", 1.005, 2.01)++ assert "00:00:01,005 --> 00:00:02,010" in cue