Skip to content

Commit 2292ca7

Browse files
committed
Fix Voice AI SRT millisecond rounding and timestamp validation
Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com>
1 parent 8329468 commit 2292ca7

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

‎engineering/engineering-voice-ai-integration-engineer.md‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ def assign_speakers(transcript_segments: list[TranscriptSegment],
363363
```python
364364
import json
365365
import re
366+
import math
366367

367368
def normalize_transcript(segments: list[TranscriptSegment]) -> list[TranscriptSegment]:
368369
"""
@@ -389,20 +390,28 @@ def export_srt(segments: list[TranscriptSegment], output_path: str) -> str:
389390
"""
390391
Export transcript as SRT subtitle file.
391392
392-
Validates reading speed (max 20 chars/second per broadcast standard).
393-
Splits long segments to comply with line length limits.
393+
Serializes validated cue times at millisecond precision.
394+
Reading-speed and line-length checks belong to the application adapter;
395+
this serialization example preserves the supplied text without splitting.
394396
"""
395397
def format_timestamp(seconds: float) -> str:
396-
h = int(seconds // 3600)
397-
m = int((seconds % 3600) // 60)
398-
s = int(seconds % 60)
399-
ms = int((seconds % 1) * 1000)
398+
if not math.isfinite(seconds) or seconds < 0:
399+
raise ValueError("Subtitle timestamps must be finite and nonnegative")
400+
milliseconds = round(seconds * 1000)
401+
h, milliseconds = divmod(milliseconds, 3_600_000)
402+
m, milliseconds = divmod(milliseconds, 60_000)
403+
s, ms = divmod(milliseconds, 1000)
400404
return f"{h:02d}:{m:02d}:{s:02d},{ms:03d}"
401405

402406
lines = []
403407
for i, seg in enumerate(segments, 1):
408+
if seg.end <= seg.start:
409+
raise ValueError("Subtitle cues need a positive duration")
410+
start, end = format_timestamp(seg.start), format_timestamp(seg.end)
411+
if start == end:
412+
raise ValueError("Subtitle cue collapses at millisecond precision")
404413
lines.append(str(i))
405-
lines.append(f"{format_timestamp(seg.start)} --> {format_timestamp(seg.end)}")
414+
lines.append(f"{start} --> {end}")
406415
speaker_prefix = f"[{seg.speaker}] " if seg.speaker else ""
407416
lines.append(f"{speaker_prefix}{seg.text}")
408417
lines.append("")

0 commit comments

Comments
 (0)