Skip to content

Commit 02b7430

Browse files
authored
Fix timestamps and strip extraneous whitespace in WebVTT output (openai#219)
* Use two-digit hours in WebVTT timestamps Per the WebVTT specification [0]: > A WebVTT timestamp consists of the following components, in the given > order: > > 1. Optionally (required if hours is non-zero): > 1. Two or more ASCII digits, representing the hours as a base ten > integer. > 2. A U+003A COLON character (:) YouTube won’t accept timestamps containing single-digit hours. [0] https://www.w3.org/TR/webvtt1/#webvtt-timestamp * Strip segment text in WebVTT output We already do this for plain text and SubRip output, so we should do it for WebVTT too.
1 parent 0b1ba3d commit 02b7430

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

whisper/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def format_timestamp(seconds: float, always_include_hours: bool = False, decimal
4040
seconds = milliseconds // 1_000
4141
milliseconds -= seconds * 1_000
4242

43-
hours_marker = f"{hours}:" if always_include_hours or hours > 0 else ""
43+
hours_marker = f"{hours:02d}:" if always_include_hours or hours > 0 else ""
4444
return f"{hours_marker}{minutes:02d}:{seconds:02d}{decimal_marker}{milliseconds:03d}"
4545

4646

@@ -54,7 +54,7 @@ def write_vtt(transcript: Iterator[dict], file: TextIO):
5454
for segment in transcript:
5555
print(
5656
f"{format_timestamp(segment['start'])} --> {format_timestamp(segment['end'])}\n"
57-
f"{segment['text'].replace('-->', '->')}\n",
57+
f"{segment['text'].strip().replace('-->', '->')}\n",
5858
file=file,
5959
flush=True,
6060
)

0 commit comments

Comments
 (0)