Skip to content

Commit 9d79182

Browse files
committed
fix(producer): compile probes each unique media source once however many elements use it
1 parent 2d695a8 commit 9d79182

3 files changed

Lines changed: 191 additions & 55 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
2-
import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
2+
import { copyFileSync, existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
55
import { compileForRender } from "./htmlCompiler.js";
@@ -167,11 +167,12 @@ describe("compileForRender media-type ownership", () => {
167167
const subCompositions: string[] = [];
168168
for (let index = 0; index < 8; index += 1) {
169169
const name = `probe-${index}.html`;
170+
copyFileSync(join(projectDir, "extensionless-video"), join(projectDir, `video-${index}`));
170171
writeFileSync(
171172
join(projectDir, name),
172173
`<!doctype html><html><body>
173174
<div data-composition-id="sub-${index}" data-width="32" data-height="32" data-duration="1">
174-
<video id="video-${index}" src="extensionless-video" data-start="0"></video>
175+
<video id="video-${index}" src="video-${index}" data-start="0"></video>
175176
</div>
176177
</body></html>`,
177178
);
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
2+
import { spawnSync } from "node:child_process";
3+
import {
4+
chmodSync,
5+
createReadStream,
6+
mkdirSync,
7+
mkdtempSync,
8+
readFileSync,
9+
rmSync,
10+
statSync,
11+
writeFileSync,
12+
} from "node:fs";
13+
import { createServer, type Server } from "node:http";
14+
import { tmpdir } from "node:os";
15+
import { join } from "node:path";
16+
import { compileForRender } from "./htmlCompiler.js";
17+
18+
const SOURCES = 3;
19+
const SCENES = 40;
20+
21+
describe("compileForRender media probes", () => {
22+
const projectDir = mkdtempSync(join(tmpdir(), "hf-compiler-probe-count-"));
23+
const mediaDir = mkdtempSync(join(tmpdir(), "hf-compiler-probe-media-"));
24+
const logPath = join(projectDir, "ffprobe.log");
25+
let server: Server;
26+
let origin = "";
27+
const savedProbePath = process.env.HYPERFRAMES_FFPROBE_PATH;
28+
29+
beforeAll(async () => {
30+
const ffmpeg = process.env.HYPERFRAMES_FFMPEG_PATH || "ffmpeg";
31+
mkdirSync(join(projectDir, "compositions"));
32+
for (let i = 0; i < SOURCES; i++) {
33+
const made = spawnSync(
34+
ffmpeg,
35+
[
36+
"-hide_banner",
37+
"-loglevel",
38+
"error",
39+
"-f",
40+
"lavfi",
41+
"-i",
42+
`color=c=red:s=16x16:r=1:d=${10 + i}`,
43+
].concat(["-c:v", "libx264", "-pix_fmt", "yuv420p", "-y", join(mediaDir, `s${i}.mp4`)]),
44+
{ encoding: "utf8" },
45+
);
46+
if (made.status !== 0) throw new Error(made.stderr || "failed to create fixture");
47+
}
48+
for (let n = 0; n < SCENES; n++) {
49+
writeFileSync(
50+
join(projectDir, "compositions", `scene${n}.html`),
51+
`<template id="scene${n}-template"><div data-composition-id="scene${n}" data-width="16" data-height="16"><video id="v${n}" muted src="../s${n % SOURCES}.mp4" data-start="0" data-track-index="0"></video></div></template>`,
52+
);
53+
}
54+
server = createServer((req, res) => {
55+
const file = join(mediaDir, (req.url ?? "").split("?")[0]!.slice(1));
56+
const size = statSync(file).size;
57+
const range = /bytes=(\d+)-(\d*)/.exec(req.headers.range ?? "");
58+
const start = range ? Number(range[1]) : 0;
59+
const end = range?.[2] ? Number(range[2]) : size - 1;
60+
res.writeHead(range ? 206 : 200, {
61+
"Content-Type": "video/mp4",
62+
"Accept-Ranges": "bytes",
63+
"Content-Length": end - start + 1,
64+
...(range ? { "Content-Range": `bytes ${start}-${end}/${size}` } : {}),
65+
});
66+
createReadStream(file, { start, end }).pipe(res);
67+
});
68+
await new Promise<void>((done) => server.listen(0, "127.0.0.1", done));
69+
origin = `http://127.0.0.1:${(server.address() as { port: number }).port}`;
70+
const real = spawnSync("sh", ["-c", "command -v ffprobe"], { encoding: "utf8" }).stdout.trim();
71+
const shim = join(projectDir, "ffprobe-shim.sh");
72+
writeFileSync(
73+
shim,
74+
`#!/bin/sh\nstart=$(date +%s%N)\n${real} "$@"\ncode=$?\necho "$start $(date +%s%N) $(eval echo \\\${$#})" >> ${logPath}\nexit $code\n`,
75+
);
76+
chmodSync(shim, 0o755);
77+
process.env.HYPERFRAMES_FFPROBE_PATH = shim;
78+
});
79+
80+
afterAll(() => {
81+
if (savedProbePath === undefined) delete process.env.HYPERFRAMES_FFPROBE_PATH;
82+
else process.env.HYPERFRAMES_FFPROBE_PATH = savedProbePath;
83+
server.close();
84+
rmSync(projectDir, { recursive: true, force: true });
85+
rmSync(mediaDir, { recursive: true, force: true });
86+
});
87+
88+
it("probes each unique remote source once however many elements use it", async () => {
89+
const scenes = Array.from(
90+
{ length: SCENES },
91+
(_, n) =>
92+
`<div data-composition-id="scene${n}" data-composition-src="compositions/scene${n}.html" data-start="0" data-duration="5" data-width="16" data-height="16"></div>`,
93+
).join("");
94+
const htmlPath = join(projectDir, "index.html");
95+
writeFileSync(
96+
htmlPath,
97+
`<!doctype html><html><body><main data-composition-id="root" data-start="0" data-duration="5" data-width="16" data-height="16">${scenes}</main></body></html>`,
98+
);
99+
await compileForRender(projectDir, htmlPath, projectDir, {
100+
allowSystemFontCapture: false,
101+
urlByAbsentLocalPath: new Map(
102+
Array.from({ length: SOURCES }, (_, i) => [
103+
join(projectDir, `s${i}.mp4`),
104+
`${origin}/s${i}.mp4`,
105+
]),
106+
),
107+
});
108+
const perFile = new Map<string, number>();
109+
for (const row of readFileSync(logPath, "utf8").trim().split("\n")) {
110+
const file = row.split(" ").slice(2).join(" ");
111+
perFile.set(file, (perFile.get(file) ?? 0) + 1);
112+
}
113+
for (let i = 0; i < SOURCES; i++) {
114+
// one stream-and-format read and one keyframe scan
115+
expect(perFile.get(`${origin}/s${i}.mp4`)).toBeLessThanOrEqual(2);
116+
}
117+
});
118+
});

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

Lines changed: 70 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,11 @@ export function detectShaderTransitionUsage(html: string): boolean {
481481
return false;
482482
}
483483

484+
type SourceProbe = { profile: MediaProbeProfile; fileDuration: number | null } | { error: unknown };
485+
486+
/** One probe per unique source for a whole compile, whatever number of elements point at it. */
487+
type SourceProbes = Map<string, Promise<SourceProbe>>;
488+
484489
async function resolveMediaDuration(
485490
src: string,
486491
mediaStart: number,
@@ -491,6 +496,7 @@ async function resolveMediaDuration(
491496
elementIdentity: string,
492497
log?: ProducerLogger,
493498
urlByAbsentLocalPath?: ReadonlyMap<string, string>,
499+
probes?: SourceProbes,
494500
): Promise<{ duration: number | null; resolvedPath: string }> {
495501
let filePath = src;
496502

@@ -541,64 +547,62 @@ async function resolveMediaDuration(
541547
return wrapped;
542548
};
543549

544-
return withMediaProbeSlot(async () => {
545-
let profile: MediaProbeProfile;
546-
try {
547-
// Payload sniff (STUDIO-5433): if an authoring bug hands us a text
548-
// payload (e.g. an unresolved nested-composition preview URL), fail with
549-
// a typed NotMediaPayloadError instead of letting ffprobe emit an opaque
550-
// `[mov,mp4,...] moov atom not found` that routes as a codec bug.
551-
// Deliberately inside this try: the audio/video split below is the
552-
// contract, so a bad audio src must still degrade to duration 0 rather
553-
// than take down the whole render.
554-
await assertMediaPayload(filePath, elementIdentity);
555-
profile = await probeMediaProfile(filePath);
556-
} catch (error) {
557-
// Preserve the historical split: invalid video sources surface their
558-
// probe failure, while invalid/unreadable audio sources resolve to zero
559-
// duration and are excluded by the compiler.
560-
if (tagName !== "video") {
561-
if (error instanceof NotMediaPayloadError) {
562-
// Dropping it silently is what let STUDIO-5433 resurface downstream
563-
// as `prepare/ffmpeg_failed` with owner "system".
564-
log?.warn(
565-
`[compile] Audio "${elementIdentity}" (${src}) is a text document, not a media ` +
566-
"file, so the element is dropped from the render. Point it at a rendered media file.",
567-
);
568-
}
569-
return { duration: null, resolvedPath: filePath };
570-
}
571-
throw withSrcContext(error);
572-
}
573-
assertAssetMediaTypeProfile(tagName === "video" ? "video" : "audio", profile, elementIdentity);
574-
575-
let metadata: { durationSeconds: number };
576-
if (tagName === "video") {
550+
const probe = () =>
551+
withMediaProbeSlot(async (): Promise<SourceProbe> => {
577552
try {
578-
metadata = await extractMediaMetadata(filePath);
553+
// Payload sniff (STUDIO-5433): if an authoring bug hands us a text
554+
// payload (e.g. an unresolved nested-composition preview URL), fail with
555+
// a typed NotMediaPayloadError instead of letting ffprobe emit an opaque
556+
// `[mov,mp4,...] moov atom not found` that routes as a codec bug.
557+
await assertMediaPayload(filePath, elementIdentity);
558+
const profile = await probeMediaProfile(filePath);
559+
if (tagName === "video") {
560+
return { profile, fileDuration: (await extractMediaMetadata(filePath)).durationSeconds };
561+
}
562+
try {
563+
return { profile, fileDuration: (await extractAudioMetadata(filePath)).durationSeconds };
564+
} catch {
565+
// Source file has no audio stream (e.g. a silent video used as an audio src).
566+
// Duration 0 excludes the element from the composition, as missing files do.
567+
return { profile, fileDuration: null };
568+
}
579569
} catch (error) {
580-
throw withSrcContext(error);
570+
return { error };
581571
}
582-
} else {
583-
try {
584-
metadata = await extractAudioMetadata(filePath);
585-
} catch {
586-
// Source file has no audio stream (e.g. a silent video used as an audio src).
587-
// Return duration 0 so the element is excluded from the composition gracefully,
588-
// matching how missing files and failed downloads are already handled above.
589-
return { duration: null, resolvedPath: filePath };
572+
});
573+
const key = `${tagName}\0${filePath}`;
574+
let probed = probes?.get(key);
575+
if (!probed) {
576+
probed = probe();
577+
probes?.set(key, probed);
578+
}
579+
const outcome = await probed;
580+
if ("error" in outcome) {
581+
// Invalid video sources surface their probe failure; invalid or unreadable
582+
// audio sources resolve to zero duration and are excluded by the compiler.
583+
if (tagName !== "video") {
584+
if (outcome.error instanceof NotMediaPayloadError) {
585+
log?.warn(
586+
`[compile] Audio "${elementIdentity}" (${src}) is a text document, not a media ` +
587+
"file, so the element is dropped from the render. Point it at a rendered media file.",
588+
);
590589
}
590+
return { duration: null, resolvedPath: filePath };
591591
}
592-
593-
const fileDuration = metadata.durationSeconds;
594-
const duration = resolveNaturalMediaTimelineDurationFromValues(
595-
fileDuration,
596-
mediaStart,
597-
playbackRate,
598-
);
599-
600-
return { duration, resolvedPath: filePath };
601-
});
592+
throw withSrcContext(outcome.error);
593+
}
594+
assertAssetMediaTypeProfile(
595+
tagName === "video" ? "video" : "audio",
596+
outcome.profile,
597+
elementIdentity,
598+
);
599+
if (outcome.fileDuration === null) return { duration: null, resolvedPath: filePath };
600+
const duration = resolveNaturalMediaTimelineDurationFromValues(
601+
outcome.fileDuration,
602+
mediaStart,
603+
playbackRate,
604+
);
605+
return { duration, resolvedPath: filePath };
602606
}
603607

604608
/**
@@ -628,6 +632,7 @@ async function compileHtmlFile(
628632
downloadDir: string,
629633
log?: ProducerLogger,
630634
urlByAbsentLocalPath?: ReadonlyMap<string, string>,
635+
probes?: SourceProbes,
631636
): Promise<{ html: string; unresolvedCompositions: UnresolvedElement[] }> {
632637
const { html: staticCompiled, unresolved } = compileTimingAttrs(html);
633638

@@ -650,6 +655,7 @@ async function compileHtmlFile(
650655
el.id,
651656
log,
652657
urlByAbsentLocalPath,
658+
probes,
653659
).then(({ duration }) => ({ id: el.id, duration })),
654660
),
655661
);
@@ -678,6 +684,8 @@ async function compileHtmlFile(
678684
el.tagName,
679685
el.id,
680686
log,
687+
undefined,
688+
probes,
681689
);
682690
return { id: el.id, tagName: el.tagName, duration: el.duration, maxDuration, src: el.src! };
683691
}),
@@ -746,6 +754,7 @@ async function parseSubCompositions(
746754
downloadDir: string,
747755
visited: Set<string> = new Set(),
748756
urlByAbsentLocalPath?: ReadonlyMap<string, string>,
757+
probes?: SourceProbes,
749758
): Promise<{ subCompositions: Map<string, string> }> {
750759
const subCompositions = new Map<string, string>();
751760

@@ -792,6 +801,7 @@ async function parseSubCompositions(
792801
downloadDir,
793802
undefined,
794803
urlByAbsentLocalPath,
804+
probes,
795805
);
796806

797807
const nested = await parseSubCompositions(
@@ -800,6 +810,7 @@ async function parseSubCompositions(
800810
downloadDir,
801811
item.nestedVisited,
802812
urlByAbsentLocalPath,
813+
probes,
803814
);
804815

805816
return {
@@ -2040,12 +2051,14 @@ export async function compileForRender(
20402051
// start.
20412052
assertSubCompositionsUsable(rawHtml, projectDir);
20422053

2054+
const probes: SourceProbes = new Map();
20432055
const { html: compiledHtml, unresolvedCompositions } = await compileHtmlFile(
20442056
rawHtml,
20452057
projectDir,
20462058
downloadDir,
20472059
options.log,
20482060
options.urlByAbsentLocalPath,
2061+
probes,
20492062
);
20502063

20512064
// Compile each referenced sub-composition so the inliner can hoist it.
@@ -2055,6 +2068,7 @@ export async function compileForRender(
20552068
downloadDir,
20562069
undefined,
20572070
options.urlByAbsentLocalPath,
2071+
probes,
20582072
);
20592073

20602074
// Ensure the HTML is a full document before inlining sub-compositions.
@@ -2208,9 +2222,12 @@ export async function compileForRender(
22082222
// ffprobe subprocesses and should not block compilation since they only produce warnings.
22092223
// The two probes run in sequence rather than in parallel: the keyframe analysis needs
22102224
// the video stream's own duration to classify a single-keyframe (single-GOP) file.
2225+
const advised = new Set<string>();
22112226
for (const video of videos) {
22122227
if (isHttpUrl(video.src)) continue;
22132228
const videoPath = resolve(projectDir, video.src);
2229+
if (advised.has(videoPath)) continue;
2230+
advised.add(videoPath);
22142231
const reencode = `ffmpeg -i "${video.src}" -c:v libx264 -r 30 -g 30 -keyint_min 30 -movflags +faststart -c:a copy output.mp4`;
22152232
(async () => {
22162233
const metadata = await withMediaProbeSlot(() => extractMediaMetadata(videoPath));

0 commit comments

Comments
 (0)