Skip to content

Commit 059a6e5

Browse files
fix(producer): never probe a hidden video clip (#6340)
1 parent 2c10eb0 commit 059a6e5

4 files changed

Lines changed: 95 additions & 2 deletions

File tree

‎packages/engine/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ export {
253253
isVideoFrameFormat,
254254
EXTRACT_CACHE_MIN_AGE_MS,
255255
} from "./services/videoFrameExtractor.js";
256+
export { isSelfOrAncestorHidden } from "./services/mediaHidden.js";
256257
export { directorySizeBytes, gcExtractionCache } from "./services/extractionCache.js";
257258

258259
export {
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { afterEach, describe, expect, it, vi } from "vitest";
2+
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+
import { tmpdir } from "node:os";
4+
import { join } from "node:path";
5+
6+
const probed = vi.hoisted(() => ({ paths: [] as string[] }));
7+
8+
vi.mock("@hyperframes/engine", async (importOriginal) => {
9+
const actual = await importOriginal<typeof import("@hyperframes/engine")>();
10+
return {
11+
...actual,
12+
extractMediaMetadata: async (path: string) => {
13+
probed.paths.push(path);
14+
return { durationSeconds: 1, isVFR: false };
15+
},
16+
probeMediaProfile: async (path: string) => {
17+
probed.paths.push(path);
18+
return { hasVideoStream: true, hasAudioStream: false, visualKind: "moving" };
19+
},
20+
analyzeKeyframeIntervals: async (path: string) => {
21+
probed.paths.push(path);
22+
return { isProblematic: false, maxIntervalSeconds: 0 };
23+
},
24+
};
25+
});
26+
27+
vi.mock("../utils/ffprobe.js", async (importOriginal) => {
28+
const actual = await importOriginal<typeof import("../utils/ffprobe.js")>();
29+
return {
30+
...actual,
31+
extractMediaMetadata: async (path: string) => {
32+
probed.paths.push(path);
33+
return { durationSeconds: 1, isVFR: false };
34+
},
35+
};
36+
});
37+
38+
import { compileForRender } from "./htmlCompiler.js";
39+
40+
describe("hidden video clips", () => {
41+
let projectDir: string | undefined;
42+
43+
afterEach(() => {
44+
if (projectDir) rmSync(projectDir, { recursive: true, force: true });
45+
projectDir = undefined;
46+
probed.paths.length = 0;
47+
});
48+
49+
it("are not probed or collected, while a visible clip still is", async () => {
50+
projectDir = mkdtempSync(join(tmpdir(), "hf-hidden-probe-"));
51+
for (const src of ["own.mp4", "inherited.mp4", "shown.mp4"]) {
52+
writeFileSync(join(projectDir, src), "fixture");
53+
}
54+
const htmlPath = join(projectDir, "index.html");
55+
writeFileSync(
56+
htmlPath,
57+
`<!doctype html><html><body>
58+
<div data-composition-id="root" data-width="320" data-height="180" data-duration="1">
59+
<video id="own" src="own.mp4" data-hidden data-start="0" data-duration="1"></video>
60+
<div data-hidden><video id="inherited" src="inherited.mp4" data-start="0" data-duration="1"></video></div>
61+
<video id="shown" src="shown.mp4" data-start="0" data-duration="1"></video>
62+
</div>
63+
</body></html>`,
64+
);
65+
66+
const compiled = await compileForRender(projectDir, htmlPath, join(projectDir, "downloads"));
67+
await new Promise<void>((resolve) => setTimeout(resolve, 0));
68+
69+
expect(compiled.videos.map((v) => v.id)).toEqual(["shown"]);
70+
expect(probed.paths.length).toBeGreaterThan(0);
71+
expect(probed.paths.every((path) => path.endsWith("shown.mp4"))).toBe(true);
72+
});
73+
});

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ import {
7272
} from "@hyperframes/parsers/asset-resolution";
7373
import { extractMediaMetadata, extractAudioMetadata } from "../utils/ffprobe.js";
7474
import { isPathInside, toExternalAssetKey } from "../utils/paths.js";
75-
import { collectRenderMedia } from "./renderMediaCollector.js";
75+
import { collectHiddenVideoIds, collectRenderMedia } from "./renderMediaCollector.js";
7676
import {
7777
type VideoElement,
7878
type ImageElement,
@@ -664,9 +664,10 @@ async function compileHtmlFile(
664664
// Phase 2: Bound authored audio to playable source (parallel ffprobe).
665665
// Explicit video slots may outlive their source and hold the final frame.
666666
const preResolved = extractResolvedMedia(compiledHtml);
667+
const hiddenVideoIds = collectHiddenVideoIds(compiledHtml);
667668
const clampResults = await Promise.all(
668669
preResolved
669-
.filter((el) => !!el.src && !el.loop)
670+
.filter((el) => !!el.src && !el.loop && !hiddenVideoIds.has(el.id))
670671
.map(async (el) => {
671672
const { duration: maxDuration } = await resolveMediaDuration(
672673
el.src!,

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
parseVideoElements,
2929
parseImageElements,
3030
parseAudioElements,
31+
isSelfOrAncestorHidden,
3132
resolveReferencedStart,
3233
type RefResolverEl,
3334
type RefResolverDoc,
@@ -147,6 +148,17 @@ function collectHostWindows(html: string): Map<string, HostWindow> {
147148
return windows;
148149
}
149150

151+
/** Ids, as the parsers report them, of videos that are `data-hidden` or sit inside such an element. */
152+
export function collectHiddenVideoIds(html: string): Set<string> {
153+
const { document } = parseHTML(html);
154+
const hidden = new Set<string>();
155+
for (const element of document.querySelectorAll("video")) {
156+
const id = element.getAttribute(MEDIA_RENDER_ID_ATTR) || element.getAttribute("id");
157+
if (id && isSelfOrAncestorHidden(element)) hidden.add(id);
158+
}
159+
return hidden;
160+
}
161+
150162
/**
151163
* Shift a scene-relative window onto the root timeline; null when the clip starts after its host
152164
* ends (the file-tree walk's `start < absoluteEnd` drop) or its in-point hosts cut all of it.
@@ -214,8 +226,14 @@ export function collectRenderMedia(html: string): RenderMedia {
214226
const windowFor = (id: string): HostWindow => windows.get(id) ?? ROOT_WINDOW;
215227
const droppedMediaIds = new Set<string>();
216228

229+
// A hidden clip never shows, so it is not extracted or probed; its audio is the mixer's to mute.
230+
const hiddenIds = collectHiddenVideoIds(html);
217231
const videos: VideoElement[] = [];
218232
for (const video of parseVideoElements(html)) {
233+
if (hiddenIds.has(video.id)) {
234+
droppedMediaIds.add(video.id);
235+
continue;
236+
}
219237
const absolute = toAbsoluteWindow(video.start, video.end, windowFor(video.id));
220238
if (!absolute) {
221239
droppedMediaIds.add(video.id);

0 commit comments

Comments
 (0)