Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions packages/deepctl-cmd-listen/src/deepctl_cmd_listen/captions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, ",")
22 changes: 22 additions & 0 deletions packages/deepctl-cmd-listen/tests/unit/test_captions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ──────────────────────────────────────────


Expand Down