Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
fix(core): slow clips stay on the playhead at reduced preview speeds (#5440)
Miguel Ángel7d958fea0ba0
diff --git a/packages/core/src/runtime/media.test.ts b/packages/core/src/runtime/media.test.tsindex 458c7a5237..6b2e81a80b 100644--- a/packages/core/src/runtime/media.test.ts+++ b/packages/core/src/runtime/media.test.ts@@ -1981,6 +1981,44 @@ describe("syncRuntimeMedia", () => {});});+ it.each([+ [60, 0.25],+ [10, 0.5],+ ])(+ "keeps a video slower than the browser can play on the playhead at %i fps, preview speed %f",+ (fps, speed) => {+ // A 0.1x clip at 0.25x or 0.5x preview speed is below Chromium's 1/16 rate floor.+ const clip = createMockClip({ start: 0, end: 30, duration: 30, playbackRate: 0.1 });+ const el = clip.el;+ let rate = speed;+ let paused = true;+ Object.defineProperty(el, "playbackRate", {+ configurable: true,+ get: () => rate,+ set: (v: number) => {+ if (v < 0.0625) throw new DOMException("unsupported rate", "NotSupportedError");+ rate = v;+ },+ });+ Object.defineProperty(el, "paused", { configurable: true, get: () => paused });+ el.play = vi.fn(() => ((paused = false), Promise.resolve()));+ el.pause = vi.fn(() => void (paused = true));+ const dt = 1 / fps;+ let timeline = 0;+ for (let i = 0; i < fps * 4; i++) {+ timeline += speed * dt;+ if (!paused) el.currentTime += rate * dt;+ syncRuntimeMedia({+ clips: [clip],+ timeSeconds: timeline,+ playing: true,+ playbackRate: speed,+ });+ }+ expect(Math.abs(el.currentTime - 0.1 * timeline)).toBeLessThan(0.05);+ },+ );+// A seek while playing pauses and syncs in one pass, before the video element has paused.it("a seek that pauses mid-playback lands a lagging playing video on the new time", () => {const clip = createMockClip({ start: 3.85, end: 5.6, duration: 1.75 });diff --git a/packages/core/src/runtime/media.ts b/packages/core/src/runtime/media.tsindex 7c7c548e51..7ad1cbabec 100644--- a/packages/core/src/runtime/media.ts+++ b/packages/core/src/runtime/media.ts@@ -256,6 +256,9 @@ export const MEDIA_HARD_SYNC_SECONDS = 0.5;/** Drift a playing audio element may carry before sync pulls it back onto the playhead. */export const MEDIA_SYNC_TOLERANCE_SECONDS = 0.04;+// Chromium refuses a slower playbackRate (NotSupportedError).+const MIN_NATIVE_PLAYBACK_RATE = 1 / 16;+// A playing video is steered back by rate, not seeked (a seek resets its decoder).// Its rate is written only when steering starts or stops: every write costs a frame.const VIDEO_STEER = 0.03;@@ -473,6 +476,8 @@ export function syncRuntimeMedia(params: {if (el.preload !== "auto") el.preload = "auto";// Per-element rate × global transport rateconst baseRate = rateAt(clipRate, params.timeSeconds - clip.start) * params.playbackRate;+ // Too slow for the browser to play: kept paused and stepped onto the playhead by seeks.+ const playing = params.playing && baseRate >= MIN_NATIVE_PLAYBACK_RATE;// Drift correction — three tiers://// 1. Hard sync (0.5s): first tick, timeline jumps (scrub), catastrophic@@ -518,12 +523,14 @@ export function syncRuntimeMedia(params: {// seek→freeze→drift→seek stutter. So a playing video skips strict and force sync; only hard// sync (>0.5s) warrants the decoder-reset cost. A paused transport pauses this video below,// so a seek that pauses mid-playback still lands it.- const isPlayingVideo = el.tagName === "VIDEO" && !el.paused && params.playing;+ const isPlayingVideo = el.tagName === "VIDEO" && !el.paused && playing;// Only apply strict sync when offset has stabilized (not growing).// During initial buffering, offset grows ~16ms/tick as the timeline// advances while media stays at 0. Accumulated drift from pause/play// toggling shows up as a stable, non-zero offset (delta near 0).- const offsetStabilized = prevOffset !== undefined && Math.abs(offset - prevOffset) < 0.004;+ const heldBelowFloor = params.playing && !playing;+ const offsetStabilized =+ heldBelowFloor || (prevOffset !== undefined && Math.abs(offset - prevOffset) < 0.004);let strictSync = false;if (!isPlayingVideo &&@@ -545,8 +552,10 @@ export function syncRuntimeMedia(params: {try {// A hard sync lands the video on the playhead, so its pre-seek offset says nothing.if (!isPlayingVideo || hardSync) videoSteering.delete(el);- const rate =- isPlayingVideo && !hardSync ? steeredVideoRate(el, offset, baseRate) : baseRate;+ const rate = Math.max(+ MIN_NATIVE_PLAYBACK_RATE,+ isPlayingVideo && !hardSync ? steeredVideoRate(el, offset, baseRate) : baseRate,+ );// Some engines read a rate back at lower precision; an equal-enough rate is not rewritten.if (Math.abs(el.playbackRate - rate) > 1e-6) el.playbackRate = rate;} catch (err) {@@ -584,12 +593,12 @@ export function syncRuntimeMedia(params: {holdSeekBarrierUntilVideoLands(el);}playRequested.delete(el);- } else if (!params.playing) {+ } else if (!playing) {holdSeekBarrierUntilVideoLands(el);}if (isHeldVideoTail) {if (!el.paused) el.pause();- } else if (params.playing && el.paused && !playRequested.has(el) && !isUnplayable(el)) {+ } else if (playing && el.paused && !playRequested.has(el) && !isUnplayable(el)) {// `HTMLMediaElement.play()` is spec'd to queue playback and resolve// once enough data is buffered, so we can unconditionally call it —// no need to gate on `readyState` or defer to a `canplay` listener.@@ -617,7 +626,7 @@ export function syncRuntimeMedia(params: {: "";if (name === "NotAllowedError") params.onAutoplayBlocked?.();});- } else if (!params.playing && !el.paused) {+ } else if (!playing && !el.paused) {el.pause();}continue;