Skip to main content

The Math Behind a Seamless, Beat-Synced Video Loop

A Spotify Canvas repeats dozens of times while one song plays. Three small calculations decide whether anyone notices the seam.

Updated 6 min readMarkdown

A looping video has one job that a normal video does not: the last frame has to lead straight back into the first, over and over, without the viewer seeing a jump. For a music loop there is a second job. The motion is driven by the song, so the loop also has to restart on the beat. Get either wrong and the seam shows up every few seconds for as long as the track plays.

This post walks through the three calculations an audio visualizer uses for its loops: choosing the length, snapping it to the frame grid, and hiding the seam. The examples use Spotify Canvas rules, a vertical loop of 3 to 8 seconds, but the same math works for any looping clip.

1. Choose A Length That Is A Whole Number Of Beats

The user asks for a length, say 6 seconds. If the song's tempo is known, that request is turned into a whole number of beats, because a loop that ends half a beat early stutters at every restart.

const CANVAS_LOOP = { min: 3, max: 8 };

function loopLength(requested, bpm) {
  const target = Math.max(CANVAS_LOOP.min, Math.min(CANVAS_LOOP.max, requested));
  if (!bpm || bpm <= 0) return target;
  const beat = 60 / bpm;
  let beats = Math.max(1, Math.round(target / beat));
  while (beats * beat > CANVAS_LOOP.max + 1e-9 && beats > 1) beats--;
  while (beats * beat < CANVAS_LOOP.min - 1e-9) beats++;
  return beats * beat;
}

Clamp first, then round to the nearest beat, then fix the edge cases where rounding pushed the result outside the allowed range. At 120 BPM a beat is 0.5 seconds, so a 6-second request is exactly 12 beats and comes back unchanged. At 128 BPM a beat is 0.46875 seconds; 6 seconds is 12.8 beats, which rounds to 13, so the loop becomes 6.09375 seconds. Still inside the limits, and now the pulse lands on the restart.

The two while loops matter for slow songs. At 40 BPM a beat is 1.5 seconds, and a request for 8 seconds rounds to 5 beats, 7.5 seconds, which is fine. But at 22 BPM, a pathological case from a bad tempo estimate, a single beat is already longer than 2.7 seconds, and you need the guards to stay inside 3 to 8.

2. Snap The Length To The Frame Grid

A video is a whole number of frames. A loop of 6.09375 seconds at 30 fps is 182.8 frames, and nobody can encode 0.8 of a frame. Round to the nearest frame:

function loopFrames(length, fps) {
  return Math.max(1, Math.round(length * fps));
}

That rounding moves the end by at most half a frame, about 17 ms at 30 fps or 8 ms at 60 fps. The beat alignment survives because the error is far below what the eye can catch in motion. What you must not do is compute the frame count from the requested length and the beat alignment separately; derive both from the same number, or the two drift apart.

3. Hide The Seam With A Crossfade Into The Start

Even with a perfect length, the picture at the end of the loop is not the picture at the start. The music has moved on: the spectrum, the loudness and the kick envelope at second 6 are not those at second 0. Cut straight from one to the other and the visual jumps.

The fix is to blend the end of the loop into the material that leads into its start. The last fade seconds of the loop crossfade toward the features from just before the loop's start point, so by the final frame the scene is already in the state the first frame will show.

const fade = Math.min(1, length * 0.25);   // at most 1 s, at most a quarter of the loop
const start = Math.max(fade, Math.min(requestedStart, duration - length));

function sampleAt(t) {          // t in 0..length
  const now = features(start + t);
  if (t < length - fade) return now;
  const x = (t - (length - fade)) / fade;       // 0 → 1 across the fade
  const lead = features(start - fade + (t - (length - fade)));
  return mix(now, lead, x);                      // ends exactly on the run-up to the start
}

Two details make this work. The fade is capped at a quarter of the loop, so a 3-second loop does not spend a whole second blending. And the start point is pushed to at least fade seconds into the song, so there is real material before the start to blend toward. If the requested start is at 0, it moves slightly later.

Because the blend targets the run-up to the start rather than the start frame itself, motion keeps its direction through the seam. A halo that is growing just before the start is also growing at the end of the loop, instead of freezing for a frame and reversing.

Putting It Together

For a Canvas, the user drags a window onto the hook, asks for a length, and the three steps run in order: snap the length to beats, round it to frames, and crossfade the tail into the run-up to the start. A spotify canvas maker built this way never shows the seam as a jump, because the end of the loop is constructed to arrive where the start begins.

The same math carries over to other loops. A 9:16 clip for social apps from a tiktok visualizer does not need to loop seamlessly, but an animated album cover exported as a looping GIF does, and the same length, frame and fade rules apply.

What I Would Do Differently

If I started again, I would treat the tempo estimate as untrusted from day one. A wrong BPM produces a perfectly aligned loop on the wrong grid, which looks worse than no alignment at all. The guards in loopLength keep the result inside the allowed range, but they cannot tell a good tempo from a bad one. Letting the user nudge the length in half-second steps and watch the loop restart in the preview before exporting matters as much as the math.

About the author

  • I write practical notes on music videos, visualizers and release visuals, mostly about what happens inside the browser: audio analysis, WebCodecs and making long renders survive real machines.
CClickPop16h ago

Clear explanation of snapping the loop length to the frame grid. The same idea pays off on short-form platforms where replays count. A clip whose last frame flows back into the first gets rewatched without people noticing it looped. Have you tried a short crossfade versus a hard cut on the downbeat? On busy visuals the hard cut on the beat sometimes hides the seam better.

DDemoVexa1d ago

Deriving the frame count and the beat alignment from one number is the detail most loop tools get wrong, and the drift only shows after a dozen repeats. The guards for a bad tempo estimate are worth having too. One thing I'd add: render at the frame rate the platform will actually play. If a platform re-encodes a 30 fps file to a different rate, the half frame you rounded away can come back at the seam.