From 2760fa11916d37a512044f6243fcbbaba43fad3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Fri, 13 Nov 2026 02:34:27 +0000 Subject: [PATCH] fix(producer): a video variable given as a link plays its sound once (#7633) * fix(producer): a video bound to a link plays its sound once The compile step downloads a link a variable binds to a video, then the browser set the link back through data-var-src, giving the clip a second name and its sound a second lane. The downloaded element now drops its binding, so it has one name. * test(producer): run the bound-link audio test in the integration lane It makes its clip with ffmpeg, which the unit lane does not install. --- .../producer/scripts/integration-tests.json | 1 + .../htmlCompiler.remoteBoundMedia.test.ts | 58 +++++++++++++++++++ .../producer/src/services/htmlCompiler.ts | 20 ++++++- 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 packages/producer/src/services/htmlCompiler.remoteBoundMedia.test.ts diff --git a/packages/producer/scripts/integration-tests.json b/packages/producer/scripts/integration-tests.json index cd84122a87..72b29e9c5f 100644 --- a/packages/producer/scripts/integration-tests.json +++ b/packages/producer/scripts/integration-tests.json @@ -36,6 +36,7 @@ "src/services/htmlCompiler.mediaType.test.ts", "src/services/htmlCompiler.naturalDuration.test.ts", "src/services/htmlCompiler.probeCount.test.ts", + "src/services/htmlCompiler.remoteBoundMedia.test.ts", "src/services/htmlCompiler.variableMedia.test.ts", "src/services/render/audioDeliveryLimit.integration.test.ts", "src/services/render/audioPadTrim.integration.test.ts", diff --git a/packages/producer/src/services/htmlCompiler.remoteBoundMedia.test.ts b/packages/producer/src/services/htmlCompiler.remoteBoundMedia.test.ts new file mode 100644 index 0000000000..1466920832 --- /dev/null +++ b/packages/producer/src/services/htmlCompiler.remoteBoundMedia.test.ts @@ -0,0 +1,58 @@ +import { spawnSync } from "node:child_process"; +import { copyFileSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, expect, it, vi } from "vitest"; +import { compileForRender } from "./htmlCompiler.js"; + +const clip = vi.hoisted(() => ({ path: "" })); +vi.mock("../utils/urlDownloader.js", async (importOriginal) => ({ + ...(await importOriginal()), + downloadToTemp: async (_url: string, destDir: string) => { + const dest = join(destDir, "download_clip.mp4"); + copyFileSync(clip.path, dest); + return dest; + }, + fetchPublicHttpsText: async () => { + throw new Error("offline test"); + }, +})); + +const dirs: string[] = []; +afterEach(() => { + for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); +}); + +it("gives a video bound to a link one name, the downloaded copy, so its sound is mixed once", async () => { + const dir = mkdtempSync(join(tmpdir(), "hf-remote-bound-")); + dirs.push(dir); + clip.path = join(dir, "tone.mp4"); + const ffmpeg = process.env.HYPERFRAMES_FFMPEG_PATH || "ffmpeg"; + const args = [ + "-hide_banner", + "-loglevel", + "error", + "-f", + "lavfi", + "-i", + "color=c=red:s=16x16:r=5:d=5", + ]; + args.push("-f", "lavfi", "-i", "sine=d=5", "-shortest", "-c:v", "libx264", "-pix_fmt", "yuv420p"); + args.push("-c:a", "aac", "-y", clip.path); + expect(spawnSync(ffmpeg, args).status).toBe(0); + + const url = "https://media.example.com/clip.mp4"; + const htmlPath = join(dir, "index.html"); + writeFileSync( + htmlPath, + `
`, + ); + const compiled = await compileForRender(dir, htmlPath, join(dir, "out"), { + allowSystemFontCapture: false, + variables: { clip: url }, + }); + + expect(compiled.html).not.toContain(`src="${url}"`); + expect(compiled.html).not.toContain("data-var-src"); + expect(compiled.audios.map((a) => a.src)).toEqual(["_remote_media/download_clip.mp4"]); +}); diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index 8d611d0e81..dc431b7272 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -1577,13 +1577,31 @@ export async function localizeRemoteMediaSources( if (m[1]) urlSet.add(m[1]); } } - return downloadAndRewriteUrls( + const localized = await downloadAndRewriteUrls( urlSet, html, join(downloadDir, REMOTE_MEDIA_SUBDIR), "Remote media download failed", "Localized remote media source(s)", ); + return { ...localized, html: dropBindingOnLocalizedMedia(localized.html) }; +} + +/** + * A bound element now holds the downloaded copy. Its `data-var-src` would make the browser set the + * link back, giving the clip a second name and its sound a second lane. + */ +function dropBindingOnLocalizedMedia(html: string): string { + if (!html.includes("data-var-src")) return html; + const { document } = parseHTML(html); + let changed = false; + for (const el of document.querySelectorAll("video[data-var-src], audio[data-var-src]")) { + if (el.closest("[data-variable-values]")) continue; + if (!el.getAttribute("src")?.startsWith(`${REMOTE_MEDIA_SUBDIR}/`)) continue; + el.removeAttribute("data-var-src"); + changed = true; + } + return changed ? document.toString() : html; } /**