mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
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.
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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<typeof import("../utils/urlDownloader.js")>()),
|
||||
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,
|
||||
`<!doctype html><html data-composition-variables='[{"id":"clip","type":"string","default":"tone.mp4"}]'><body><main data-composition-id="root" data-start="0" data-width="16" data-height="16"><video id="v" src="tone.mp4" data-var-src="clip" data-start="0" data-track-index="0"></video></main></body></html>`,
|
||||
);
|
||||
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"]);
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user