From 7c22e72f0250f36f7cbb955b9dfaed0de296e97c Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 30 Nov 2026 13:34:00 -0400 Subject: [PATCH] fix(listen): carry rounded caption timestamps across boundaries --- .../src/deepctl_cmd_listen/captions.py | 21 +++++++++--------- .../tests/unit/test_captions.py | 22 +++++++++++++++++++ 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/packages/deepctl-cmd-listen/src/deepctl_cmd_listen/captions.py b/packages/deepctl-cmd-listen/src/deepctl_cmd_listen/captions.py index bfe1980c..9015fdd4 100644 --- a/packages/deepctl-cmd-listen/src/deepctl_cmd_listen/captions.py +++ b/packages/deepctl-cmd-listen/src/deepctl_cmd_listen/captions.py @@ -162,20 +162,19 @@ def accumulated_words(self) -> list[dict[str, Any]]: # ── Timestamp helpers ────────────────────────────────────────────────────────── +def _fmt_timestamp(seconds: float, separator: str) -> str: + total_ms = round(max(0.0, seconds) * 1000) + total_seconds, milliseconds = divmod(total_ms, 1000) + total_minutes, whole_seconds = divmod(total_seconds, 60) + hours, minutes = divmod(total_minutes, 60) + return f"{hours:02d}:{minutes:02d}:{whole_seconds:02d}{separator}{milliseconds:03d}" + + def _fmt_webvtt(seconds: float) -> str: """Format seconds as a WebVTT timestamp: HH:MM:SS.mmm""" - seconds = max(0.0, seconds) - h = int(seconds // 3600) - m = int((seconds % 3600) // 60) - s = seconds % 60 - return f"{h:02d}:{m:02d}:{s:06.3f}" + return _fmt_timestamp(seconds, ".") def _fmt_srt(seconds: float) -> str: """Format seconds as an SRT timestamp: HH:MM:SS,mmm""" - seconds = max(0.0, seconds) - h = int(seconds // 3600) - m = int((seconds % 3600) // 60) - s = int(seconds % 60) - ms = round((seconds % 1) * 1000) - return f"{h:02d}:{m:02d}:{s:02d},{ms:03d}" + return _fmt_timestamp(seconds, ",") diff --git a/packages/deepctl-cmd-listen/tests/unit/test_captions.py b/packages/deepctl-cmd-listen/tests/unit/test_captions.py index 920a074c..a4a0aab8 100644 --- a/packages/deepctl-cmd-listen/tests/unit/test_captions.py +++ b/packages/deepctl-cmd-listen/tests/unit/test_captions.py @@ -83,6 +83,28 @@ def test_negative_clamped_to_zero(self): assert _fmt_srt(-5.0) == "00:00:00,000" +# ── StreamingCaptionWriter — timestamp rounding ─────────────────────────────── + + +@pytest.mark.parametrize(("fmt", "separator"), [("webvtt", "."), ("srt", ",")]) +@pytest.mark.parametrize( + ("start", "end", "expected_start", "expected_end"), + [ + (0.9996, 1.125, "00:00:01.000", "00:00:01.125"), + (59.9996, 60.125, "00:01:00.000", "00:01:00.125"), + (3599.9994, 3599.9996, "00:59:59.999", "01:00:00.000"), + ], +) +def test_streaming_timestamp_rounding( + capsys, fmt, separator, start, end, expected_start, expected_end +): + writer = StreamingCaptionWriter(fmt) + writer.write_entry(_words(("Hello", start, end)), start, end) + + timing = f"{expected_start} --> {expected_end}".replace(".", separator) + assert timing in capsys.readouterr().out.splitlines() + + # ── StreamingCaptionWriter — WebVTT ──────────────────────────────────────────