Offline Video Transcription with Whisper and FFmpeg, with Timestamps
To transcribe an MP4 on your own computer and keep the times, install FFmpeg and the openai-whisper package, call model.transcribe() on the file, and save the segments list from the result, not only the text. The model weights are downloaded once; after that the transcription itself runs on your machine and nothing needs to be uploaded.
AI-Powered Video Analyzer, a tool that analyzes video files on your machine, also transcribes with Whisper, but its report keeps only the text and the detected language, with no times. This tutorial therefore shows both: what the analyzer does (version 1.1.0, checked 27 November 2026) and a separate, direct use of Whisper for timestamps.
What happens to the audio before Whisper sees it
A video file is a container holding separate streams, typically one video stream and one audio stream. Transcription uses only the audio. The path has four steps:
- Decode the audio stream from its codec (AAC in a typical MP4) into raw samples.
- Downmix and resample to one channel at 16 kHz.
openai-whisperdoes this by calling theffmpegprogram itself. - Convert the samples into a log-Mel spectrogram, a picture of energy by frequency band over time.
- Decode text from that spectrogram with a Transformer encoder-decoder.
From a video file to timestamped text
Five stages in order: FFmpeg decodes the audio stream to 16 kHz mono samples, the samples become a log-Mel spectrogram in 30 second windows, an encoder turns it into 1500 positions, a decoder produces text and timestamp tokens, and these are grouped into segments.
| stage | input | output | what happens |
|---|---|---|---|
| 1. Decode and resample | audio stream in an MP4 | 16 kHz mono waveform | FFmpeg decodes the codec, mixes to one channel and resamples. The video stream is ignored. |
| 2. Log-Mel spectrogram | waveform samples | 80 bands x 3000 frames per 30 s | 25 ms windows every 10 ms, Mel filter bank, logarithm and scaling. The large-v3 and turbo models use 128 bands. |
| 3. Encoder | 80 x 3000 spectrogram | 1500 positions of 20 ms | Two convolutions, the second with stride 2, followed by Transformer blocks. |
| 4. Decoder | encoder output and tokens so far | text and timestamp tokens | Special tokens select the language and the task; timestamps are tokens in steps of 20 ms. |
| 5. Segments | tokens of each window | start, end, text | Times are shifted by the window position. The window then moves on through the file. |
Stage 1 of 5, Decode and resample. Input: audio stream in an MP4. Output: 16 kHz mono waveform.
Sampling rate and sample count
The number of samples is the sampling rate times the duration: N = f_s * T. A 12 second clip at 16 kHz has 192,000 samples. Whisper works in 30 second windows of 480,000 samples; shorter audio is padded with silence.
sr, seconds = 16000, 12
print("samples in the clip:", sr * seconds)
print("samples per 30 s window:", 30 * sr)
print("Mel frames per window:", 30 * sr // 160, "| encoder positions:", 30 * sr // 160 // 2)
print("bytes, 16 kHz mono 16-bit:", sr * seconds * 2)
print("bytes, 44.1 kHz stereo 16-bit:", 44100 * seconds * 2 * 2)
samples in the clip: 192000
samples per 30 s window: 480000
Mel frames per window: 3000 | encoder positions: 1500
bytes, 16 kHz mono 16-bit: 384000
bytes, 44.1 kHz stereo 16-bit: 2116800
The last line is the size of the temporary WAV the analyzer writes for such a clip (44.1 kHz, stereo, 16-bit), apart from the header. Whisper resamples it down again.
Short-time Fourier transform and Mel filters
A waveform shows amplitude over time but hides which frequencies are present. The short-time Fourier transform (STFT) cuts the signal into short overlapping windows and takes a Fourier transform of each:
X(m, k) = sum over n of x[n + m*H] * w[n] * exp(-2*pi*i*k*n / N_fft)
S(m, b) = log10( sum over k of M_b(k) * |X(m, k)|^2 )
Here x is the signal, m the window number, H the step between windows (160 samples), w the window function, N_fft the window length (400 samples), k the frequency bin, and M_b the b-th triangular filter on the Mel scale, which is finely spaced at low frequencies and coarsely spaced at high ones. Whisper then clips the dynamic range and rescales the values.
The following is a simplified version for a made-up signal: a 300 Hz tone for half a second, then a 2000 Hz tone. It uses its own Mel filters, no padding and none of Whisper's scaling.
import numpy as np
sr, n_fft, hop, n_mels = 16000, 400, 160, 80 # 25 ms window, 10 ms step
t = np.arange(sr) / sr # one second of audio
x = np.where(t < 0.5, np.sin(2 * np.pi * 300 * t), np.sin(2 * np.pi * 2000 * t))
frames = np.stack([x[i:i + n_fft] for i in range(0, len(x) - n_fft + 1, hop)])
power = np.abs(np.fft.rfft(frames * np.hanning(n_fft), axis=1)) ** 2 # STFT power
to_mel = lambda f: 2595 * np.log10(1 + f / 700)
to_hz = lambda m: 700 * (10 ** (m / 2595) - 1)
edges = to_hz(np.linspace(0, to_mel(sr / 2), n_mels + 2))
freqs = np.fft.rfftfreq(n_fft, 1 / sr)
bank = np.stack([np.clip(np.minimum((freqs - lo) / (mid - lo), (hi - freqs) / (hi - mid)), 0, None)
for lo, mid, hi in zip(edges[:-2], edges[1:-1], edges[2:])])
logmel = np.log10(np.maximum(power @ bank.T, 1e-10)) # (time, mel)
print("samples:", len(x), "| STFT power:", power.shape, "| log-Mel:", logmel.shape)
for name, part in (("first half ", logmel[:45]), ("second half", logmel[55:])):
band = part.mean(axis=0).argmax()
print(f"{name}: strongest Mel band {band}, centred at {edges[band + 1]:.0f} Hz")
samples: 16000 | STFT power: (98, 201) | log-Mel: (98, 80)
first half : strongest Mel band 11, centred at 317 Hz
second half: strongest Mel band 42, centred at 1967 Hz
Each tone peaks in a band centred within about 35 Hz of it. The FFT bins of a 25 ms window are 40 Hz apart and neighbouring filters overlap, so adjacent bands receive energy too.
For real input, use Whisper's own functions: whisper.log_mel_spectrogram(whisper.load_audio("talk.wav")) returns 80 rows by 1200 frames for 12 seconds of audio, which transcribe() pads to a 3000 frame window before the model sees it. The figure shows that array for the spoken test clip used below, averaged down to a coarse grid.
Whisper log-Mel input of the 12 second test clip
A coarse spectrogram of twenty Mel band groups over twelve seconds of synthetic speech. Energy is concentrated in the lower bands and continues through most of the clip, with brief quieter columns, one of them near six and a half seconds.
scaled log-Mel (cells below the threshold are dimmed)
-0.7 (dark)1 (light)
Left to right: time (s) 0 to 11.75. Bottom to top: lowest Mel band in row 0 to 76.
- selected cell
- lowest Mel band in row 0, time (s) 0
- scaled log-Mel there
- 0
- scaled log-Mel: cells counted
- 960 of 960
| row | 0 | 0.25 | 0.5 | 0.75 | 1 | 1.25 | 1.5 | 1.75 | 2 | 2.25 | 2.5 | 2.75 | 3 | 3.25 | 3.5 | 3.75 | 4 | 4.25 | 4.5 | 4.75 | 5 | 5.25 | 5.5 | 5.75 | 6 | 6.25 | 6.5 | 6.75 | 7 | 7.25 | 7.5 | 7.75 | 8 | 8.25 | 8.5 | 8.75 | 9 | 9.25 | 9.5 | 9.75 | 10 | 10.25 | 10.5 | 10.75 | 11 | 11.25 | 11.5 | 11.75 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 76 | -0.6 | -0.6 | -0.6 | -0.3 | 0 | -0.4 | -0.4 | -0.4 | -0.4 | -0.5 | -0.3 | -0.6 | -0.6 | -0.6 | -0.6 | -0.5 | -0.6 | -0.6 | -0.5 | -0.5 | -0.2 | -0.6 | -0.2 | -0.5 | -0.4 | -0.6 | -0.3 | -0.6 | -0.3 | -0.5 | -0.4 | -0.5 | -0.4 | -0.6 | -0.7 | -0.7 | -0.6 | -0.6 | -0.5 | -0.6 | -0.6 | -0.4 | -0.4 | -0.3 | -0.6 | -0.6 | -0.6 | -0.5 |
| 72 | -0.5 | -0.3 | -0.2 | 0 | 0.3 | -0.2 | -0.2 | -0.1 | -0.2 | -0.3 | -0.1 | -0.4 | -0.1 | -0.5 | -0.3 | -0.3 | -0.3 | -0.4 | -0.2 | -0.3 | 0 | -0.3 | 0 | -0.3 | -0.3 | -0.4 | -0.1 | -0.4 | 0.1 | -0.2 | -0.2 | -0.3 | -0.2 | -0.5 | -0.5 | -0.5 | -0.3 | -0.4 | -0.2 | -0.2 | -0.3 | -0.2 | -0.2 | -0.1 | -0.4 | -0.4 | -0.4 | -0.4 |
| 68 | -0.5 | -0.2 | -0.1 | 0.1 | 0.1 | -0.1 | -0.1 | -0.1 | -0.2 | -0.1 | -0.1 | -0.1 | 0 | -0.4 | -0.1 | -0.1 | -0.2 | -0.1 | -0.1 | -0.1 | -0.1 | -0.2 | 0 | -0.1 | -0.2 | -0.3 | -0.2 | -0.3 | 0.1 | -0.1 | -0.1 | -0.2 | -0.2 | -0.3 | -0.3 | -0.4 | 0 | -0.3 | -0.1 | -0.1 | 0 | -0.2 | 0 | -0.2 | -0.2 | -0.3 | -0.4 | -0.4 |
| 64 | -0.5 | -0.1 | 0 | 0 | -0.1 | -0.2 | -0.1 | -0.2 | -0.1 | -0.1 | -0.2 | -0.2 | -0.1 | -0.4 | -0.1 | -0.2 | -0.1 | -0.2 | -0.3 | -0.1 | -0.2 | -0.1 | -0.1 | -0.1 | -0.3 | -0.2 | -0.3 | -0.3 | 0.1 | -0.1 | -0.2 | -0.2 | -0.3 | -0.3 | -0.3 | -0.3 | -0.1 | -0.3 | -0.1 | -0.1 | -0.1 | -0.2 | 0 | -0.3 | -0.1 | -0.2 | -0.3 | -0.4 |
| 60 | -0.5 | 0.1 | 0.1 | 0 | -0.2 | -0.2 | 0.1 | -0.3 | -0.1 | 0 | -0.3 | -0.1 | 0.1 | -0.4 | 0 | -0.1 | -0.1 | 0 | -0.2 | 0 | -0.3 | 0 | 0 | 0 | -0.2 | -0.1 | -0.4 | -0.2 | 0.1 | 0 | -0.2 | -0.2 | -0.3 | -0.2 | -0.2 | -0.4 | 0 | -0.2 | 0 | 0.1 | 0.1 | -0.2 | 0.1 | -0.3 | 0 | -0.1 | -0.3 | -0.3 |
| 56 | -0.4 | 0.1 | 0.1 | 0.1 | -0.2 | -0.2 | 0.1 | -0.2 | -0.1 | 0.1 | -0.3 | -0.1 | 0.2 | -0.3 | 0 | -0.1 | 0 | 0 | -0.2 | 0 | -0.2 | 0.2 | -0.1 | 0 | -0.1 | -0.1 | -0.4 | -0.1 | 0.1 | 0.1 | -0.3 | -0.1 | -0.3 | -0.2 | -0.1 | -0.2 | 0.1 | -0.2 | 0 | 0.2 | 0 | -0.2 | 0.1 | -0.3 | 0 | 0 | -0.3 | -0.3 |
| 52 | -0.4 | 0.2 | 0.2 | -0.1 | -0.3 | -0.2 | 0.1 | 0 | 0 | 0.1 | -0.2 | -0.2 | 0.3 | -0.3 | 0.2 | 0 | -0.1 | -0.1 | -0.1 | 0 | -0.2 | 0.2 | -0.1 | -0.1 | -0.2 | 0.1 | -0.5 | 0 | 0.1 | 0.2 | -0.2 | -0.1 | -0.2 | -0.3 | 0 | -0.2 | -0.1 | 0 | -0.1 | 0 | 0 | -0.1 | 0.1 | -0.3 | 0.1 | 0.1 | -0.1 | -0.3 |
| 48 | -0.4 | 0.1 | 0.1 | -0.3 | -0.4 | -0.2 | 0 | -0.2 | -0.2 | 0.2 | -0.3 | -0.4 | 0.1 | -0.4 | 0.1 | -0.1 | -0.2 | -0.3 | -0.3 | -0.1 | -0.4 | -0.1 | -0.4 | -0.2 | -0.2 | 0.1 | -0.6 | -0.2 | -0.2 | 0.1 | -0.2 | -0.1 | -0.3 | -0.3 | -0.2 | -0.3 | -0.3 | -0.1 | -0.2 | -0.1 | 0.2 | -0.2 | 0 | -0.3 | 0.2 | -0.1 | -0.2 | -0.4 |
| 44 | -0.4 | 0 | 0.2 | -0.2 | -0.4 | -0.1 | 0 | -0.2 | -0.3 | 0.1 | -0.4 | -0.4 | 0.3 | -0.3 | 0.2 | 0 | -0.2 | -0.4 | -0.3 | -0.1 | -0.3 | -0.3 | -0.4 | -0.1 | -0.2 | 0 | -0.5 | -0.4 | -0.1 | 0.2 | -0.2 | -0.1 | -0.2 | -0.2 | -0.3 | -0.3 | -0.2 | -0.1 | -0.2 | -0.1 | 0.2 | -0.3 | 0 | -0.3 | 0.2 | -0.2 | -0.2 | -0.3 |
| 40 | -0.5 | -0.1 | 0.2 | -0.1 | -0.5 | -0.1 | -0.1 | -0.3 | -0.3 | 0 | -0.4 | -0.3 | 0.3 | -0.4 | 0.1 | 0.1 | -0.1 | -0.4 | -0.2 | -0.2 | -0.4 | -0.4 | -0.4 | -0.1 | -0.2 | -0.1 | -0.6 | -0.5 | -0.3 | 0.1 | -0.1 | -0.2 | -0.1 | -0.1 | -0.5 | -0.4 | -0.1 | -0.2 | -0.2 | 0.1 | 0.3 | -0.3 | -0.1 | -0.3 | 0.1 | -0.3 | -0.3 | -0.4 |
| 36 | -0.5 | -0.1 | 0.2 | -0.1 | -0.5 | -0.2 | -0.2 | -0.4 | -0.3 | -0.1 | -0.5 | -0.2 | 0.2 | -0.5 | 0 | -0.1 | -0.1 | -0.3 | -0.1 | -0.3 | -0.4 | -0.4 | -0.4 | -0.1 | -0.3 | -0.2 | -0.6 | -0.6 | -0.4 | -0.1 | -0.1 | -0.2 | -0.2 | -0.1 | -0.5 | -0.4 | 0 | -0.2 | -0.1 | 0.2 | 0.4 | -0.4 | -0.1 | -0.2 | 0 | -0.4 | -0.3 | -0.4 |
| 32 | -0.5 | -0.1 | 0.2 | 0 | -0.5 | -0.2 | -0.2 | -0.4 | -0.3 | -0.2 | -0.5 | -0.1 | 0.2 | -0.5 | -0.1 | -0.1 | 0 | -0.1 | 0 | -0.3 | -0.4 | -0.4 | -0.4 | -0.1 | -0.3 | -0.2 | -0.6 | -0.6 | -0.4 | -0.1 | -0.2 | -0.2 | -0.3 | -0.1 | -0.6 | -0.5 | 0.2 | -0.1 | -0.1 | 0.3 | 0.4 | -0.5 | -0.1 | -0.3 | 0 | -0.4 | -0.4 | -0.4 |
| 28 | -0.5 | 0 | 0.3 | 0 | -0.5 | -0.2 | -0.1 | -0.2 | -0.2 | 0 | -0.4 | 0 | 0.3 | -0.5 | 0 | -0.1 | 0.2 | 0.2 | 0.1 | -0.2 | -0.4 | -0.2 | -0.4 | 0 | -0.3 | 0 | -0.6 | -0.5 | -0.4 | 0 | -0.2 | -0.2 | -0.2 | 0 | -0.4 | -0.3 | 0.3 | 0 | -0.1 | 0.4 | 0.5 | -0.4 | 0 | -0.2 | 0.1 | -0.3 | -0.1 | -0.4 |
| 24 | -0.5 | 0.1 | 0.4 | 0.1 | -0.5 | -0.2 | -0.1 | -0.2 | -0.2 | 0 | -0.4 | 0 | 0.4 | -0.5 | 0.1 | 0 | 0.1 | 0.1 | 0.1 | -0.2 | -0.4 | -0.1 | -0.4 | 0 | -0.3 | 0 | -0.6 | -0.5 | -0.4 | 0.1 | -0.2 | -0.1 | -0.2 | -0.1 | -0.4 | -0.3 | 0.3 | 0.1 | -0.1 | 0.4 | 0.5 | -0.3 | 0 | -0.3 | 0.1 | -0.2 | -0.1 | -0.4 |
| 20 | -0.4 | 0.2 | 0.5 | 0.2 | -0.5 | -0.1 | 0.1 | -0.2 | -0.2 | 0.1 | -0.5 | 0 | 0.6 | -0.4 | 0.3 | 0.1 | 0.2 | 0.2 | 0.1 | -0.1 | -0.4 | -0.1 | -0.4 | 0.1 | -0.3 | 0.1 | -0.6 | -0.5 | -0.3 | 0.2 | -0.2 | -0.1 | -0.1 | 0 | -0.3 | -0.2 | 0.4 | 0.1 | -0.1 | 0.5 | 0.7 | -0.3 | 0 | -0.2 | 0.3 | -0.2 | -0.1 | -0.3 |
| 16 | -0.4 | 0.4 | 0.6 | 0.3 | -0.4 | 0 | 0.2 | 0.1 | 0 | 0.3 | -0.2 | 0.2 | 0.5 | -0.2 | 0.4 | 0.3 | 0.4 | 0.4 | 0.2 | 0.1 | -0.2 | 0.2 | -0.2 | 0.2 | -0.1 | 0.2 | -0.5 | -0.3 | -0.1 | 0.3 | -0.1 | 0.1 | 0 | 0.2 | -0.1 | -0.1 | 0.6 | 0.4 | 0.1 | 0.6 | 0.8 | -0.1 | 0.2 | -0.1 | 0.5 | 0.1 | 0.2 | -0.2 |
| 12 | -0.3 | 0.6 | 0.6 | 0.3 | -0.3 | 0.1 | 0.3 | 0.4 | 0.1 | 0.3 | 0 | 0.4 | 0.4 | 0 | 0.5 | 0.5 | 0.6 | 0.6 | 0.4 | 0.1 | -0.2 | 0.5 | -0.1 | 0.3 | 0.1 | 0.3 | -0.5 | 0 | 0.1 | 0.3 | 0 | 0.2 | 0.1 | 0.4 | 0.1 | -0.1 | 0.7 | 0.5 | 0.2 | 0.6 | 0.7 | 0.1 | 0.3 | 0.1 | 0.7 | 0.3 | 0.3 | -0.2 |
| 8 | -0.2 | 0.9 | 0.7 | 0.5 | -0.2 | 0.2 | 0.4 | 0.5 | 0.3 | 0.4 | 0.3 | 0.5 | 0.6 | 0.1 | 0.6 | 0.6 | 0.8 | 0.7 | 0.6 | 0.2 | -0.1 | 0.7 | 0 | 0.3 | 0.2 | 0.6 | -0.4 | 0.3 | 0.1 | 0.4 | 0.2 | 0.3 | 0.2 | 0.6 | 0.3 | 0 | 0.7 | 0.7 | 0.4 | 0.7 | 0.7 | 0.2 | 0.4 | 0.3 | 0.8 | 0.5 | 0.6 | -0.1 |
| 4 | 0 | 1 | 1 | 0.7 | -0.1 | 0.3 | 0.5 | 0.8 | 0.4 | 0.6 | 0.5 | 0.7 | 0.8 | 0.3 | 0.8 | 0.7 | 1 | 0.9 | 0.9 | 0.5 | 0 | 0.9 | 0.1 | 0.5 | 0.4 | 0.9 | -0.3 | 0.5 | 0.3 | 0.6 | 0.4 | 0.7 | 0.3 | 0.8 | 0.5 | 0.2 | 0.9 | 1 | 0.7 | 0.9 | 0.9 | 0.4 | 0.5 | 0.5 | 0.9 | 0.7 | 0.9 | 0.2 |
| 0 | 0 | 0.5 | 0.7 | 0.4 | -0.1 | 0.2 | 0.3 | 0.6 | 0.3 | 0.4 | 0.4 | 0.5 | 0.5 | 0.3 | 0.6 | 0.5 | 0.7 | 0.7 | 0.7 | 0.4 | 0 | 0.7 | 0.1 | 0.4 | 0.3 | 0.6 | -0.3 | 0.3 | 0.2 | 0.4 | 0.3 | 0.5 | 0.2 | 0.5 | 0.4 | 0.2 | 0.7 | 0.7 | 0.5 | 0.6 | 0.6 | 0.3 | 0.3 | 0.3 | 0.6 | 0.5 | 0.7 | 0.1 |
Cell lowest Mel band in row 0, time (s) 0: scaled log-Mel 0 (at or above the threshold). Cells at or above -0.7: scaled log-Mel 960 of 960.
Most of the energy sits in the lower bands and runs through the clip. A quieter column near 6.5 seconds coincides with a segment boundary reported later, but similar dips occur inside sentences too: you cannot read the segmentation off silences alone.
The model
Whisper is an encoder-decoder Transformer. The paper's models were trained on 680,000 hours of audio paired with transcripts collected at scale (weak supervision). The decoder's token sequence begins with a language token and a task token (transcribe or translate), followed by the text. For timestamps it predicts additional tokens that give times within the current window in steps of 20 ms, before and after each stretch of text. A segment's start and end are therefore model predictions, not measurements of the signal.
The sizes below are the figures in OpenAI's README as read on 27 November 2026. Relative speed was measured by OpenAI for English speech on their GPU, not here.
| Size | Parameters | Approximate VRAM | Relative speed (large = 1) |
|---|---|---|---|
| tiny | 39M | about 1 GB | about 10x |
| base | 74M | about 1 GB | about 7x |
| small | 244M | about 2 GB | about 4x |
| medium | 769M | about 5 GB | about 2x |
| large | 1550M | about 10 GB | 1x |
| turbo | 809M | about 6 GB | about 8x |
Extract the audio with FFmpeg
Look at the streams first:
ffprobe -v error -show_entries stream=codec_type,codec_name,sample_rate,channels,start_time,duration -of compact talk.mp4
Then write a WAV file in the format Whisper uses internally:
ffmpeg -i talk.mp4 -vn -ac 1 -ar 16000 -c:a pcm_s16le talk.wav
| Option | Effect | When you need it |
|---|---|---|
-vn |
Do not write a video stream | Always, for audio extraction |
-ac 1 |
One channel | Whisper is mono |
-ar 16000 |
16 kHz sampling rate | Whisper's input rate |
-c:a pcm_s16le |
Uncompressed 16-bit WAV | Easy to inspect and reuse |
-map 0:a:1 |
Take the second audio stream | Files with several languages or tracks |
-ss 60 -t 30 |
Start at 60 s, keep 30 s | Testing on a short piece |
-af aresample=async=1:first_pts=0 |
Fill a late audio start with silence | Keeping times on the video's timeline |
You do not strictly need this step: Whisper accepts the MP4 and runs FFmpeg itself. Extracting first lets you listen to exactly what the model receives, cut it, and check its length.
The last row of the table matters for alignment. In a test file whose audio stream started 1.98 s after the video, plain extraction produced a WAV that began directly with the sound, so every timestamp would have been about two seconds early relative to the picture. With the aresample filter shown, FFmpeg 8.0.1 wrote about two seconds of silence first. Passing the MP4 straight to Whisper behaves like the plain extraction, so check start_time in the ffprobe output either way.
Transcribe and keep the timestamps
Install Whisper with the command its README gives, and FFmpeg from your system's package manager:
pip install -U openai-whisper
sudo apt install ffmpeg # macOS: brew install ffmpeg
This is a direct use of openai-whisper. It is not something the analyzer exports.
import json
import whisper
model = whisper.load_model("base") # downloads the weights on first use
result = model.transcribe("talk.wav", task="transcribe") # language=None: auto-detect
segments = [{"start": round(s["start"], 2), "end": round(s["end"], 2), "text": s["text"].strip()}
for s in result["segments"]]
with open("talk_segments.json", "w", encoding="utf-8") as f:
json.dump({"language": result["language"], "segments": segments}, f,
ensure_ascii=False, indent=2)
print(result["language"], len(segments))
for s in segments:
print(f"{s['start']:6.2f} {s['end']:6.2f} {s['text']}")
The test material is a 12 second clip with a synthetic voice (FFmpeg's flite) reading three sentences, so the correct text is known. A recorded run of base with library defaults (openai-whisper 20250625, CUDA GPU, Linux), on the 44.1 kHz stereo WAV that the analyzer extracted from that clip, gave these segments:
0.00- 3.20 ' Video analysis starts by choosing which frames to look at.'
3.20- 6.48 ' The detector then labels objects in each selected frame.'
6.48- 11.76 ' Speech is transcribed separately, and the language model writes a short summary at the end.'
One word differs from the script: "the language model" where the script says "a language model". Synthetic speech without noise is an easy case, so this says nothing about accuracy on real recordings.
Write SRT subtitles
SRT is a numbered list of time ranges and text. This block formats the recorded segments above; in your own script, pass the segments list from transcribe().
def srt_time(seconds):
ms = round(seconds * 1000)
h, ms = divmod(ms, 3_600_000)
m, ms = divmod(ms, 60_000)
s, ms = divmod(ms, 1000)
return f"{h:02}:{m:02}:{s:02},{ms:03}"
def to_srt(segments):
return "\n".join(f"{i}\n{srt_time(s['start'])} --> {srt_time(s['end'])}\n{s['text']}\n"
for i, s in enumerate(segments, 1))
# recorded segments: Whisper base on the audio extracted from the 12 s test clip
segments = [
{"start": 0.00, "end": 3.20, "text": "Video analysis starts by choosing which frames to look at."},
{"start": 3.20, "end": 6.48, "text": "The detector then labels objects in each selected frame."},
{"start": 6.48, "end": 11.76, "text": "Speech is transcribed separately, and the language model "
"writes a short summary at the end."},
]
print(to_srt(segments), end="")
1
00:00:00,000 --> 00:00:03,200
Video analysis starts by choosing which frames to look at.
2
00:00:03,200 --> 00:00:06,480
The detector then labels objects in each selected frame.
3
00:00:06,480 --> 00:00:11,760
Speech is transcribed separately, and the language model writes a short summary at the end.
Keep the JSON as your source record and generate SRT from it. The package also installs a command that writes subtitle files directly:
whisper talk.mp4 --model base --output_format srt
Its defaults differ from the Python call: the command uses the turbo model unless you pass --model, and decodes with a beam of 5, while transcribe() decodes greedily unless told otherwise.
Segment boundaries are not stable
The same sentences can be cut differently when the model, the input file or the decoder settings change. On the clip above, two model sizes disagreed on both the wording and the number of segments:
Segments from tiny and base on the same 12 second clip
Two rows on a twelve second axis. The tiny model produces two long segments that split near six and a half seconds. The base model produces three segments that split at 3.2 and 6.48 seconds.
| lane | holds |
|---|---|
| tiny | tiny 1: sentences one and two |
| base | base 1 |
| event | lane | time |
|---|---|---|
| tiny 1: sentences one and two | tiny | 0 to 6.54 s |
| tiny 2: sentence three | tiny | 6.54 to 11.88 s |
| base 1 | base | 0 to 3.2 s |
| base 2 | base | 3.2 to 6.48 s |
| base 3 | base | 6.48 to 11.76 s |
At 0 s. tiny: tiny 1: sentences one and two. base: base 1.
tiny also rendered "Video analysis" as "The Dionylysis" and "model" as "noddle". And base itself returned two segments, not three, when given the original 16 kHz mono recording in place of the extracted WAV. Treat segment times as good for finding a passage, and verify against the audio where an exact cut matters.
Word-level times are available through word_timestamps=True, which the command-line help labels experimental; it was not tested here. Whisper does not label speakers.
Why the analyzer's report has no times
The analyzer extracts the soundtrack with moviepy to a temporary WAV, runs transcribe() with task="transcribe" and the model chosen by --whisper-model (default base), and keeps result["text"] and result["language"]. The segment list is discarded. The JSON report holds one transcript string and transcript_language.
To get the transcript this way, install the full requirements and the package itself, then switch the other stages off:
git clone https://github.com/arashsajjadi/ai-powered-video-analyzer.git
cd ai-powered-video-analyzer
python -m pip install -r pip_requirements.txt
python -m pip install -e . # the requirements file does not install the analyzer
ai-video-analyzer analyze talk.mp4 --backend none --no-captioning \
--no-audio-events --no-summarization --whisper-model base -o results
Things to know about this stage in version 1.1.0:
- There is no switch that turns transcription off; it runs whenever audio extraction succeeds.
- If Whisper is missing or transcription fails, the transcript is empty, the language is
unknown, and the report's limitations list says nothing about it. - Whisper uses a CUDA GPU if it finds one;
--devicedoes not control it. --languagepasses a language to Whisper; without it the language is detected.
Line up speech with sampled frames
The analyzer samples video frames and records each one's timestamp_sec. With your own segments you can attach the spoken sentence to each frame. For the test clip, the sampled times were 0, 1, 2 and so on up to 11 seconds.
def segment_at(t, segments, offset=0.0):
"""1-based number of the segment spoken at video time t, or None."""
return next((i for i, s in enumerate(segments, 1)
if s["start"] + offset <= t < s["end"] + offset), None)
# the analyzer sampled this clip once per second: frame_observations[].timestamp_sec
frame_times = [float(t) for t in range(12)]
for number in (1, 2, 3, None):
hits = [t for t in frame_times if segment_at(t, segments) == number]
print(f"segment {number}: frames at {hits}")
segment 1: frames at [0.0, 1.0, 2.0, 3.0]
segment 2: frames at [4.0, 5.0, 6.0]
segment 3: frames at [7.0, 8.0, 9.0, 10.0, 11.0]
segment None: frames at []
Spoken segments and sampled frames on one time axis
Three speech segments from Whisper base lie on a twelve second axis above twelve sampled frames at one second spacing. Four frames fall in the first sentence, three in the second and five in the third.
| lane | holds |
|---|---|
| Speech (Whisper base) | choosing frames |
| Sampled frames | a sampled frame |
| event | lane | time | Sampled frames |
|---|---|---|---|
| choosing frames | Speech (Whisper base) | 0 to 3.2 s | — |
| detector labels | Speech (Whisper base) | 3.2 to 6.48 s | — |
| transcript and summary | Speech (Whisper base) | 6.48 to 11.76 s | — |
Sampled frames: 12 frames in 12 s (1 per second).
At 0 s. Speech (Whisper base): choosing frames. Sampled frames: a sampled frame.
The offset argument is the audio stream's late start discussed above; leave it at zero if you extracted with silence padding or both streams start at zero. The alignment is only as good as both clocks: the analyzer computes frame times as frame index divided by the nominal frame rate, which is wrong for variable-frame-rate recordings. Sharing a time axis also does not show that a sentence is about what the frame displays.
Failure modes and mitigations
| Symptom | Likely cause | What to try |
|---|---|---|
| Wrong words, especially names and terms | Model limits, unusual voice or audio | A larger model; check against the audio |
| A word or phrase repeated many times | Repetition loop, sometimes triggered by odd audio | Check the extracted audio; condition_on_previous_text=False |
| Text where nobody speaks | Hallucination on silence, music or noise | Listen to those stretches; cut them out before transcribing |
| Wrong language | Detection uses the first 30 seconds | Pass language="en" (or the right code) |
| Missing first or last words | Known long-form failure | Compare the ends with the audio |
| Timestamps shifted against the picture | Audio stream starts later than the video | Pad on extraction or add the offset |
No noisy recordings were tested for this article. Background noise, overlapping speakers, music, distant microphones and unfamiliar accents are the conditions to try on your own material before trusting a transcript. OpenAI's README also states that performance varies widely between languages.
The Whisper authors list repeat loops, skipped words at the edges of a segment and complete hallucination as failure modes of long-form transcription. By default transcribe() retries a window at a higher temperature when its output looks too repetitive or has a low average log-probability, which reduces but does not remove these problems.
One case seen in testing is worth a specific warning. The analyzer extracts audio with moviepy (version 1.0.3 in this test). When a test file's audio stream was shorter than its video stream (11.98 s against 17.9 s), the extracted WAV ran to the video's length, filled with an exactly repeating loop about half a second long. Whisper base transcribed that tail as "End. End. End." and so on. The same file extracted with the ffmpeg command above produced a WAV of about 12 s. This was observed on synthetic files; it does not show that every such video loops. Compare stream durations with ffprobe when a transcript ends strangely.
Privacy
Whisper runs locally, and the analyzer has no upload code. Network access is still needed to install packages and to download model weights the first time; Whisper keeps them in a per-user cache folder. A transcript is a readable copy of what was said, so protect the JSON, SRT and report files as you protect the recording.
Speech is only part of a soundtrack. For non-speech sounds, see audio event detection with PANNs.
To turn a transcript and frame evidence into a checked summary, see local video summarization with Ollama.
For the complete first run of the pipeline, start with how to analyze videos locally.
References
- A. Radford, J. W. Kim, T. Xu, G. Brockman, C. McLeavey, I. Sutskever. Robust Speech Recognition via Large-Scale Weak Supervision. arXiv:2212.04356
- OpenAI. Whisper repository and README. github.com/openai/whisper
- FFmpeg documentation. ffmpeg.org/ffmpeg.html
- A. Sajjadi. AI-Powered Video Analyzer, source repository, version 1.1.0. GitHub