Zack Design’s audio-analysis-and-recut is a single Python script, plus FFmpeg, that rebuilds a band’s live arrangement from the song’s studio recording. Given a 1 minute 56 second phone recording of a band playing “Ya Te Olvide” live, it works out which five sections of the 4 minute 40 second studio original they played, and in what order. Then it cuts those sections out of the studio track, so you get the live arrangement at studio quality. The matching uses the fast Fourier transform and no learned components.
This post covers:
- why the two recordings differ, and why dancers need the recut;
- a filter that keeps only the 200 to 4,000 Hz range, so the vocals dominate and the crowd does not;
- a sliding comparison of 5-second chunks against the whole original, done with the fast Fourier transform and normalised by local loudness;
- grouping the matches into sections wherever the offset stays constant;
- why cut points need a human ear: the two-and-a-half-second boundary uncertainty that a 5-second window buys you, and the two manual overrides it needs;
- why seeking the MP3 per piece puts the joins slightly wrong (it lands on the nearest compressed frame), and the one-input FFmpeg filter graph, with the sample-accurate
atrimfilter, that cuts all five from one decode; - how to run it on your own song.
Why it exists
Dancers at Havana on the Hastings rehearse to the studio recording, the one on Spotify, and perform to the band’s live arrangement: the pre-intro skipped, the breakdown compressed, a different ending. A choreography that works to one doesn’t line up with the other. The phone recording is no good to rehearse with: crowd noise, the venue’s speakers, and a phone microphone’s idea of bass. The recut is the live structure with clean audio to cue on.
Compare only the vocal range
FFmpeg decodes both files to mono 16-bit samples at 22,050 per second, and NumPy reads them straight from its output. The band-pass filter is the bluntest possible one: transform to frequencies, zero everything outside 200 to 4,000 Hz, and transform back.
def bandpass(audio, sr, low=200, high=4000):
freqs = np.fft.rfftfreq(len(audio), 1.0 / sr)
spectrum = np.fft.rfft(audio)
mask = (freqs >= low) & (freqs <= high)
spectrum[~mask] = 0
return np.fft.irfft(spectrum, len(audio))
That is crude, and for matching it doesn’t matter. The point is to make the comparison depend on the melody and words, not on room rumble and cheering. The average level is subtracted from both signals afterwards.
Finding where each five-second chunk sits in the original
For each short piece of the live recording, the question is: where in the studio track does this sound most alike? Sliding the piece along the whole original and scoring every position is called cross-correlation. Done directly, it is slow.
The fast Fourier transform turns that sliding comparison into one multiplication. The original is transformed once, padded with zeros to the next power of two. Then a 5-second chunk of the performance is taken every half second. Each one is reversed, transformed, multiplied against the original and transformed back. That gives its match score at every possible position in one pass.
The best position’s score is normalised by the chunk’s own loudness and by the original’s loudness over the same window. The window loudness comes from a running total rather than a loop:
orig_sq = orig_bp ** 2
cs = np.insert(np.cumsum(orig_sq), 0, 0)
...
window_energy = cs[top_lag + chunk_n] - cs[top_lag]
ncc = corr_valid[top_lag] / (chunk_norm * np.sqrt(window_energy))
Each chunk yields a time in the performance, a time in the original, a normalised score, and the offset between the two.
Grouping into segments
A verse played straight through produces a run of chunks that all agree on the same offset. detect_segments walks the results and starts a new segment whenever the offset moves by more than 3 seconds. Anything shorter than 2 seconds is dropped as noise. On this recording that produces five segments:
SEGMENT MAP:
1 0:00-0:19 | Orig 0:12-0:30 | 18.5s (intro/verse start)
2 0:19-1:04 | Orig 0:50-1:35 | 44.5s (verse/chorus)
3 1:04-1:36 | Orig 2:44-3:15 | 31.5s (montuno section)
4 1:36-1:46 | Orig 4:13-4:23 | 9.5s (ending)
5 1:46-1:48 | Orig 4:33-4:35 | 2.0s (final tag)
Skipped from original:
0:00-0:12 (11.8s) - pre-intro
0:30-0:50 (20.1s) - transition/repeat
1:35-2:44 (69.1s) - repeated verse section
3:15-4:13 (57.9s) - extended montuno/breakdown
4:23-4:33 (10.3s) - outro padding
The map reads like a cut list. The band skipped the pre-intro, dropped a repeated verse and most of the breakdown, and landed on a different ending.
One thing the recut does differently from the map: it keeps the studio intro. If the first matched section starts more than a second into the original, build_recut puts everything before it at the front. Here that is the 12-second pre-intro.
Why cut points need a human ear
A 5-second window locates a section, not a downbeat. Each boundary can be wrong by up to two and a half seconds, half the window, either way. On this song that is audible at the first cut.
This song is fixed by hand. Listening, and using the loudness profile to find a natural phrase boundary, gives two overrides:
OVERRIDES = {
0: {"orig_end": 25.0}, # Cut 1 exit: 0:30.29 -> 0:25.00
1: {"orig_start": 45.0}, # Cut 1 entry: 0:50.41 -> 0:45.00
}
They’re in analyze.py, applied after the analysis and printed as they’re used. They don’t solve the problem generally. A second pass with a shorter window around each boundary would, or beat tracking.
Cutting all five pieces from one decoded file
build_recut decodes the original once and trims every piece out of that one stream:
f"[0:a]atrim=start={seg['orig_start']:.3f}:end={seg['orig_end']:.3f},asetpts=PTS-STARTPTS[s{idx}]"
...
filter_complex = ";".join(trim_parts) + f";{concat_inputs}concat=n={idx}:v=0:a=1[out]"
Seeking into the MP3 separately for each piece, with -ss and -t, would land each cut on the nearest compressed frame instead of the analysed time. One decode gives trims accurate to the sample. asetpts=PTS-STARTPTS restarts each piece’s clock at zero, so concat sees pieces that follow on from each other.
The result is a 16-bit WAV, output/ya_te_olvide_recut.wav, so it can go straight into an editor without another lossy encode.
Trying it
You need Python 3 with NumPy, and FFmpeg with ffprobe on your $PATH.
git clone https://github.com/isaacrowntree/audio-analysis-and-recut.git
cd audio-analysis-and-recut
pip install numpy
mkdir -p staging
cp original_song.mp3 staging/original.mp3
cp performance_recording.mp3 staging/performance.mp3
python3 analyze.py
It prints the segment map and the skipped sections, then writes the recut to output/. The file names and the “Ya Te Olvide” labels are hard-coded in analyze.py. Any song works, but the output keeps that name.
What the approach shows
Keeping only the vocal range and normalising by local loudness was enough to match a phone recording to a studio master through crowd noise. The Fourier transform does the heavy lifting, and there are no learned components.
Window size trades reliable detection against precise boundaries, and at 5 seconds the boundaries need a human ear. Seeking an MP3 once per segment isn’t sample-accurate; trimming one decoded stream is. The repo is one file. Making it general means lifting the constants, the file names and the overrides into arguments. Source on GitHub.
