Sub topic 9
Reading Segment and Word Timestamps
What timestamps represent
This chapter assumes that you can run a basic Whisper transcription and understand Python dictionaries and lists. A timestamp is a time position in the audio, measured in seconds from the beginning of the file.
Whisper returns two useful timestamp levels:
- A segment is a short group of words with a start and end time.
- A word timestamp is the estimated start and end time for one word inside a segment.
Segment timestamps are useful for subtitles, search results, and chapter markers. Word timestamps are useful for highlighting the transcript as audio plays.
A segment can contain text such as "Welcome to the course." with start equal to 0.0 and end equal to 2.4. The values are estimates. They do not guarantee that the first sound begins exactly at the start value.
Requesting segment timestamps
The standard transcription result already includes segment timestamps. Set word_timestamps=True when you also need word-level data.
import whisper
model = whisper.load_model("small")
result = model.transcribe(
"interview.mp3",
language="en",
task="transcribe",
word_timestamps=True,
fp16=False,
)
for segment in result["segments"]:
print(f"Segment: {segment['start']:.2f} to {segment['end']:.2f}")
print(segment["text"].strip())
for word in segment.get("words", []):
print(f" {word['start']:.2f} to {word['end']:.2f}: {word['word'].strip()}")The result has a top-level text field and a segments list. Each segment usually contains:
| Field | Meaning |
|---|---|
| `id` | The segment's index |
| `start` | Estimated segment start in seconds |
| `end` | Estimated segment end in seconds |
| `text` | Text recognized in the segment |
| `words` | Word timing objects when requested |
Each item in words commonly contains word, start, and end. Use .get("words", []) when processing results because some runtime versions or unusual inputs might omit the field.
Turning timestamps into subtitles
Subtitle formats usually expect a start and end time rather than raw seconds. Convert seconds into hours, minutes, seconds, and milliseconds. Keep each subtitle short enough to read comfortably.
def srt_time(seconds):
milliseconds = round(seconds * 1000)
hours, remainder = divmod(milliseconds, 3_600_000)
minutes, remainder = divmod(remainder, 60_000)
seconds, milliseconds = divmod(remainder, 1000)
return f"{hours:02}:{minutes:02}:{seconds:02},{milliseconds:03}"
for number, segment in enumerate(result["segments"], start=1):
print(number)
print(f"{srt_time(segment['start'])} --> {srt_time(segment['end'])}")
print(segment["text"].strip())
print()Accuracy and common errors
Whisper predicts timing while it predicts text. A timestamp can drift when speakers talk quickly, overlap, pause for a long time, or speak over music. Word timestamps are generally less stable than segment timestamps.
Do not treat a word timestamp as a precise phonetic measurement. If exact synchronization matters, inspect the audio or use a dedicated forced-alignment tool after transcription.
When a segment crosses a meaningful boundary, such as a sentence or speaker change, segment timestamps may not match your desired display units. You can split text into smaller subtitle cues, but splitting without word timings can place words at inaccurate positions.
Exercise
Which option should you request when you need to highlight each spoken word in a media player?
Show answer
Set word_timestamps=True, then read each segment's words list and use each word's start and end values.