# How Real-Time Dubbing Stays in Sync With a YouTube Video: Fitting Translated Speech Into Fixed Time Slots

> Source: <https://dev.to/chriscornell/how-real-time-dubbing-stays-in-sync-with-a-youtube-video-fitting-translated-speech-into-fixed-time-2a21>
> Published: 2026-11-23 10:11:34+00:00

**Recommendation:** Drive audio sync from video.currentTime rather than setTimeout, and monitor lateBy across sentences to catch translations that are too long for their speaker.
More in the playbook "Running AI in production": <https://wpnews.pro/playbooks/production-ops>

*I work on EarDub, a YouTube dubbing extension. The code is a teaching version written for this post, not EarDub's source. The table below is the planner's real output; the player sync function was only tested against a mock player, so try it in your target browsers. This post was drafted with AI.*

A dubbed sentence has to start when the speaker starts and, ideally, end before the next sentence begins. The translation rarely cooperates. Keeping a dub in sync therefore comes down to three decisions per sentence: how much silence you can borrow, how far you can speed the voice up, and what to do when neither is enough.

Dubbing researchers call it isochrony: the translated speech should occupy the same time as the original. Federico and colleagues describe it in [From Speech-to-Speech Translation to Automatic Dubbing](https://arxiv.org/abs/2001.06785), along with controlling the length of the translation itself so there's less to fix later.

In a browser you work with less control. The video is already playing, the sentence boundaries come from captions or speech recognition, and the viewer can seek, pause or change the playback rate at any moment.

Say sentence *i* starts at `start` and the next one starts at `nextStart`. The obvious slot is `end - start`. The useful slot is `nextStart - start`, because the pause between sentences can absorb a longer translation.

Leave a small gap so two sentences don't run together:

``` js
const GAP = 0.12;            // seconds of air between sentences
const MAX_RATE = 1.35;       // above this, speech gets tiring

function planSentence(s, nextStart) {
  const slot = Math.max(0.2, nextStart - s.start - GAP);
  const needed = s.voiceDuration / slot;
  const rate = Math.min(MAX_RATE, Math.max(1, needed));
  const spoken = s.voiceDuration / rate;
  return { start: s.start, rate, end: s.start + spoken, overrun: Math.max(0, spoken - slot) };
}
```

The rate never goes below 1. Slowing a short translation down to fill its slot sounds worse than ending early.

When a sentence still doesn't fit at `MAX_RATE`, you can cut it, raise the cap, or let it run late and catch up. Cutting loses meaning, so a late start for the next sentence is usually the better choice, provided the debt is paid back:

``` js
function planAll(sentences, videoEnd) {
  let cursor = 0;
  return sentences.map((s, i) => {
    const start = Math.max(s.start, cursor);
    const nextStart = i + 1 < sentences.length ? sentences[i + 1].start : videoEnd;
    const p = planSentence({ ...s, start }, nextStart);
    cursor = p.end + GAP;
    return { ...p, id: s.id, lateBy: +(start - s.start).toFixed(3) };
  });
}
```

`lateBy` is the number to watch. A couple of hundred milliseconds is hard to notice. If it keeps growing, the translation is too long for this speaker and the fix belongs upstream, in a shorter translation.

Here's what it does with four sentences, where the second one is about 40% too long for its slot:

``` js
const plan = planAll([
  { id: 1, start: 0.0, voiceDuration: 2.0 },
  { id: 2, start: 2.5, voiceDuration: 4.2 },
  { id: 3, start: 5.6, voiceDuration: 1.5 },
  { id: 4, start: 8.0, voiceDuration: 1.8 },
], 11);
console.table(plan);
```

| id | start | end | rate | lateBy | 
|---|---|---|---|---|
| 1 | 0.00 | 2.00 | 1.00 | 0 | 
| 2 | 2.50 | 5.61 | 1.35 | 0 | 
| 3 | 5.73 | 7.23 | 1.00 | 0.131 | 
| 4 | 8.00 | 9.80 | 1.00 | 0 | 

Sentence 2 hits the cap and overruns, sentence 3 starts slightly late, and sentence 4 is back on time because there was silence to absorb the debt.

`setTimeout` drifts and ignores pauses. Read `video.currentTime` instead, and work out which sentence should be playing right now and how far into it you are:

``` js
function sync(video, voice, plan) {
  const t = video.currentTime;
  const p = plan.find(x => t >= x.start && t < x.end);
  if (!p || video.paused) { voice.pause(); return; }
  if (voice.dataset.id !== String(p.id)) {
    voice.dataset.id = p.id;
    voice.src = urlFor(p.id);
  }
  const rate = p.rate * video.playbackRate;
  const want = (t - p.start) * p.rate;
  if (Math.abs(voice.currentTime - want) > 0.25) voice.currentTime = want;
  voice.playbackRate = rate;
  if (voice.paused) voice.play().catch(() => {});
}
```

`urlFor(id)` stands for whatever returns the audio of that sentence. Run `sync` on `timeupdate`, `seeking`, `seeked`, `ratechange`, `pause`, `waiting` and `play`. Two details matter here:

`currentTime` on every tick causes audible clicks. A quarter of a second is a reasonable tolerance.
Browsers keep the pitch constant while changing speed as long as [`preservesPitch`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/preservesPitch) is true, which is the default.

Translation and speech synthesis take longer than a sentence. Keep a window of upcoming sentences ready, for example the next 20 to 30 seconds. When the viewer jumps, cancel everything outside the new window with an `AbortController` before requesting new audio. Otherwise the sentence they need waits behind ten they skipped.

This is also why recorded video can be dubbed in sync and live video can't. With a recording, the next sentence already exists when you need it. In a live stream nobody has said it yet.

Lowering the original to about 20% keeps the laughter, emphasis and timing cues, and it hides small timing errors. Ramp the volume over 150 to 300 ms so it doesn't pump.

| Metric | Why | 
|---|---|
| Median and 95th percentile of `lateBy` | Average lateness hides the bad moments | 
| Share of sentences at `MAX_RATE` | Tells you the translations are too long | 
| Time from seek to first dubbed word | The delay viewers notice most | 
| Resyncs per minute | High numbers mean audible jumps | 

If you'd rather hear the result than build it, [EarDub](https://www.eardub.com) does real-time YouTube dubbing in 75+ languages, for recorded videos rather than live streams. If you build your own, start by logging `lateBy` on a ten-minute interview: two speakers, short turns and few pauses will break a naive scheduler quickest.
