Sub topic 8
Transcribing Long Recordings with Sliding 30-Second Windows
This chapter assumes basic Python, pip, audio file formats, and the earlier idea that Whisper converts audio into tokens with an encoder-decoder model. The important new problem is duration: Whisper processes audio in windows of about 30 seconds, but recordings can last much longer.
Why long audio needs windows
A Whisper model has a fixed audio context. It cannot place an entire two-hour recording into one encoder input. The transcription runtime therefore divides the recording into consecutive windows. Each window is padded or trimmed to the model's expected duration, decoded, and added to the result.
The runtime keeps track of each window's position. As a result, the returned segment timestamps refer to the original recording rather than starting at zero for every window.
For speech that crosses a window boundary, Whisper can condition the next window on text decoded from the previous one. This gives the decoder context, but an earlier error can also influence later text. The condition_on_previous_text option lets you choose this behavior.
Transcribe a long file
The normal transcribe call already handles long-form audio. It loads and preprocesses the file, processes windows, and returns a dictionary containing the full text and individual segments.
import whisper
model = whisper.load_model("small")
result = model.transcribe(
"long_interview.mp3",
language="en",
task="transcribe",
condition_on_previous_text=True,
fp16=False, # Use False when running on a CPU.
)
with open("long_interview.txt", "w", encoding="utf-8") as output:
output.write(result["text"].strip() + "\n")
for segment in result["segments"]:
print(
f"{segment['start'] / 60:.2f}–{segment['end'] / 60:.2f} min: "
f"{segment['text'].strip()}"
)The fp16=False setting avoids a half-precision warning on most CPUs. On a compatible GPU, half precision usually reduces memory use and improves speed.
Whisper's default window progression is designed to preserve continuity. You normally should not manually split a file into independent 30-second files unless you also handle timestamps and boundary context. Independent files can repeat words, omit words at cuts, or lose the beginning of a sentence.
Common long-form decisions
| Decision | Useful default | Reason |
|---|---|---|
| Previous-text conditioning | `True` | Preserves context across windows |
| Language | Set it when known | Prevents repeated language-detection mistakes |
| Silence handling | Inspect results | Silence can produce invented text |
| Output format | Save segments with timestamps | Makes review and searching practical |
For recordings with long pauses, background noise, or music, use a voice activity detector (VAD) before transcription or inspect suspicious segments afterward. VAD is a tool that estimates which parts contain speech. Removing silence can reduce processing time, but aggressive filtering may remove quiet words.
If the transcript suddenly repeats phrases, changes topic, or produces text during silence, try condition_on_previous_text=False. This reduces error propagation between windows, though it can weaken continuity at legitimate sentence boundaries.
Exercise
Exercise
Why can setting conditiononprevious_text=False help when a long transcript starts repeating invented phrases?
Show answer
It prevents a bad decoded window from supplying its text as context to the next window, which can stop the error from propagating.