Skip to content

Commit da4386f

Browse files
perf(aws-lambda): cloud renders plan without reading video bytes and start chunks sooner (#6194)
* perf(engine): a remote source's tail decodes run many at a time, not one per half-core * perf(producer): the plan logs where its extract stage spends its time * perf(engine): a window that ends inside its source takes its frame count without a decode * test(engine): pin the tail-decode slot test to the windows that decode * style(engine): format the tail-probe window check * test(engine): deferred counts match extraction on a B-frame NTSC source; VFR is not deferred * style(engine): format the tail-probe tests * perf(aws-lambda): a site stores each video's ffprobe JSON, and the plan reads it instead of probing * test(aws-lambda): a site stores and hands out each video's probe * style(aws-lambda): format the probe-facts upload and tests * perf(engine): a site's stored probe also holds the final-frame timestamp the plan would probe for * test(engine): drop an unused import * test(aws-lambda): the stored probe holds the ffprobe JSON and the final frame * test(aws-lambda): the stored probe holds the ffprobe JSON and the final frame * perf(producer): a distributed chunk encodes while it captures * perf(producer): the audio task logs where its mix spends its time One line per audio stage: prepare, groups, mix, master, encode and the peak check, in ms. * perf(engine): locked warmup ticks stop sleeping once the page is ready * perf(engine): a window reaching a stored video's end takes its frame count from stored facts * fix(producer): the cloud audio task logs its phases * perf(engine): stored facts keep the last packet length so late-start windows skip the tail decode * fix(engine): a probe spawns without waiting when no stored facts are declared * chore(aws-lambda): shorten comments over the line limit * fix(engine): warmup keeps pacing after a failed tick; stored final frames check ffprobe major * test(engine): the recorded ffprobe major matches the build's own banner * test(engine): the version-banner probes go through a named binary like the other spawns
1 parent 80be6ff commit da4386f

27 files changed

Lines changed: 1063 additions & 112 deletions

‎packages/aws-lambda/src/contentStore.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export function contentStoreUri(bucket: string, sha256: string): string {
1414
return `s3://${bucket}/${CONTENT_STORE_PREFIX}/${sha256.slice(0, 2)}/${sha256}`;
1515
}
1616

17+
export function probeFactsUri(bucket: string, sha256: string): string {
18+
return `${contentStoreUri(bucket, sha256)}.probe.v2.json`;
19+
}
20+
1721
export function audioSidecarUri(bucket: string, sha256: string): string {
1822
return `${contentStoreUri(bucket, sha256)}.audio.m4a`;
1923
}

‎packages/aws-lambda/src/s3Transport.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import * as tar from "tar";
4949
import {
5050
CONTENT_STORE_REFRESH_AFTER_DAYS,
5151
audioSidecarUri,
52+
probeFactsUri,
5253
contentStoreUri,
5354
} from "./contentStore.js";
5455

@@ -710,3 +711,30 @@ export async function untarDirectory(tarballPath: string, destDir: string): Prom
710711
mkdirSync(destDir, { recursive: true });
711712
await tar.extract({ file: tarballPath, cwd: destDir });
712713
}
714+
715+
export async function putProbeFacts(
716+
client: S3Client,
717+
localPath: string,
718+
bucket: string,
719+
sha256: string,
720+
): Promise<"uploaded" | "reused"> {
721+
const uri = probeFactsUri(bucket, sha256);
722+
if (await s3ObjectExists(client, uri)) return "reused";
723+
const { key } = parseS3Uri(uri);
724+
const size = statSync(localPath).size;
725+
try {
726+
await putImmutableBody(
727+
client,
728+
localPath,
729+
bucket,
730+
key,
731+
size,
732+
await sha256File(localPath),
733+
"application/json",
734+
);
735+
} catch (error) {
736+
if (!isS3PreconditionFailed(error)) throw error;
737+
return "reused";
738+
}
739+
return "uploaded";
740+
}

