Repository navigation
Filter beatdetect
beatdetect reports a track's tempo in BPM, where its beats fall, and how much to trust the answer. It is an audio filter. The audio passes through unchanged. The result is written as frame metadata and to the log.
The filter comes from the ffmpeg-beatdetect project, pinned to a release. This build ships beatdetect 1.4.1. ffmpeg -h filter=beatdetect prints the version.
ffmpeg -hide_banner -nostats -i song.flac -af beatdetect,ametadata=mode=print:file=- -f null - | tail -12Excerpt of the output:
lavfi.beatdetect.bpm=128.01101
lavfi.beatdetect.confidence=0.882
lavfi.beatdetect.beat_interval_ms=468.7097
lavfi.beatdetect.beat_offset_ms=461.67
lavfi.beatdetect.final=1
How the method works: an onset envelope from a spectral-flux STFT, autocorrelation over it, a 400-band comb filter bank across 40 to 200 BPM, and a tempo-octave decision made from the onsets themselves. It does not use a preferred tempo range unless you ask for one (tempo_prior=general).
The table is copied from ffmpeg -h filter=beatdetect of v1.0.50.
| Option | Type | Default | Range | What it does |
|---|---|---|---|---|
window_ms |
int | 46 |
5 to 400 | Analysis window length in ms. |
hop_ms |
int | 12 |
1 to 100 | Analysis hop length in ms. |
window_size |
int | 0 |
0 to 16384 | Deprecated. Window length in samples. It overrides window_ms. |
hop_size |
int | 0 |
0 to 16384 | Deprecated. Hop length in samples. It overrides hop_ms. |
peak_threshold |
double | 0.3 |
0.1 to 1 | Deprecated. Read by nothing since 1.0.0. Accepted for compatibility. |
tempo_prior |
int | none |
0 to 1 | Tempo range preference. none (0) decides the octave from the onset evidence. general (1) prefers 80 to 110 BPM and the pre-rework sweet spots. |
downmix |
int | front |
0 to 1 | How multichannel input is folded to mono. front (0): front channels, LFE dropped, surrounds at half weight. all (1): equal average of every channel. |
hpss |
boolean | false |
Build the beat grid from the percussive part only (median-filter mask). | |
onsets |
boolean | false |
Carry the onset strength per hop on every frame, as t_ms:full:low. |
|
beats |
boolean | false |
Report the beat list on the final frame, as beatsN keys of eight times. |
|
flux_lo_hz |
double | 30 |
0 to 20000 | Lower edge of the spectral flux band. |
flux_hi_hz |
double | 5000 |
100 to 96000 | Upper edge of the spectral flux band. |
Every frame carries the running estimate. The last frame carries the answer for the whole stream. The filter holds one frame back for this, which is why ametadata=mode=print ends with the final values. An API consumer reads the frame with final=1.
Each key is lavfi.beatdetect.<key>.
| Key | Meaning |
|---|---|
bpm |
The tempo. Five decimals. A running estimate on earlier frames, the final tempo on the last. |
confidence |
0 to 1. See below. |
beat_interval_ms |
60000 / bpm. Last frame only. |
beat_offset_ms |
The time of the first beat on the grid, from the stream start. Last frame only. Beat n falls at beat_offset_ms + n * beat_interval_ms. |
tempo_spread |
Drift in one number: the tempo read over 30 s windows, as an IQR percentage of the median. Absent when fewer than three windows read the track's tempo. |
candidateN |
Each octave candidate as bpm:fit:offset_ms, strongest comb response first. Offset is -1 when the candidate never got a phase. Last frame only. |
phase_stepN |
Each step in the grid phase as at_ms:delta_ms, in order of time. Absent when there is none. Last frame only. |
phase_bpm |
The tempo that the phase trace reads. bpm is the better number on real music. |
phase_noise_ms |
The scatter of one 10 s phase reading. Absent when the input is too short for four readings. |
downbeat_offset_ms |
The time of the first beat one on the grid. Last frame only. |
beats_per_bar |
4 or 3. Last frame only. |
downbeat_confidence |
0 to 1. Use 0.6 and above as the gate to start from. |
tempo_segmentN |
The tempo map: each stretch of one tempo as start_ms:end_ms:bpm. Last frame only. |
no_pulseN |
start_ms:end_ms per stretch of the beat list with no pulse under it. Absent on a track with a pulse throughout. Last frame only. |
tempo_wander |
How far the tempo moves: the p10 to p90 width of the local tempo over 16-beat spans, as a percentage of its median. 0.7 is the flag. Last frame only. |
beatsN |
With beats=1, on the last frame: the beat list, eight times in ms per key (t:t:t:...). |
onsets |
With onsets=1, on every frame: the hops since the previous frame, each as t_ms:full:low. |
final |
1 on the last frame, 0 before. |
version |
The beatdetect version. On every frame. |
confidence is built from three things, measured at the end of the stream:
- Grid fit. How well the chosen beat grid explains the onsets.
- Separation. How far the runner-up octave's fit is below the winner's. A coin flip between 70 and 140 reads as low confidence.
- Stability. The share of the rolling detections that agree with the final tempo.
On real music, confidence works as a don't trust this flag. It does not pick the octave. When no periodicity is found at all, the filter reports bpm=0 with confidence=0. It never reports a round number that looks like a tempo.
A track can open or end without its beat: silence, a tone, pads, a voice. The filter locks onto the beat where it is heard. Since beatdetect 1.4.1 the beats before the first lock and after the last are counted from the lock, on the straight line through the beats it heard there. So the whole beat list is on one grid.
- A stretch without the beat does not become a tempo segment of its own (
tempo_segmentN). - A lock that is not straight over 64 beats, for example a live drummer, is left as the tracker found it.
Before 1.4.1 the beat list followed whatever moved in such a stretch. On one real track it ran half a beat out for 34 s and then slid onto the kit over 28 beats. On that track the first 45 s are now a median 0.7 ms from the kit's grid. It was 227 ms.
Compared with 1.4.0, values move in beatsN, tempo_wander, no_pulseN and tempo_segmentN on tracks with such a stretch. On 48 library tracks, 36 beat lists changed, most by the few beats before the first hit or after the last. bpm, confidence, beat_offset_ms and the bar do not move.
The pre-rework output is kept byte for byte, because other programs scrape it:
lavfi.beatdetect.bpm=128.05
It is written to the log at info level and also straight to stderr, so it appears twice. The stderr copy ignores -loglevel. New code should read the metadata instead. To read the tempo from the log:
ffmpeg -hide_banner -nostats -i song.flac -af beatdetect -f null - 2>&1 \
| sed -n 's/.*lavfi\.beatdetect\.bpm=\([0-9.]*\).*/\1/p' | tail -1The filter is in every platform archive. It needs no model and no extra files. It is built into FFmpeg without extra libraries.
v1.0.46 to v1.0.49 ship beatdetect 1.4.0. v1.0.50 ships 1.4.1.
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