Skip to content

Commit 2ddf41e

Browse files
perf(producer): download a remote video once per render (#6637)
1 parent 0bdc128 commit 2ddf41e

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
2+
import { tmpdir } from "node:os";
3+
import { join } from "node:path";
4+
import { afterEach, expect, it, vi } from "vitest";
5+
import { compileForRender } from "./htmlCompiler.js";
6+
7+
const downloads = vi.hoisted(() => [] as { url: string; destDir: string }[]);
8+
vi.mock("../utils/urlDownloader.js", async (importOriginal) => ({
9+
...(await importOriginal<typeof import("../utils/urlDownloader.js")>()),
10+
downloadToTemp: async (url: string, destDir: string) => {
11+
downloads.push({ url, destDir });
12+
throw new Error("offline test");
13+
},
14+
fetchPublicHttpsText: async () => {
15+
throw new Error("offline test");
16+
},
17+
}));
18+
19+
const dirs: string[] = [];
20+
afterEach(() => {
21+
downloads.length = 0;
22+
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
23+
});
24+
25+
// The download cache is keyed by url and folder: one folder is what makes the second request a cache hit.
26+
it("asks for a remote video in one folder for both its duration probe and its localisation", async () => {
27+
const dir = mkdtempSync(join(tmpdir(), "hf-remote-once-"));
28+
dirs.push(dir);
29+
const url = "https://media.example.com/clip.mp4";
30+
const htmlPath = join(dir, "index.html");
31+
writeFileSync(
32+
htmlPath,
33+
`<!doctype html><html><body><main data-composition-id="root" data-start="0" data-width="16" data-height="16"><video id="v" muted src="${url}" data-start="0" data-track-index="0"></video></main></body></html>`,
34+
);
35+
await compileForRender(dir, htmlPath, join(dir, "out"), { allowSystemFontCapture: false });
36+
const forClip = downloads.filter((call) => call.url === url);
37+
expect(forClip.length).toBeGreaterThanOrEqual(2);
38+
expect(new Set(forClip.map((call) => call.destDir)).size).toBe(1);
39+
});

‎packages/producer/src/services/htmlCompiler.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,10 @@ async function resolveMediaDuration(
504504
let filePath = src;
505505

506506
if (isHttpUrl(src)) {
507-
if (!existsSync(downloadDir)) mkdirSync(downloadDir, { recursive: true });
507+
const remoteDir = join(downloadDir, REMOTE_MEDIA_SUBDIR);
508+
if (!existsSync(remoteDir)) mkdirSync(remoteDir, { recursive: true });
508509
try {
509-
filePath = await downloadToTemp(src, downloadDir, undefined, undefined, undefined, {
510+
filePath = await downloadToTemp(src, remoteDir, undefined, undefined, undefined, {
510511
onTelemetry: logRemoteDownloadTelemetry,
511512
});
512513
} catch {

0 commit comments

Comments
 (0)