‎packages/aws-lambda/src/sdk/deploySite.test.ts‎

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { join } from "node:path";
66
import { createHash } from "node:crypto";
77
import type { S3Client } from "@aws-sdk/client-s3";
88
import { AUDIO_SIDECAR_MIN_BYTES } from "@hyperframes/producer/distributed";
9-
import { audioSidecarUri } from "../contentStore.js";
9+
import { audioSidecarUri, probeFactsUri } from "../contentStore.js";
1010
import { parseSiteManifest } from "../siteManifest.js";
1111
import { asS3Client, FakeS3 } from "./__fixtures__/fakeS3.js";
1212
import { deploySite } from "./deploySite.js";
@@ -275,3 +275,55 @@ describe.skipIf(!HAS_FFMPEG)("deploySite audio sidecars", { timeout: 60_000 }, (
275275
expect(notices[0]).toContain("ffmpeg");
276276
});
277277
});
278+
279+
describe.skipIf(!HAS_FFMPEG)("deploySite probe facts", { timeout: 60_000 }, () => {
280+
function addVideo(): void {
281+
const made = spawnSync(FFMPEG, [
282+
...["-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=size=64x64:rate=25:duration=2"],
283+
...["-c:v", "mpeg4", join(projectDir, "assets", "take.mp4")],
284+
]);
285+
expect(made.status).toBe(0);
286+
}
287+
288+
it("stores each video's ffprobe JSON beside it, and leaves the site's file list as it was", async () => {
289+
addVideo();
290+
const s3 = new FakeS3();
291+
const site = await deploySite({ projectDir, bucketName: "test-bucket", s3: asS3Client(s3) });
292+
293+
const take = parseSiteManifest(
294+
s3.bodies.get(`test-bucket/sites/${site.siteId}/files.json`)?.toString() ?? "",
295+
).find((file) => file.path === "assets/take.mp4");
296+
const facts = probeFactsUri("test-bucket", take?.sha256 ?? "").replace("s3://", "");
297+
const stored = JSON.parse(s3.bodies.get(facts)?.toString() ?? "{}");
298+
expect(stored.ffprobe.streams[0].codec_type).toBe("video");
299+
expect(stored.finalFrame.seconds).toBeGreaterThan(1.5);
300+
expect(stored.finalFrame.lastPacketSeconds).toBeGreaterThan(0);
301+
expect(sitePaths(s3, site.siteId)).toEqual([
302+
"assets/style.css",
303+
"assets/take.mp4",
304+
"index.html",
305+
]);
306+
});
307+
308+
it("deploys the same site with no probe stored when there is no ffprobe, and says so", async () => {
309+
addVideo();
310+
const notices: string[] = [];
311+
const before = process.env.HYPERFRAMES_FFPROBE_PATH;
312+
process.env.HYPERFRAMES_FFPROBE_PATH = join(scratch, "no-such-ffprobe");
313+
const s3 = new FakeS3();
314+
try {
315+
await deploySite({
316+
projectDir,
317+
bucketName: "test-bucket",
318+
s3: asS3Client(s3),
319+
onNotice: (message) => notices.push(message),
320+
});
321+
} finally {
322+
if (before === undefined) delete process.env.HYPERFRAMES_FFPROBE_PATH;
323+
else process.env.HYPERFRAMES_FFPROBE_PATH = before;
324+
}
325+
326+
expect([...s3.existing].filter((id) => id.endsWith(".probe.v2.json"))).toEqual([]);
327+
expect(notices.some((message) => message.includes("ffprobe"))).toBe(true);
328+
});
329+
});

‎packages/aws-lambda/src/sdk/deploySite.ts‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ import { existsSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync
1515
import { tmpdir } from "node:os";
1616
import { join } from "node:path";
1717
import { S3Client } from "@aws-sdk/client-s3";
18-
import { CONTENT_STORE_RETENTION_DAYS, audioSidecarUri } from "../contentStore.js";
18+
import { CONTENT_STORE_RETENTION_DAYS, audioSidecarUri, probeFactsUri } from "../contentStore.js";
1919
import {
2020
buildAudioSidecar,
21+
buildProbeFacts,
2122
hashProjectDir,
2223
listSiteFiles,
2324
wantsAudioSidecar,
25+
wantsProbeFacts,
2426
} from "@hyperframes/producer/distributed";
2527
import {
2628
downloadS3ObjectToFile,
@@ -29,6 +31,7 @@ import {
2931
mapConcurrent,
3032
parseS3Uri,
3133
putAudioSidecar,
34+
putProbeFacts,
3235
putContentStoreFile,
3336
s3ObjectExists,
3437
sha256File,
@@ -167,6 +170,7 @@ export async function deploySite(opts: DeploySiteOptions): Promise<SiteHandle> {
167170
uploaded ||= outcome === "uploaded";
168171
});
169172
stored.sort((a, b) => (a.path < b.path ? -1 : 1));
173+
await storeProbeFacts(s3, opts, stored);
170174
await storeAudioSidecars(s3, opts, stored);
171175

172176
const workdir = mkdtempSync(join(tmpdir(), "hf-deploy-site-"));
@@ -228,3 +232,36 @@ async function storeAudioSidecars(
228232
);
229233
}
230234
}
235+
236+
/**
237+
* Each video's ffprobe JSON, stored beside it, so a render reads kilobytes instead of probing the video.
238+
* Optional: a machine without ffprobe leaves the site as it was, and renders probe the video.
239+
*/
240+
async function storeProbeFacts(
241+
s3: S3Client,
242+
opts: DeploySiteOptions,
243+
files: readonly SiteFile[],
244+
): Promise<void> {
245+
const videos = files.filter((file) => wantsProbeFacts(file.path));
246+
if (videos.length === 0) return;
247+
const workdir = mkdtempSync(join(tmpdir(), "hf-probe-facts-"));
248+
let unavailable = false;
249+
try {
250+
await mapConcurrent(videos, 4, async ({ path, sha256 }) => {
251+
if (unavailable || (await s3ObjectExists(s3, probeFactsUri(opts.bucketName, sha256)))) return;
252+
const factsPath = join(workdir, `${sha256}.json`);
253+
const outcome = await buildProbeFacts(join(opts.projectDir, path), factsPath);
254+
if (outcome === "unavailable") unavailable = true;
255+
if (outcome === "built") await putProbeFacts(s3, factsPath, opts.bucketName, sha256);
256+
rmSync(factsPath, { force: true });
257+
});
258+
} finally {
259+
rmSync(workdir, { recursive: true, force: true });
260+
}
261+
if (unavailable) {
262+
opts.onNotice?.(
263+
"ffprobe was not found on this machine, so what renders learn about your videos was not stored: " +
264+
"renders will probe each video themselves and plan more slowly. Install ffmpeg and run `sites create` again.",
265+
);
266+
}
267+
}

