Repository navigation
Whisper Word Timestamps
The whisper filter transcribes speech with whisper.cpp. The filter itself is upstream FFmpeg. The upstream filter documentation is at ffmpeg.org/ffmpeg-filters.html#whisper.
This page covers the NoMercy patch. It adds six options for aligning known text to audio, such as lyrics to a vocal stem (see Filter-stemsplit for making the stem). It also adds per-word times to the JSON output.
The language report is a separate patch: Whisper-Language-Detection.
Since: v1.0.45.
Every new option defaults to the old behaviour. Without them, format=json and format=srt output is what it was before v1.0.45. The one deliberate exception is the segment end rule below. Since v1.0.50 every JSON segment line also has a language field. That one comes from FFmpeg 9.1, not from this patch.
# Word times with DTW, on a vocal stem, as JSON Lines on stdout
ffmpeg -i vocals.flac -af "whisper=model=ggml-base.en.bin:format=json:destination=-:dtw=base.en:word_timestamps=1" -f null -
# Steer the transcript with known lyrics from a file, with beam search
ffmpeg -i vocals.flac -af "whisper=model=ggml-base.en.bin:format=json:destination=-:prompt_file=lyrics.txt:beam_size=5" -f null -The table is copied from ffmpeg -h filter=whisper of v1.0.47. Only the options this patch adds are listed.
| Option | Type | Default | Range | What it does |
|---|---|---|---|---|
dtw |
string | empty (off) | DTW token timestamps with this alignment-heads preset. Turns flash attention off. See the preset list below. | |
word_timestamps |
boolean | false |
Adds a words array to each format=json segment. With format=text or srt it logs a warning and changes nothing. |
|
prompt |
string | empty | whisper's initial prompt. Only its last 223 tokens or so are used. | |
prompt_file |
string | empty | The same prompt, read from a UTF-8 file. It wins over prompt when both are set. |
|
beam_size |
int |
0 (greedy) |
0 to 8 | Beam search with this beam size. |
best_of |
int |
0 (library default, 5) |
0 to 8 | Candidates for greedy decoding and for the temperature fallback. |
The dtw presets are tiny, tiny.en, base, base.en, small, small.en, medium, medium.en, large.v1, large.v2, large.v3 and large.v3.turbo. Use the one named after the model file.
prompt_file is for long prompts. It also saves you the filtergraph escaping. A file that cannot be read fails at start-up and the error names the path.
The filter also has vad_model, vad_threshold, vad_min_speech_duration and vad_min_silence_duration. Those are upstream options. The patch changes how a VAD run ends its segments (next section) and how many threads the VAD context gets.
The output is JSON Lines. The detected_language line comes first. With word_timestamps=1 a segment line looks like this. Times are absolute milliseconds, like start and end.
{"start":0,"end":2000,"text":"And so am I fellow Americans.","language":"en","words":[{"text":"And","start":760,"end":1040,"p":0.745},{"text":"so","start":1040,"end":1400,"p":0.918}]}This line shape was produced on v1.0.50 with ggml-tiny.en.bin, dtw=tiny.en and word_timestamps=1. The language field is new in v1.0.50; see Whisper-Language-Detection.
How the word times are made:
- A word starts where its first token starts. That is the DTW time when
dtwis set and DTW placed the token. Otherwise it is the token's own timestamp. - Starts stay inside the segment and never go backwards. DTW can place a token slightly outside its segment.
- A word ends where the next one starts. The last word ends with its segment.
-
pis the mean probability of the word's tokens, between 0 and 1. - Text that whisper cut in the middle of a UTF-8 character comes out as
U+FFFD. So every line is valid JSON. Valid text is never changed.
A segment, and so its last word, ends inside the audio its window held. whisper pads every window to 30 s. A segment without a closing timestamp token used to end where that padding ends. With vad_model that was up to 30 s past the speech, overlapping the next segment. A segment that starts in the padding came from nothing in the audio and is not written.
This is the one default that changed in v1.0.45. Everything else was byte for byte what v1.0.44 wrote, until FFmpeg 9.1 added the language field to the JSON lines in v1.0.50.
| Topic | What happens |
|---|---|
dtw and flash attention |
whisper.cpp 1.9.1 turns DTW off silently when flash attention is on. The filter switches flash attention off whenever dtw is set and logs it. |
| Preset must fit the model | The filter compares the preset with the model's shape and refuses a mismatch. Without the check, base on ggml-base.en.bin would load and give wrong times. An unknown preset name also fails at start-up. |
| Prompt length | whisper keeps the last n_text_ctx/2 tokens, about 223 for the released models. The prompt applies to every transcription window, not only the first. |
| Very short DTW windows | whisper.cpp 1.9.1 killed the whole process when a DTW window was under about 160 ms. The build carries upstream's fix. |
| Threads | By default whisper uses half the physical cores, like stemsplit. -filter_threads N is followed exactly, never above the CPUs the process may use. The VAD context gets at most 4. The log says whisper: N threads. The thread count does not change the output. |
| Model load | The model loads once per run, when the real filter graph is configured. A missing model fails then, before any output. |
| Writing to stdout | Use destination=-. It means pipe:1. The written-out form needs its colon escaped in a filtergraph. |
The patched filter is in every platform archive. You need a ggml model file. The release archives do not contain one. Get whisper.cpp models from https://huggingface.co/ggerganov/whisper.cpp. A dtw preset needs the matching model.
For GPU and CPU details see GPU-Acceleration, Metal and CPU-Instruction-Sets.
Users
Filters and components
- Filter-stemsplit
- Filter-lyricsalign
- Whisper-Language-Detection
- Whisper-Word-Timestamps
- Filter-trailingsilence
- Filter-keydetect
- Filter-beatdetect
- Filter-rubberband
- Muxer-Sprite-Sheet
- Muxer-Chapter-VTT
- Muxer-VOBsub
- Protocol-dvdread
- Protocol-OmniDrive
- Auto-Create-Directories
- AACS-and-BDplus
- HLS-Master-Playlist
- Subtitles-OCR
- HEVC-Alpha
Acceleration and codecs
Contributors