Skip to content

Filter keydetect

Phillippe Pelzer edited this page Nov 15, 2026 · 1 revision

keydetect

keydetect detects the musical key and the chord progression of a track. 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-keydetect project, pinned to a release. This build ships keydetect 1.6.0. ffmpeg -h filter=keydetect prints the version.

Quick example

ffmpeg -i song.flac -af keydetect,ametadata=mode=print:file=- -f null -

Every value is a running total over the stream so far. The last frame holds the answer for the whole stream. It is marked lavfi.keydetect.final=1.

Options

The table is copied from ffmpeg -h filter=keydetect of v1.0.47.

Option Type Default Range What it does
window_ms int 190 50 to 500 Analysis window length in ms.
hop_ms int 50 10 to 500 Analysis hop length in ms.
profile int shaath 0 to 3 Key profile for the major keys. Also for the minor keys with minor_profile=same.
minor_profile int edma -1 to 3 Key profile for the minor keys. same takes the minor row of profile.
tuning_hz double 440 400 to 480 Reference A4 frequency in Hz.
detect_tuning boolean true Estimate the tuning deviation from the signal.
threshold float 0.55 0 to 1 Minimum chord template similarity.
sevenths boolean true Also match seventh and ninth chord templates. They are reported as their triad.
overtones boolean true Every chord tone of the templates also expects its fifth harmonic, a major third up.
bass_root boolean true Lift the two triads on a clearly played bass note.
silence_threshold double -60 -120 to 0 Silence RMS threshold in dB.
smooth_ms int 1000 100 to 5000 Chord score smoothing length in ms.
min_chord_ms int 400 100 to 5000 Minimum chord duration in ms.
compression float 100 1 to 10000 Chroma log compression factor.
salience float 1 0 to 1 Weight the key chroma by how tonal each window is. 0 disables it.
rerank boolean true Break near ties in the key correlation using the chord progression.
rerank_margin double 0.12 0 to 1 Consider keys within this correlation distance of the leader.
rerank_gap double 0.05 0 to 2 Chord score the challenger must win by at zero correlation deficit.
rerank_slope double 0.5 0 to 20 Extra chord score demanded per unit of correlation deficit. 0 restores a hard window.
rerank_floor double 0.5 0 to 2 Absolute chord score a challenger must reach to override the chroma. 0 removes the floor.
mode_balance boolean true Equalise the harmonic function weights between a key and its relative.
transition float 1 0 to 4 Weight of cadence transitions in the chord score.
tonic_bonus float 1 0 to 4 Weight of the first and last tonic-chord bonuses in the chord score.
bass boolean true Accumulate a bass-register chroma and report it as metadata.
chroma boolean false Report the chroma and bass band of every analysis window as frame metadata.

Profiles for profile and minor_profile:

Value Source
same (-1, minor_profile only) The minor row of profile.
krumhansl (0) Krumhansl-Kessler (1982).
temperley (1) Temperley-Kostka-Payne (2005).
shaath (2) Sha'ath (2011), general audio.
edma (3) Faraldo (2016), electronic and pop.

Frame metadata

Each key is lavfi.keydetect.<key>.

Key Meaning
version The keydetect version. Always present.
key The answer, for example Fm.
key_confidence Pearson r of the winning key.
key_r_leader r of the correlation leader. Use this to compare r across tracks.
key_alt The runner-up key.
key_alt_confidence r of the runner-up.
key_margin The gap between the top two keys.
key_candidates The candidate keys with their r.
key_reranked 1 when chord evidence overrode the chroma. Then key_confidence is the challenger's r, below the leader's.
tonalness How pitched the material is.
chord_coherence How well the chords fit any key.
tuning_cents Cents away from the tuning_hz grid, from -50 to +50. Absent with detect_tuning=0 or before a pitched window. Absent means not measured.
bass_tonic The strongest pitch class between E1 and E3.
bass_chroma The whole bass band, C first.
chord The current stable chord.
chord_start When that chord began, in seconds from the first sample.
chords The progression so far.
chroma<N>, bass<N> With chroma=1: the chroma and the bass band of each analysis window as time:12 values, C first. <N> counts from 0 within the frame.
key_profiles The answer of every profile off the same chroma. Only on the last frame.
final 1 on the last frame, 0 before.

The filter holds one frame back so it can mark the last one. Frames leave one frame later than they came in. Their values, order, timestamps and audio are untouched.

Reading the confidence

key_confidence alone answers almost nothing. Three fields answer three questions. Trust a result only when all three agree.

Field Question A bad value looks like
tonalness Is there pitched material at all? Low on percussive or sparse tracks.
chord_coherence Does that material form a key? At or below 0 on chromatic riffs.
key_margin Is the tonal centre within that key decided? Near 0 on relative-key ties.

Chord times

A chord is reported once the filter is sure of it. That is later than the chord began. chord_start is when it began. hop_ms is its grain. On the test corpus it lands within 10 ms of the true change for struck chords and within 60 ms for held chords and chords that follow silence.

Platform notes

The filter is in every platform archive. It needs no model and no extra files.

Since

Version Change
v1.0.46 (keydetect 1.6.0) New option minor_profile, default edma. minor_profile=shaath restores the 1.5.0 key decision exactly. Chords and every other option are unchanged.

Clone this wiki locally