‎packages/aws-lambda/src/siteManifest.test.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { mkdtempSync, rmSync } from "node:fs";
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
55
import { S3Client } from "@aws-sdk/client-s3";
6-
import { audioSidecarUri, contentStoreUri } from "./contentStore.js";
6+
import { audioSidecarUri, contentStoreUri, probeFactsUri } from "./contentStore.js";
77
import { FakeS3 } from "./sdk/__fixtures__/fakeS3.js";
88
import { downloadSiteFiles, serializeSiteManifest, siteManifestUri } from "./siteManifest.js";
99

@@ -70,4 +70,19 @@ describe("downloadSiteFiles", () => {
7070
expect(remote["takes/camera.mov"]?.url).toBeString();
7171
expect(remote["takes/camera.mov"]?.audioSidecarUrl).toBeUndefined();
7272
});
73+
74+
it("hands a remote video's stored probe along with it, when one is stored", async () => {
75+
const fake = new FakeS3();
76+
const manifestUri = siteWithVideo(fake);
77+
fake.existing.add(probeFactsUri("bucket", VIDEO_SHA).replace("s3://", ""));
78+
79+
const remote = await downloadSiteFiles(
80+
clientOver(fake),
81+
manifestUri,
82+
join(dir, "project"),
83+
true,
84+
);
85+
86+
expect(remote["takes/camera.mov"]?.probeFactsUrl).toContain(`${VIDEO_SHA}.probe.v2.json`);
87+
});
7388
});

