mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(cli): check copies only the stretch of a video it plays and stops after 2 minutes of copies (#7182)
* fix(cli): check stops after 2 minutes of video copies and says how to finish * fix(cli): check copies only the stretch of a video the composition plays * docs(cli): check copies only the played stretch of a long video * docs(cli): shorten two comments on the copy window * fix(cli): finding crops and frames get the same copy windows as the check * fix(cli): drop a comment the ratchet counts * refactor(studio-server): keep runFfmpeg under the complexity gate
This commit is contained in:
@@ -1258,10 +1258,11 @@ cover every finding from the full check.
|
||||
| `--prepare-media` | Only make the browser-playable video copies, then exit: no browser, no report. `--json` lists each file as `ready`, `cached` or `failed`; exit 1 if any failed |
|
||||
|
||||
A video Chrome cannot decode (HEVC, ProRes) is copied once, and the first check waits
|
||||
for it. A source longer than 22 seconds is copied in 15-second pieces that are kept, so a
|
||||
for it for up to 2 minutes; past that, check stops with a `media_copies_unfinished` finding that names
|
||||
each unfinished file and its progress. Run `--prepare-media` (no time limit) to finish them. When every use of a long video in the composition has a known `data-duration` (on the video or the element that times it, no `loop`), check copies only that stretch from the start of the file, rounded up to whole 15-second pieces, instead of the whole file. A source longer than 22 seconds is copied in 15-second pieces that are kept, so a
|
||||
copy that is stopped continues from its finished pieces on the next run. While it works, the
|
||||
check prints each copy's progress to stderr every 10 seconds (stdout stays clean for `--json`),
|
||||
and ends a copy only when it makes no progress for 3 minutes (`HYPERFRAMES_PROXY_STALL_MS`).
|
||||
and ends a copy that makes no progress for 3 minutes (`HYPERFRAMES_PROXY_STALL_MS`).
|
||||
For a long project, run `hyperframes check --prepare-media` first, then the check itself finds
|
||||
every copy ready.
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ import {
|
||||
} from "./checkBrowser.js";
|
||||
import { CheckDeadline } from "./checkDeadline.js";
|
||||
import { closeManagedBrowser } from "../browser/launch.js";
|
||||
import { serveStaticProjectHtml } from "./staticProjectServer.js";
|
||||
import type { ProjectDir } from "./project.js";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
@@ -53,6 +54,10 @@ const mocks = vi.hoisted(() => ({
|
||||
) => Promise<string>
|
||||
>(),
|
||||
FfmpegUnavailableError: class FfmpegUnavailableError extends Error {},
|
||||
proxyLimits: [] as Array<number | undefined>,
|
||||
proxyWindowSeconds: vi.fn(
|
||||
async (_sourcePath: string, _neededSeconds: number): Promise<number | undefined> => undefined,
|
||||
),
|
||||
getProxyCachePath: vi.fn(
|
||||
(projectDir: string, sourcePath: string, _variant?: string, _box?: unknown) =>
|
||||
`${sourcePath}.proxy.mp4`,
|
||||
@@ -126,12 +131,14 @@ vi.mock("@hyperframes/studio-server/media-codec-map", async (importOriginal) =>
|
||||
vi.mock("@hyperframes/studio-server/proxy-transcoder", () => ({
|
||||
FfmpegUnavailableError: mocks.FfmpegUnavailableError,
|
||||
getProxyCachePath: mocks.getProxyCachePath,
|
||||
proxyWindowSeconds: mocks.proxyWindowSeconds,
|
||||
resolveProxies: (
|
||||
projectDir: string,
|
||||
sources: Array<{
|
||||
sourcePath: string;
|
||||
variant: string;
|
||||
box?: { width: number; height: number };
|
||||
limitSeconds?: number;
|
||||
}>,
|
||||
onSettled: (index: number) => void = () => {},
|
||||
onProgress: (
|
||||
@@ -141,6 +148,7 @@ vi.mock("@hyperframes/studio-server/proxy-transcoder", () => ({
|
||||
signal?: AbortSignal,
|
||||
) => {
|
||||
mocks.proxySignal = signal;
|
||||
mocks.proxyLimits = sources.map(({ limitSeconds }) => limitSeconds);
|
||||
mocks.proxyProgress.feed?.(onProgress);
|
||||
return Promise.allSettled(
|
||||
sources.map(({ sourcePath, variant, box }, index) =>
|
||||
@@ -1441,6 +1449,7 @@ describe("preResolveHostileMediaProxies", () => {
|
||||
join(projectDir, "clip.mp4"),
|
||||
"h264",
|
||||
box,
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1480,6 +1489,7 @@ describe("preResolveHostileMediaProxies", () => {
|
||||
],
|
||||
failedPaths: new Set(["/clip.mp4"]),
|
||||
files: [{ file: "clip.mp4", state: "failed", reason: "ffmpeg exited with code 1" }],
|
||||
limits: new Map(),
|
||||
});
|
||||
expect(errorSpy).toHaveBeenCalledWith(
|
||||
expect.stringContaining("media proxy pre-resolve: 0/1 ready, 1 failed"),
|
||||
@@ -1572,6 +1582,61 @@ describe("preResolveHostileMediaProxies", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("copies only the stretch of a long video the composition plays", async () => {
|
||||
const projectDir = mkProjectDir();
|
||||
mocks.scanProjectMediaCodecMap.mockResolvedValue({
|
||||
"/clip.mov": {
|
||||
codecName: "hevc",
|
||||
browserHostile: true,
|
||||
representativeMime: null,
|
||||
hasAlpha: false,
|
||||
},
|
||||
});
|
||||
mocks.proxyWindowSeconds.mockResolvedValue(30);
|
||||
mocks.resolveProxy.mockResolvedValue("/cache/clip.mov.mp4");
|
||||
const html =
|
||||
'<html><body><video src="clip.mov" data-start="0" data-duration="12" data-media-start="3"></video></body></html>';
|
||||
|
||||
const { limits } = await preResolveHostileMediaProxies(projectDir, html);
|
||||
|
||||
expect(mocks.proxyWindowSeconds).toHaveBeenCalledWith(join(projectDir, "clip.mov"), 15);
|
||||
expect(mocks.proxyLimits).toEqual([30]);
|
||||
expect(limits).toEqual(new Map([[join(projectDir, "clip.mov"), 30]]));
|
||||
});
|
||||
|
||||
it("stops a check whose copies outrun the copy budget, naming each file and the way to finish", async () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
const projectDir = mkProjectDir();
|
||||
mocks.scanProjectMediaCodecMap.mockResolvedValue({
|
||||
"/clip.mov": {
|
||||
codecName: "hevc",
|
||||
browserHostile: true,
|
||||
representativeMime: null,
|
||||
hasAlpha: false,
|
||||
},
|
||||
});
|
||||
mocks.resolveProxy.mockImplementation(
|
||||
() =>
|
||||
new Promise<string>((_, reject) =>
|
||||
mocks.proxySignal?.addEventListener("abort", () => reject(new Error("aborted"))),
|
||||
),
|
||||
);
|
||||
mocks.proxyProgress.feed = (report) => report(0, { doneSeconds: 30, totalSeconds: 120 });
|
||||
vi.spyOn(console, "error").mockImplementation(() => undefined);
|
||||
|
||||
const pending = preResolveHostileMediaProxies(projectDir, "<html></html>", undefined, 60_000);
|
||||
const outcome = expect(pending).rejects.toThrow(
|
||||
/clip\.mov 25%.*hyperframes check --prepare-media/s,
|
||||
);
|
||||
await vi.advanceTimersByTimeAsync(60_000);
|
||||
await outcome;
|
||||
} finally {
|
||||
mocks.proxyProgress.feed = undefined;
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it("stops reporting a copy once it is done, so a finished one is not read as stuck", async () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
@@ -1683,6 +1748,7 @@ describe("preResolveHostileMediaProxies", () => {
|
||||
findings: [],
|
||||
failedPaths: new Set(),
|
||||
files: [],
|
||||
limits: new Map(),
|
||||
});
|
||||
|
||||
expect(errorSpy).toHaveBeenCalledWith(
|
||||
@@ -2231,6 +2297,25 @@ describe("finding frame capture", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("serves the page the same copy windows the check made, so a capture starts no full copy", async () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "finding-frames-windows-"));
|
||||
installSessionMock(fakePage());
|
||||
mocks.bundleWithLocalizedFonts.mockResolvedValueOnce(
|
||||
'<html><body><video src="clip.mov" data-duration="5"></video></body></html>',
|
||||
);
|
||||
mocks.proxyWindowSeconds.mockResolvedValue(30);
|
||||
try {
|
||||
await captureFindingFrames({ ...PROJECT, dir }, DEFAULT_CHECK_OPTIONS, [
|
||||
{ time: 1, filename: "a.png" },
|
||||
]);
|
||||
expect(vi.mocked(serveStaticProjectHtml).mock.calls.at(-1)?.[5]).toEqual(
|
||||
new Map([[join(dir, "clip.mov"), 30]]),
|
||||
);
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("closes the session when a later screenshot fails", async () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "finding-frames-failure-"));
|
||||
const page = fakePage();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { DEFAULT_CONFIG } from "@hyperframes/engine";
|
||||
import type { CheckDeadline } from "./checkDeadline.js";
|
||||
import { CheckCopiesUnfinishedError, COPY_BUDGET_MS, type CheckDeadline } from "./checkDeadline.js";
|
||||
import { SEEK_SETTLES_BEFORE_PROTOCOL_TIMEOUT_MS } from "../capture/captureTimeout.js";
|
||||
import { logCheckProgress } from "./checkProgress.js";
|
||||
import { BrowserCleanupError, closeManagedBrowser, onStopSignal } from "../browser/launch.js";
|
||||
@@ -43,6 +43,7 @@ import {
|
||||
} from "./contrastMotion.js";
|
||||
import { resolveAutoProxy } from "./projectConfig.js";
|
||||
import { findStageProxyBox } from "./compositionViewport.js";
|
||||
import { mediaUsedSeconds } from "./mediaUsedSeconds.js";
|
||||
import type { PreviewProxyBox } from "@hyperframes/core";
|
||||
import { injectTagsAtHeadStart } from "@hyperframes/core/compiler/html-document";
|
||||
import {
|
||||
@@ -58,6 +59,7 @@ import {
|
||||
import {
|
||||
FfmpegUnavailableError,
|
||||
getProxyCachePath,
|
||||
proxyWindowSeconds,
|
||||
resolveProxies,
|
||||
type ProxyProgress,
|
||||
} from "@hyperframes/studio-server/proxy-transcoder";
|
||||
@@ -168,9 +170,11 @@ function startCopyHeartbeat(
|
||||
progress: (index: number, progress: ProxyProgress) => void;
|
||||
settled: (index: number) => void;
|
||||
stop: () => void;
|
||||
percent: (index: number) => string;
|
||||
} {
|
||||
const startedAt = Date.now();
|
||||
const latest = new Map<number, ProxyProgress>();
|
||||
const last = new Map<number, ProxyProgress>();
|
||||
const total = cached.filter((isCached) => !isCached).length;
|
||||
let finished = 0;
|
||||
const timer = setInterval(() => {
|
||||
@@ -186,7 +190,16 @@ function startCopyHeartbeat(
|
||||
}, COPY_HEARTBEAT_MS);
|
||||
timer.unref();
|
||||
return {
|
||||
progress: (index, progress) => void latest.set(index, progress),
|
||||
progress: (index, progress) => {
|
||||
latest.set(index, progress);
|
||||
last.set(index, progress);
|
||||
},
|
||||
percent: (index) => {
|
||||
const { doneSeconds = 0, totalSeconds } = last.get(index) ?? {};
|
||||
return totalSeconds
|
||||
? `${Math.min(100, Math.floor((doneSeconds / totalSeconds) * 100))}%`
|
||||
: "not started";
|
||||
},
|
||||
settled: (index) => {
|
||||
latest.delete(index);
|
||||
if (!cached[index]) finished += 1;
|
||||
@@ -195,6 +208,26 @@ function startCopyHeartbeat(
|
||||
};
|
||||
}
|
||||
|
||||
/** Absolute path -> seconds of it the composition plays, for the given files worth copying only in part. */
|
||||
async function copyWindows(
|
||||
projectDir: string,
|
||||
html: string,
|
||||
pathnames: Iterable<string>,
|
||||
): Promise<Map<string, number>> {
|
||||
const used = mediaUsedSeconds(html);
|
||||
const windows = new Map<string, number>();
|
||||
await Promise.all(
|
||||
[...pathnames].map(async (pathname) => {
|
||||
const seconds = used.get(pathname);
|
||||
if (seconds === undefined) return;
|
||||
const sourcePath = resolve(projectDir, pathname.replace(/^\/+/, ""));
|
||||
const window = await proxyWindowSeconds(sourcePath, seconds);
|
||||
if (window !== undefined) windows.set(sourcePath, window);
|
||||
}),
|
||||
);
|
||||
return windows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Awaits the H.264 authoring proxy for every browser-hostile local video
|
||||
* asset in `html` BEFORE the timed render-ready wait starts. `check`'s
|
||||
@@ -211,8 +244,14 @@ export async function preResolveHostileMediaProxies(
|
||||
projectDir: string,
|
||||
html: string,
|
||||
autoProxyOverride?: boolean,
|
||||
budgetMs?: number,
|
||||
): Promise<ProxyPreResolution> {
|
||||
const none: ProxyPreResolution = { findings: [], failedPaths: new Set(), files: [] };
|
||||
const none: ProxyPreResolution = {
|
||||
findings: [],
|
||||
failedPaths: new Set(),
|
||||
files: [],
|
||||
limits: new Map(),
|
||||
};
|
||||
if (!resolveAutoProxy(projectDir, autoProxyOverride)) return none;
|
||||
let codecMap: Awaited<ReturnType<typeof scanProjectMediaCodecMap>>;
|
||||
try {
|
||||
@@ -230,7 +269,14 @@ export async function preResolveHostileMediaProxies(
|
||||
|
||||
const startedAt = Date.now();
|
||||
const stageBox = findStageProxyBox(html);
|
||||
const sources = hostileEntries.map(([pathname, facts]) => {
|
||||
const sources: Array<{
|
||||
pathname: string;
|
||||
file: string;
|
||||
sourcePath: string;
|
||||
variant: ReturnType<typeof proxyVariantFor>;
|
||||
box: PreviewProxyBox | undefined;
|
||||
limitSeconds?: number;
|
||||
}> = hostileEntries.map(([pathname, facts]) => {
|
||||
const file = pathname.replace(/^\/+/, "");
|
||||
return {
|
||||
pathname,
|
||||
@@ -240,6 +286,15 @@ export async function preResolveHostileMediaProxies(
|
||||
box: stageBox,
|
||||
};
|
||||
});
|
||||
const windows =
|
||||
mediaUsedSeconds(html).size > 0
|
||||
? await copyWindows(
|
||||
projectDir,
|
||||
html,
|
||||
sources.map(({ pathname }) => pathname),
|
||||
)
|
||||
: new Map<string, number>();
|
||||
for (const source of sources) source.limitSeconds = windows.get(source.sourcePath);
|
||||
const total = hostileEntries.length;
|
||||
const cached = sources.map((source) => proxyCached(projectDir, source));
|
||||
const toMake = cached.filter((isCached) => !isCached).length;
|
||||
@@ -252,6 +307,14 @@ export async function preResolveHostileMediaProxies(
|
||||
const heartbeat = startCopyHeartbeat(sources, cached);
|
||||
const stopped = new AbortController();
|
||||
const forgetStop = onStopSignal(() => stopped.abort());
|
||||
let overBudget: number | undefined;
|
||||
const budget =
|
||||
budgetMs === undefined
|
||||
? undefined
|
||||
: setTimeout(() => {
|
||||
overBudget = budgetMs;
|
||||
stopped.abort();
|
||||
}, budgetMs);
|
||||
const results = await resolveProxies(
|
||||
projectDir,
|
||||
sources,
|
||||
@@ -265,7 +328,14 @@ export async function preResolveHostileMediaProxies(
|
||||
).finally(() => {
|
||||
heartbeat.stop();
|
||||
forgetStop();
|
||||
clearTimeout(budget);
|
||||
});
|
||||
const unfinished = sources.flatMap(({ file }, index) =>
|
||||
results[index]!.status === "rejected" ? [`${file} ${heartbeat.percent(index)}`] : [],
|
||||
);
|
||||
if (overBudget !== undefined && unfinished.length > 0) {
|
||||
throw new CheckCopiesUnfinishedError(overBudget, unfinished);
|
||||
}
|
||||
const failures = sources.flatMap((source, index) => {
|
||||
const result = results[index]!;
|
||||
return result.status === "rejected" ? [{ ...source, reason: result.reason }] : [];
|
||||
@@ -295,7 +365,12 @@ export async function preResolveHostileMediaProxies(
|
||||
}
|
||||
return { file, state: cached[index] ? "cached" : "ready" };
|
||||
});
|
||||
return { findings, failedPaths: new Set(failures.map(({ pathname }) => pathname)), files };
|
||||
return {
|
||||
findings,
|
||||
failedPaths: new Set(failures.map(({ pathname }) => pathname)),
|
||||
files,
|
||||
limits: windows,
|
||||
};
|
||||
}
|
||||
|
||||
function proxyCached(
|
||||
@@ -304,10 +379,19 @@ function proxyCached(
|
||||
sourcePath: string;
|
||||
variant: ReturnType<typeof proxyVariantFor>;
|
||||
box?: PreviewProxyBox;
|
||||
limitSeconds?: number;
|
||||
},
|
||||
): boolean {
|
||||
try {
|
||||
return existsSync(getProxyCachePath(projectDir, source.sourcePath, source.variant, source.box));
|
||||
return existsSync(
|
||||
getProxyCachePath(
|
||||
projectDir,
|
||||
source.sourcePath,
|
||||
source.variant,
|
||||
source.box,
|
||||
source.limitSeconds,
|
||||
),
|
||||
);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
@@ -325,6 +409,8 @@ interface ProxyPreResolution {
|
||||
failedPaths: Set<string>;
|
||||
/** One entry per browser-hostile video, in project order. */
|
||||
files: PreparedMediaFile[];
|
||||
/** Absolute path -> seconds from the start that the composition plays, for the copies made short. */
|
||||
limits: Map<string, number>;
|
||||
}
|
||||
|
||||
function proxyFailedDraft(message: string): RuntimeDraft {
|
||||
@@ -353,11 +439,11 @@ export async function runBrowserCheck(
|
||||
): Promise<CheckBrowserResult> {
|
||||
const html = await loadCheckHtmlWithDeferredMedia(project.dir);
|
||||
deadline?.enter("copies");
|
||||
const { findings: drafts, failedPaths } = await preResolveHostileMediaProxies(
|
||||
project.dir,
|
||||
html,
|
||||
options.autoProxy,
|
||||
);
|
||||
const {
|
||||
findings: drafts,
|
||||
failedPaths,
|
||||
limits,
|
||||
} = await preResolveHostileMediaProxies(project.dir, html, options.autoProxy, COPY_BUDGET_MS);
|
||||
deadline?.enter("launch");
|
||||
const server = await serveStaticProjectHtml(
|
||||
project.dir,
|
||||
@@ -365,6 +451,7 @@ export async function runBrowserCheck(
|
||||
"Failed to bind check server",
|
||||
[],
|
||||
options.autoProxy,
|
||||
limits,
|
||||
);
|
||||
const clock = createRuntimeClock();
|
||||
let failure: { kind: "none" } | { kind: "failed"; error: unknown } = { kind: "none" };
|
||||
@@ -667,6 +754,7 @@ async function withFindingCapturePage<T>(
|
||||
"Failed to bind check server",
|
||||
[],
|
||||
options.autoProxy,
|
||||
await copyWindows(project.dir, html, mediaUsedSeconds(html).keys()),
|
||||
);
|
||||
let chromeBrowser: import("puppeteer-core").Browser | undefined;
|
||||
let failure: { kind: "none" } | { kind: "failed"; error: unknown } = { kind: "none" };
|
||||
|
||||
@@ -38,6 +38,17 @@ export class CheckDeadlineError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
export const COPY_BUDGET_MS = 120_000;
|
||||
|
||||
export class CheckCopiesUnfinishedError extends Error {
|
||||
constructor(budgetMs: number, unfinished: readonly string[]) {
|
||||
super(
|
||||
`Making browser-playable video copies took longer than ${budgetMs / 1000} s: ${unfinished.join(", ")}. ` +
|
||||
"Finished pieces are kept. Run `hyperframes check --prepare-media` (no time limit) to finish them, then run check again.",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export class CheckStoppedError extends Error {
|
||||
constructor(signal: string) {
|
||||
super(`Check stopped before finishing (${signal}).`);
|
||||
|
||||
@@ -3,6 +3,7 @@ import { BrowserCleanupError, onStopSignal } from "../browser/launch.js";
|
||||
import type { CheckProgress } from "./checkProgress.js";
|
||||
import {
|
||||
CheckDeadline,
|
||||
CheckCopiesUnfinishedError,
|
||||
CheckDeadlineError,
|
||||
CheckStoppedError,
|
||||
idleWindow,
|
||||
@@ -2257,9 +2258,12 @@ function emptyBrowserResult(): CheckBrowserResult {
|
||||
};
|
||||
}
|
||||
|
||||
function runtimeFailure(error: unknown, code = "check_runtime_failure"): CheckFinding {
|
||||
function runtimeFailure(error: unknown, code?: string): CheckFinding {
|
||||
return findingAtRoot(
|
||||
code,
|
||||
code ??
|
||||
(error instanceof CheckCopiesUnfinishedError
|
||||
? "media_copies_unfinished"
|
||||
: "check_runtime_failure"),
|
||||
"error",
|
||||
normalizeErrorMessage(error),
|
||||
error instanceof CheckDeadlineError ? (error.sourceFile ?? "index.html") : "index.html",
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { mediaUsedSeconds } from "./mediaUsedSeconds.js";
|
||||
|
||||
const page = (body: string): string => `<html><body>${body}</body></html>`;
|
||||
|
||||
describe("mediaUsedSeconds", () => {
|
||||
it("reads the stretch a timed video plays, from its media offset", () => {
|
||||
const html = page(
|
||||
'<video src="assets/a.mov" data-start="0" data-duration="5" data-media-start="10"></video>',
|
||||
);
|
||||
expect(mediaUsedSeconds(html).get("/assets/a.mov")).toBe(15);
|
||||
});
|
||||
|
||||
it("takes the longest of several clips of one file, whatever the src spelling", () => {
|
||||
const html = page(
|
||||
'<video src="./a.mov?x=1" data-duration="4"></video><video src="/a.mov" data-duration="9"></video>',
|
||||
);
|
||||
expect(mediaUsedSeconds(html).get("/a.mov")).toBe(9);
|
||||
});
|
||||
|
||||
it("reads an untimed video's stretch from the wrapper that times it", () => {
|
||||
const html = page('<div data-start="2" data-duration="6"><video src="a.mov"></video></div>');
|
||||
expect(mediaUsedSeconds(html).get("/a.mov")).toBe(6);
|
||||
});
|
||||
|
||||
it("rounds a slower rate up, not down", () => {
|
||||
const html = page(
|
||||
'<video src="a.mov" data-duration="5" data-playback-rate="0.5"></video><video src="b.mov" data-duration="5" data-playback-rate="2"></video>',
|
||||
);
|
||||
const used = mediaUsedSeconds(html);
|
||||
expect([used.get("/a.mov"), used.get("/b.mov")]).toEqual([5, 10]);
|
||||
});
|
||||
|
||||
it("leaves a file out, to be copied whole, when any use of it is not a known stretch", () => {
|
||||
const html = page(
|
||||
[
|
||||
'<video src="loop.mov" data-duration="5" loop></video>',
|
||||
'<video src="untimed.mov"></video>',
|
||||
'<video src="mixed.mov" data-duration="5"></video><img src="mixed.mov">',
|
||||
'<video data-duration="5"><source src="inner.mov"></video>',
|
||||
].join(""),
|
||||
);
|
||||
expect([...mediaUsedSeconds(html).keys()]).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,52 @@
|
||||
import { parseHTML } from "linkedom";
|
||||
|
||||
const number = (value: string | null | undefined): number | undefined => {
|
||||
const parsed = Number(value);
|
||||
return value !== null && value !== undefined && value.trim() !== "" && Number.isFinite(parsed)
|
||||
? parsed
|
||||
: undefined;
|
||||
};
|
||||
|
||||
/** `clip.mov?x#y`, `./clip.mov` and `/clip.mov` all name the file `/clip.mov`. */
|
||||
function pathnameOf(src: string): string {
|
||||
const bare = src
|
||||
.replace(/[?#].*$/, "")
|
||||
.replace(/^\.\//, "")
|
||||
.replace(/^\/+/, "");
|
||||
try {
|
||||
return `/${decodeURIComponent(bare)}`;
|
||||
} catch {
|
||||
return `/${bare}`;
|
||||
}
|
||||
}
|
||||
|
||||
function plannedDuration(video: Element): number | undefined {
|
||||
for (let node: Element | null = video; node; node = node.parentElement) {
|
||||
const duration = number(node.getAttribute("data-duration"));
|
||||
if (duration !== undefined) return duration;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/** Seconds from the start of each file the composition plays, by pathname; a file with any unknown use is left out. */
|
||||
export function mediaUsedSeconds(html: string): Map<string, number> {
|
||||
const { document } = parseHTML(html);
|
||||
const used = new Map<string, number>();
|
||||
const whole = new Set<string>();
|
||||
for (const element of document.querySelectorAll("[src]")) {
|
||||
const pathname = pathnameOf(element.getAttribute("src") ?? "");
|
||||
const video = element.tagName === "VIDEO" ? element : null;
|
||||
const duration = video ? plannedDuration(video) : undefined;
|
||||
if (!video || duration === undefined || video.hasAttribute("loop")) {
|
||||
whole.add(pathname);
|
||||
continue;
|
||||
}
|
||||
const rate = Math.max(1, number(video.getAttribute("data-playback-rate")) ?? 1);
|
||||
const offset =
|
||||
(number(video.getAttribute("data-media-start")) ?? 0) +
|
||||
(number(video.getAttribute("data-playback-start")) ?? 0);
|
||||
used.set(pathname, Math.max(used.get(pathname) ?? 0, offset + duration * rate));
|
||||
}
|
||||
for (const pathname of whole) used.delete(pathname);
|
||||
return used;
|
||||
}
|
||||
@@ -232,6 +232,7 @@ async function serveProxyRequest(
|
||||
rangeHeader: string | undefined,
|
||||
res: ServerResponse,
|
||||
signal: AbortSignal,
|
||||
limitSeconds?: number,
|
||||
): Promise<void> {
|
||||
const contentType = getMimeType(filePath);
|
||||
if (!autoProxy || !contentType.startsWith("video/")) {
|
||||
@@ -259,7 +260,10 @@ async function serveProxyRequest(
|
||||
return;
|
||||
}
|
||||
recordProxyRequest();
|
||||
const proxyPath = await resolveProxy(projectDir, filePath, variant, box, { signal });
|
||||
const proxyPath = await resolveProxy(projectDir, filePath, variant, box, {
|
||||
signal,
|
||||
limitSeconds,
|
||||
});
|
||||
// The await above can span a whole transcode; the client may be gone.
|
||||
if (res.writableEnded || res.destroyed) return;
|
||||
serveFileWithRange(proxyPath, rangeHeader, res, PROXY_VARIANT_CONFIG[variant].contentType);
|
||||
@@ -278,6 +282,7 @@ export async function serveStaticProjectHtml(
|
||||
// Explicit CLI --proxy/--no-proxy value. Undefined preserves the project's
|
||||
// committed hyperframes.json setting.
|
||||
autoProxyOverride?: boolean,
|
||||
proxyLimits: ReadonlyMap<string, number> = new Map(),
|
||||
): Promise<StaticProjectServer> {
|
||||
const roots = [projectDir, ...assetRoots];
|
||||
// Codec-map injection lives here (not per-caller) so all seven callers of
|
||||
@@ -336,6 +341,7 @@ export async function serveStaticProjectHtml(
|
||||
req.headers.range,
|
||||
res,
|
||||
copies.signal,
|
||||
proxyLimits.get(filePath),
|
||||
);
|
||||
} else {
|
||||
if (!serveUntaggedPng(filePath, res))
|
||||
|
||||
@@ -243,6 +243,54 @@ describe("resolveProxy on a long source", () => {
|
||||
expect(copiesIn(cacheDirectory)).toEqual([cachePath.split("/").at(-1)]);
|
||||
});
|
||||
|
||||
it("copies only the first stretch when asked, as a copy of its own", async () => {
|
||||
const { spawn, calls } = createSpawnSpy();
|
||||
const { resolveProxy, getProxyCachePath, proxyWindowSeconds } = await loadModule(
|
||||
spawn,
|
||||
FFMPEG_PATH,
|
||||
false,
|
||||
null,
|
||||
undefined,
|
||||
LONG_SECONDS,
|
||||
);
|
||||
const projectDir = tmpProject();
|
||||
const sourcePath = join(projectDir, "long.mov");
|
||||
writeFileSync(sourcePath, "source-bytes");
|
||||
|
||||
const window = await proxyWindowSeconds(sourcePath, 5);
|
||||
expect(window).toBe(PIECE_SECONDS);
|
||||
const windowed = getProxyCachePath(projectDir, sourcePath, "h264", undefined, window);
|
||||
expect(windowed).not.toBe(getProxyCachePath(projectDir, sourcePath));
|
||||
|
||||
const result = resolveProxy(projectDir, sourcePath, "h264", undefined, {
|
||||
limitSeconds: window,
|
||||
});
|
||||
await flush();
|
||||
succeed(calls[0]!);
|
||||
|
||||
expect(await result).toBe(windowed);
|
||||
expect(calls).toHaveLength(1);
|
||||
expect(
|
||||
calls[0]!.args.slice(calls[0]!.args.indexOf("-t"), calls[0]!.args.indexOf("-t") + 2),
|
||||
).toEqual(["-t", String(PIECE_SECONDS)]);
|
||||
});
|
||||
|
||||
it("copies the whole source when the stretch saves less than a piece", async () => {
|
||||
const { spawn } = createSpawnSpy();
|
||||
const { proxyWindowSeconds } = await loadModule(
|
||||
spawn,
|
||||
FFMPEG_PATH,
|
||||
false,
|
||||
null,
|
||||
undefined,
|
||||
LONG_SECONDS,
|
||||
);
|
||||
const sourcePath = join(tmpProject(), "long.mov");
|
||||
writeFileSync(sourcePath, "source-bytes");
|
||||
|
||||
expect(await proxyWindowSeconds(sourcePath, LONG_SECONDS - 10)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("copies alpha pieces side by side, shares the cores out, and counts them together", async () => {
|
||||
const { spawn, calls } = createSpawnSpy();
|
||||
const { resolveProxy } = await loadModule(
|
||||
|
||||
@@ -183,8 +183,14 @@ function getCanonicalProxyCachePath(
|
||||
source: CanonicalProxySource,
|
||||
variant: ProxyVariant,
|
||||
box?: PreviewProxyBox,
|
||||
limitSeconds?: number,
|
||||
): string {
|
||||
const key = fastCopyKey(source.sourcePath, PROXY_PARAMS_VERSION, variant, box);
|
||||
const key = fastCopyKey(
|
||||
source.sourcePath,
|
||||
PROXY_PARAMS_VERSION,
|
||||
keyVariant(variant, limitSeconds),
|
||||
box,
|
||||
);
|
||||
const made = join(
|
||||
source.projectDir,
|
||||
CACHE_DIR_NAME,
|
||||
@@ -203,14 +209,37 @@ export function getProxyCachePath(
|
||||
absoluteSourcePath: string,
|
||||
variant: ProxyVariant = "h264",
|
||||
box?: PreviewProxyBox,
|
||||
limitSeconds?: number,
|
||||
): string {
|
||||
return getCanonicalProxyCachePath(
|
||||
canonicalizeProxySource(projectDir, absoluteSourcePath),
|
||||
variant,
|
||||
box,
|
||||
limitSeconds,
|
||||
);
|
||||
}
|
||||
|
||||
function copiedSeconds(duration: number | undefined, limitSeconds: number | undefined) {
|
||||
return limitSeconds === undefined ? duration : Math.min(limitSeconds, duration ?? limitSeconds);
|
||||
}
|
||||
|
||||
function lengthArgs(seconds: number | undefined): string[] {
|
||||
return seconds === undefined ? [] : ["-t", String(seconds)];
|
||||
}
|
||||
|
||||
function keyVariant(variant: ProxyVariant, limitSeconds: number | undefined): string {
|
||||
return limitSeconds === undefined ? variant : `${variant}@${limitSeconds}s`;
|
||||
}
|
||||
|
||||
export async function proxyWindowSeconds(
|
||||
absoluteSourcePath: string,
|
||||
neededSeconds: number,
|
||||
): Promise<number | undefined> {
|
||||
const { durationSeconds } = await probeMediaMetadata(absoluteSourcePath);
|
||||
const window = Math.ceil((neededSeconds + 1) / PROXY_CHUNK_SECONDS) * PROXY_CHUNK_SECONDS;
|
||||
return durationSeconds && window + PROXY_CHUNK_SECONDS <= durationSeconds ? window : undefined;
|
||||
}
|
||||
|
||||
// --- global concurrency limiter -------------------------------------------
|
||||
// ponytail: a counter + one wait queue is the whole semaphore. Priority copies
|
||||
// queue ahead of the rest and are never refused for a full queue, so a few
|
||||
@@ -694,6 +723,7 @@ async function runFfmpeg(
|
||||
chunkPrefix: string,
|
||||
variant: ProxyVariant,
|
||||
box: PreviewProxyBox | undefined,
|
||||
limitSeconds: number | undefined,
|
||||
signal: AbortSignal,
|
||||
onProgress: (progress: ProxyProgress) => void,
|
||||
isBackground: () => boolean = () => false,
|
||||
@@ -711,7 +741,7 @@ async function runFfmpeg(
|
||||
...["-map", "0:V:0"],
|
||||
...(metadata.audioStream === undefined ? [] : ["-map", `0:a:${metadata.audioStream}`]),
|
||||
];
|
||||
const totalSeconds = metadata.durationSeconds;
|
||||
const totalSeconds = copiedSeconds(metadata.durationSeconds, limitSeconds);
|
||||
// libvpx scales poorly across threads, so alpha pieces run side by side with the cores shared out.
|
||||
const gate = keepsAlpha ? ALPHA_PIECE_GATE : UNGATED;
|
||||
const vp8Threads = (pieces: number) => [
|
||||
@@ -731,7 +761,7 @@ async function runFfmpeg(
|
||||
"-i",
|
||||
sourcePath,
|
||||
...streamArgs,
|
||||
...(range ? ["-t", String(range.lengthSeconds)] : []),
|
||||
...lengthArgs(range?.lengthSeconds ?? limitSeconds),
|
||||
"-vf",
|
||||
videoFilter,
|
||||
...KEYFRAME_EVERY_SECOND_ARGS,
|
||||
@@ -883,6 +913,7 @@ async function transcodeToCache(
|
||||
cachePath: string,
|
||||
variant: ProxyVariant,
|
||||
box: PreviewProxyBox | undefined,
|
||||
limitSeconds: number | undefined,
|
||||
): Promise<string> {
|
||||
await acquireSlot(job);
|
||||
try {
|
||||
@@ -896,7 +927,7 @@ async function transcodeToCache(
|
||||
await contentCopyKey(
|
||||
absoluteSourcePath,
|
||||
PROXY_PARAMS_VERSION,
|
||||
variant,
|
||||
keyVariant(variant, limitSeconds),
|
||||
box,
|
||||
job.abandoned.signal,
|
||||
),
|
||||
@@ -928,6 +959,7 @@ async function transcodeToCache(
|
||||
chunkPrefix,
|
||||
variant,
|
||||
box,
|
||||
limitSeconds,
|
||||
job.abandoned.signal,
|
||||
(progress) => reportProgress(job, progress),
|
||||
() => job.background && !job.priority,
|
||||
@@ -1001,7 +1033,7 @@ export async function resolveProxy(
|
||||
): Promise<string> {
|
||||
options.signal?.throwIfAborted();
|
||||
const source = canonicalizeProxySource(projectDir, absoluteSourcePath);
|
||||
const cachePath = getCanonicalProxyCachePath(source, variant, box);
|
||||
const cachePath = getCanonicalProxyCachePath(source, variant, box, options.limitSeconds);
|
||||
if (existsSync(cachePath)) {
|
||||
markCacheEntryUsed(cachePath);
|
||||
maintainProxyCache(dirname(cachePath));
|
||||
@@ -1035,6 +1067,7 @@ export async function resolveProxy(
|
||||
cachePath,
|
||||
variant,
|
||||
box,
|
||||
options.limitSeconds,
|
||||
)
|
||||
.catch((err: unknown) => {
|
||||
if (!(err instanceof ProxyDroppedError) && !job.abandoned.signal.aborted) {
|
||||
@@ -1066,7 +1099,12 @@ export async function resolveProxy(
|
||||
* progress (TRANSCODE_STALL_MS), so a long source that keeps moving is never cut by a clock. */
|
||||
export async function resolveProxies(
|
||||
projectDir: string,
|
||||
sources: ReadonlyArray<{ sourcePath: string; variant: ProxyVariant; box?: PreviewProxyBox }>,
|
||||
sources: ReadonlyArray<{
|
||||
sourcePath: string;
|
||||
variant: ProxyVariant;
|
||||
box?: PreviewProxyBox;
|
||||
limitSeconds?: number;
|
||||
}>,
|
||||
onSettled: (index: number) => void = () => {},
|
||||
onProgress: (index: number, progress: ProxyProgress) => void = () => {},
|
||||
signal?: AbortSignal,
|
||||
@@ -1085,8 +1123,9 @@ export async function resolveProxies(
|
||||
let next = 0;
|
||||
const worker = async (): Promise<void> => {
|
||||
for (let index = next++; index < sources.length; index = next++) {
|
||||
const { sourcePath, variant, box } = sources[index]!;
|
||||
const { sourcePath, variant, box, limitSeconds } = sources[index]!;
|
||||
results[index] = await resolveProxy(projectDir, sourcePath, variant, box, {
|
||||
limitSeconds,
|
||||
onProgress: (progress) => onProgress(index, progress),
|
||||
signal,
|
||||
}).then(
|
||||
@@ -1153,6 +1192,7 @@ async function makeStill(
|
||||
}
|
||||
|
||||
export interface ResolveProxyOptions {
|
||||
limitSeconds?: number;
|
||||
priority?: boolean;
|
||||
background?: boolean;
|
||||
signal?: AbortSignal;
|
||||
|
||||
Reference in New Issue
Block a user