‎packages/aws-lambda/src/siteManifest.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { readFileSync } from "node:fs";
55
import { extname, join } from "node:path";
66
import type { S3Client } from "@aws-sdk/client-s3";
77
import type { RemoteProjectFile } from "@hyperframes/producer/distributed";
8-
import { audioSidecarUri, contentStoreUri } from "./contentStore.js";
8+
import { audioSidecarUri, contentStoreUri, probeFactsUri } from "./contentStore.js";
99
import {
1010
downloadS3ObjectToFile,
1111
downloadS3ObjectToFileVerified,
@@ -82,11 +82,19 @@ export async function downloadSiteFiles(
8282
const uri = contentStoreUri(bucket, sha256);
8383
if (leaveVideosRemote && VIDEO_EXTENSIONS_PLANNED_IN_PLACE.has(extname(path).toLowerCase())) {
8484
const sidecar = audioSidecarUri(bucket, sha256);
85+
const facts = probeFactsUri(bucket, sha256);
86+
const [hasSidecar, hasFacts] = await Promise.all([
87+
s3ObjectExists(s3, sidecar),
88+
s3ObjectExists(s3, facts),
89+
]);
8590
remoteFiles[path] = {
8691
url: await presignS3GetUrl(s3, uri, REMOTE_URL_TTL_SECONDS),
87-
...((await s3ObjectExists(s3, sidecar))
92+
...(hasSidecar
8893
? { audioSidecarUrl: await presignS3GetUrl(s3, sidecar, REMOTE_URL_TTL_SECONDS) }
8994
: {}),
95+
...(hasFacts
96+
? { probeFactsUrl: await presignS3GetUrl(s3, facts, REMOTE_URL_TTL_SECONDS) }
97+
: {}),
9098
sha256,
9199
sizeBytes,
92100
};

‎packages/engine/src/index.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ export {
331331
extractFinalVideoFrameTimestamp,
332332
extractAudioMetadata,
333333
declareContentAddressedUrl,
334+
declareProbeFactsUrl,
335+
PROBE_FACTS_VERSION,
336+
buildProbeFactsText,
334337
probeMediaProfile,
335338
analyzeKeyframeIntervals,
336339
type VideoMetadata,

‎packages/engine/src/services/audioMixer.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,43 @@ describe("processCompositionAudio", () => {
711711
},
712712
);
713713

714+
it("reports how long each phase of the mix took", async () => {
715+
const baseDir = mkdtempSync(join(tmpdir(), "hf-audio-base-"));
716+
const workDir = mkdtempSync(join(tmpdir(), "hf-audio-work-"));
717+
tempDirs.push(baseDir, workDir);
718+
writeFileSync(join(baseDir, "bed.wav"), "stub");
719+
720+
const result = await processCompositionAudio(
721+
[
722+
{
723+
id: "bed",
724+
src: "bed.wav",
725+
start: 0,
726+
end: 2,
727+
mediaStart: 0,
728+
layer: 0,
729+
volume: 1,
730+
type: "audio",
731+
},
732+
],
733+
baseDir,
734+
workDir,
735+
join(baseDir, "out.m4a"),
736+
2,
737+
undefined,
738+
{ masterFxChain: gainChain },
739+
);
740+
741+
expect(result.success).toBe(true);
742+
expect(Object.keys(result.phaseMs ?? {}).sort()).toEqual([
743+
"encode",
744+
"groups",
745+
"master",
746+
"mix",
747+
"prepare",
748+
]);
749+
});
750+
714751
it.each([
715752
["the master pass", {}, { masterFxChain: gainChain }],
716753
["a group bus", { groupId: "bus", groupFxChain: gainChain }, {}],

‎packages/engine/src/services/audioMixer.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1348,9 +1348,11 @@ async function mixThroughFloatSum(
13481348
},
13491349
): Promise<MixResult> {
13501350
const sumPath = join(workDir, "master-sum.wav");
1351+
const mixStart = Date.now();
13511352
const sum = await mixAudioTracks(tracks, sumPath, totalDuration, signal, config);
13521353
if (!sum.success) return { ...sum, outputPath };
13531354

1355+
const masterStart = Date.now();
13541356
const encoderInput = masterChain
13551357
? (
13561358
await applyAudioFxChain(sumPath, masterChain, join(workDir, "master-fx.wav"), {
@@ -1359,6 +1361,7 @@ async function mixThroughFloatSum(
13591361
})
13601362
).path
13611363
: sumPath;
1364+
const encodeStart = Date.now();
13621365
const encoded = await encodeAac(encoderInput, outputPath, totalDuration, {
13631366
signal,
13641367
timeout: config?.ffmpegProcessTimeout ?? DEFAULT_CONFIG.ffmpegProcessTimeout,
@@ -1379,7 +1382,16 @@ async function mixThroughFloatSum(
13791382
};
13801383
}
13811384
if (config?.keepEncoderInputAt) renameWithRetry(encoderInput, config.keepEncoderInputAt);
1382-
return { ...sum, outputPath, durationMs: sum.durationMs + encoded.durationMs };
1385+
return {
1386+
...sum,
1387+
outputPath,
1388+
durationMs: sum.durationMs + encoded.durationMs,
1389+
phaseMs: {
1390+
mix: masterStart - mixStart,
1391+
master: encodeStart - masterStart,
1392+
encode: Date.now() - encodeStart,
1393+
},
1394+
};
13831395
}
13841396

13851397
export async function processCompositionAudio(
@@ -1752,6 +1764,7 @@ export async function processCompositionAudio(
17521764
failures,
17531765
};
17541766
};
1767+
const prepareMs = Date.now() - startMs;
17551768
if (failures.length > 0) return bail();
17561769

17571770
// Parsed before any group is mixed, so a malformed master chain fails fast and
@@ -1899,6 +1912,7 @@ export async function processCompositionAudio(
18991912
}
19001913
if (failures.length > 0) return bail();
19011914

1915+
const groupsMs = Date.now() - startMs - prepareMs;
19021916
let mixResult: MixResult;
19031917
try {
19041918
mixResult =
@@ -1928,6 +1942,7 @@ export async function processCompositionAudio(
19281942
return {
19291943
...mixResult,
19301944
durationMs: Date.now() - startMs,
1945+
phaseMs: { prepare: prepareMs, groups: groupsMs, ...mixResult.phaseMs },
19311946
// Both, when both degraded. `mixResult.error ?? degradedNote` reported only
19321947
// the outer mix and dropped the one that names which GROUPS lost their
19331948
// members' automation — two different losses, and the operator needs to

‎packages/engine/src/services/audioMixer.types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,5 @@ export interface MixResult {
113113
tracksProcessed: number;
114114
error?: string;
115115
failures?: AudioProcessingFailure[];
116+
phaseMs?: Partial<Record<"prepare" | "groups" | "mix" | "master" | "encode", number>>;
116117
}

0 commit comments

Comments
 (0)