Skip to content

Commit 472b750

Browse files
fix(cli): cloud export re-encodes media whose bytes differ from its extension (#7033)
* fix(cli): cloud export re-encodes media whose bytes are not the type its extension claims * test(cli): generated media in the offload tests carries its container header * fix(cli): an audio-only MP4 named as video is re-encoded as audio * test(cli): type the header fixture * test(cli): give the heavy-media dry-run fixture a real mp4 header
1 parent acd5c30 commit 472b750

4 files changed

Lines changed: 194 additions & 10 deletions

File tree

‎packages/cli/src/cloud/offload.test.ts‎

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
writeFileSync,
1212
} from "node:fs";
1313
import { tmpdir } from "node:os";
14-
import { dirname, join } from "node:path";
14+
import { dirname, extname, join } from "node:path";
1515
import { buildPublishFileMap, stagePublishFiles } from "../utils/publishProject.js";
1616
import {
1717
type GifPrep,
@@ -20,6 +20,7 @@ import {
2020
type Reencoder,
2121
type UploadableFile,
2222
} from "./offload.js";
23+
import { bytesMatchExtension } from "./offloadSniff.js";
2324

2425
vi.mock("node:fs", async (importOriginal) => {
2526
const actual = await importOriginal<typeof import("node:fs")>();
@@ -37,12 +38,30 @@ afterEach(() => {
3738
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
3839
});
3940

41+
// The first bytes a real file of each extension starts with; HEAVY stands in for an honest file.
42+
const HEADERS: Record<string, Buffer> = {
43+
".mp4": Buffer.concat([Buffer.from([0, 0, 0, 24]), Buffer.from("ftypisom")]),
44+
".mov": Buffer.concat([Buffer.from([0, 0, 0, 20]), Buffer.from("ftypqt ")]),
45+
".png": Buffer.from([0x89, 0x50, 0x4e, 0x47, 13, 10, 26, 10]),
46+
".mp3": Buffer.from("ID3\u0004\u0000"),
47+
".webm": Buffer.from([0x1a, 0x45, 0xdf, 0xa3]),
48+
};
49+
50+
function honest(path: string): Buffer {
51+
const header = HEADERS[extname(path).toLowerCase()] ?? Buffer.alloc(0);
52+
return Buffer.concat([header, HEAVY.subarray(header.length)]);
53+
}
54+
4055
function project(files: Record<string, string | Buffer>): string {
4156
const dir = mkdtempSync(join(tmpdir(), "hf-offload-test-"));
4257
dirs.push(dir);
4358
for (const [path, content] of Object.entries(files)) {
4459
mkdirSync(dirname(join(dir, path)), { recursive: true });
45-
writeFileSync(join(dir, path), content);
60+
const header = content === HEAVY ? HEADERS[extname(path).toLowerCase()] : undefined;
61+
writeFileSync(
62+
join(dir, path),
63+
header ? Buffer.concat([header, HEAVY.subarray(header.length)]) : content,
64+
);
4665
}
4766
return dir;
4867
}
@@ -319,6 +338,82 @@ describe("offloadHeavyMedia", () => {
319338
expect(fileMap.has("take.mkv")).toBe(false);
320339
});
321340

341+
it("re-encodes an AAC file named .mp3 instead of uploading it as audio/mpeg", async () => {
342+
const m4a = Buffer.concat([
343+
Buffer.from([0, 0, 0, 28]),
344+
Buffer.from("ftypM4A "),
345+
Buffer.alloc(OFFLOAD_MIN_BYTES, 1),
346+
]);
347+
const dir = project({
348+
"index.html": html(`<audio src="song.mp3"></audio>`),
349+
"song.mp3": m4a,
350+
});
351+
const reencoder: Reencoder = {
352+
choose: vi.fn(async () => "audio" as const),
353+
run: vi.fn(async () => ({
354+
bytes: Buffer.from("wav"),
355+
filename: "song.wav",
356+
contentType: "audio/wav",
357+
})),
358+
};
359+
const upload = fakeUpload();
360+
361+
const report = await offloadHeavyMedia(dir, buildPublishFileMap(dir), {
362+
apiBaseUrl: "https://api.example",
363+
upload,
364+
reencoder,
365+
cachePath: cachePath(),
366+
});
367+
368+
expect(report[0]).toMatchObject({ path: "song.mp3", action: "reencode" });
369+
expect(upload.mock.calls[0]![0]).toMatchObject({
370+
filename: "song.wav",
371+
contentType: "audio/wav",
372+
});
373+
});
374+
375+
it("treats an audio-only MP4 named .mp4 as audio, whatever brand its ftyp box has", async () => {
376+
const audioOnly = Buffer.concat([
377+
HEADERS[".mp4"]!,
378+
Buffer.alloc(OFFLOAD_MIN_BYTES - HEADERS[".mp4"]!.length, 1),
379+
]);
380+
const dir = project({
381+
"index.html": html(`<audio src="voice.mp4"></audio>`),
382+
"voice.mp4": audioOnly,
383+
});
384+
const reencoder: Reencoder = {
385+
hasVideo: async () => false,
386+
choose: vi.fn(async (_path, kind) => (kind === "audio" ? ("audio" as const) : null)),
387+
run: vi.fn(async () => ({
388+
bytes: Buffer.from("wav"),
389+
filename: "voice.wav",
390+
contentType: "audio/wav",
391+
})),
392+
};
393+
const upload = fakeUpload();
394+
395+
const report = await offloadHeavyMedia(dir, buildPublishFileMap(dir), {
396+
apiBaseUrl: "https://api.example",
397+
upload,
398+
reencoder,
399+
cachePath: cachePath(),
400+
});
401+
402+
expect(reencoder.choose).toHaveBeenCalledWith(expect.any(String), "audio");
403+
expect(report[0]).toMatchObject({ path: "voice.mp4", action: "reencode" });
404+
expect(upload.mock.calls[0]![0]).toMatchObject({ filename: "voice.wav" });
405+
});
406+
407+
it("only takes bytes as-is when they are the container the extension names", () => {
408+
const wave = Buffer.from("RIFF\0\0\0\0WAVEfmt ");
409+
expect(bytesMatchExtension(".wav", wave)).toBe(true);
410+
expect(bytesMatchExtension(".mp3", wave)).toBe(false);
411+
expect(bytesMatchExtension(".mp3", Buffer.from([0xff, 0xfb, 0x90, 0x00]))).toBe(true);
412+
expect(bytesMatchExtension(".mp4", HEADERS[".mp4"]!)).toBe(true);
413+
expect(bytesMatchExtension(".mp4", Buffer.from("\0\0\0\x1cftypM4A "))).toBe(false);
414+
expect(bytesMatchExtension(".jpg", HEADERS[".png"]!)).toBe(false);
415+
});
416+
322417
it("fails naming the file when a re-encode is still over the asset cap", async () => {
323418
const dir = project({
324419
"index.html": html(`<video src="long.mkv"></video>`),
@@ -417,7 +512,7 @@ describe("offloadHeavyMedia", () => {
417512
});
418513
const gifPrep = vi.fn<GifPrep>(async (input, options) => {
419514
const outputPath = join(options.outputDir, "hfgif-v1-abc.webm");
420-
writeFileSync(outputPath, HEAVY);
515+
writeFileSync(outputPath, honest(outputPath));
421516
return {
422517
html: input.replace(
423518
`<img src="media/loop.gif" data-loop="true" data-duration="4" />`,
@@ -464,7 +559,7 @@ describe("offloadHeavyMedia", () => {
464559
const gifPrep = vi.fn<GifPrep>(async (input, options) => {
465560
prepared = readFileSync(options.sourceAssets!.get("logo.gif")!);
466561
const outputPath = join(options.outputDir, "hfgif-v1-abc.webm");
467-
writeFileSync(outputPath, HEAVY);
562+
writeFileSync(outputPath, honest(outputPath));
468563
return {
469564
html: input.replace(
470565
`<img src="logo.gif" />`,

‎packages/cli/src/cloud/offload.ts‎

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
import { execFile } from "node:child_process";
77
import { createHash } from "node:crypto";
88
import {
9+
closeSync,
910
copyFileSync,
1011
createReadStream,
1112
mkdirSync,
1213
mkdtempSync,
14+
openSync,
1315
readFileSync,
16+
readSync,
1417
renameSync,
1518
rmSync,
1619
writeFileSync,
@@ -29,6 +32,7 @@ import {
2932
import { findFFmpeg, findFFprobe, getFFmpegInstallHint } from "../browser/ffmpeg.js";
3033
import { probeAudio } from "../audio/analysis/decode.js";
3134
import { formatBytes } from "../ui/format.js";
35+
import { bytesMatchExtension } from "./offloadSniff.js";
3236
import { type PlayedSegment, planTrim } from "./offloadTrim.js";
3337
import {
3438
assertUnchanged,
@@ -177,6 +181,8 @@ export interface Reencoder {
177181
choose: (sourcePath: string, kind: MediaKind) => Promise<PresetName | null>;
178182
/** Positions per second a cut may land on (the frame rate), or null when the file cannot be cut. */
179183
trimGrid?: (sourcePath: string, kind: MediaKind) => Promise<number | null>;
184+
/** Whether a container named as video holds a video stream; `true` when that cannot be told. */
185+
hasVideo?: (sourcePath: string) => Promise<boolean>;
180186
run: (
181187
sourcePath: string,
182188
preset: PresetName,
@@ -447,7 +453,34 @@ async function trimGrid(sourcePath: string, kind: MediaKind): Promise<number | n
447453
return Number.isFinite(fps) && Math.abs(fps - rate(average)) < 0.01 ? fps : null;
448454
}
449455

450-
const ffmpegReencoder: Reencoder = { choose: choosePreset, trimGrid, run: ffmpegReencode };
456+
async function hasVideoStream(sourcePath: string): Promise<boolean> {
457+
const ffprobe = findFFprobe();
458+
if (!ffprobe) return true;
459+
try {
460+
const { stdout } = await execFileAsync(ffprobe, [
461+
"-v",
462+
"error",
463+
"-select_streams",
464+
"v:0",
465+
"-show_entries",
466+
"stream=codec_type",
467+
"-of",
468+
"csv=p=0",
469+
"--",
470+
sourcePath,
471+
]);
472+
return stdout.trim() !== "";
473+
} catch {
474+
return true;
475+
}
476+
}
477+
478+
const ffmpegReencoder: Reencoder = {
479+
choose: choosePreset,
480+
trimGrid,
481+
hasVideo: hasVideoStream,
482+
run: ffmpegReencode,
483+
};
451484

452485
async function stagedSha256(file: StagedFile): Promise<string> {
453486
if (Buffer.isBuffer(file)) return createHash("sha256").update(file).digest("hex");
@@ -529,24 +562,48 @@ interface FilePlan {
529562
segments?: PlayedSegment[];
530563
}
531564

565+
function readHead(file: StagedFile): Buffer {
566+
if (Buffer.isBuffer(file)) return file.subarray(0, 16);
567+
const fd = openSync(file.absPath, "r");
568+
try {
569+
const head = Buffer.alloc(16);
570+
readSync(fd, head, 0, 16, 0);
571+
return head;
572+
} finally {
573+
closeSync(fd);
574+
}
575+
}
576+
532577
async function planFile(
533578
ctx: OffloadContext,
534579
path: string,
535580
file: StagedFile,
536581
): Promise<FilePlan | null> {
537-
const kind = mediaKind(path);
582+
let kind = mediaKind(path);
538583
const size = stagedSize(file);
539584
if (!kind || size < OFFLOAD_MIN_BYTES) return null;
540-
const accepted = ACCEPTED[extname(path).toLowerCase()];
541-
const asIs = accepted !== undefined && size <= accepted.maxBytes;
585+
const ext = extname(path).toLowerCase();
586+
const accepted = ACCEPTED[ext];
587+
// The API reads the stored bytes, not the name: a file whose bytes differ from its extension is re-encoded.
588+
let asIs =
589+
accepted !== undefined && size <= accepted.maxBytes && bytesMatchExtension(ext, readHead(file));
590+
let sourcePath: string | undefined;
591+
if (kind === "video" && ctx.reencoder.hasVideo) {
592+
sourcePath = snapshotPath(path, file, ctx.tempFiles);
593+
// An audio-only MP4 is audio whatever its name says.
594+
if (!(await ctx.reencoder.hasVideo(sourcePath))) {
595+
kind = "audio";
596+
asIs = false;
597+
}
598+
}
542599
const mayBeCached =
543600
ctx.opts.upload !== undefined || (asIs ? ctx.cachedSizes.has(size) : ctx.cachedSizes.size > 0);
544601
const sha256 = mayBeCached ? await stagedSha256(file) : undefined;
545602
const keyFor = (variant: string) => sha256 && `${ctx.opts.apiBaseUrl} ${sha256} ${variant}`;
546603
if (asIs) {
547604
return { key: keyFor("as-is"), sourcePath: "", contentType: accepted?.contentType };
548605
}
549-
const sourcePath = snapshotPath(path, file, ctx.tempFiles);
606+
sourcePath ??= snapshotPath(path, file, ctx.tempFiles);
550607
const preset = await ctx.reencoder.choose(sourcePath, kind);
551608
// Files left as authored stay in the zip; the archive size check names them.
552609
if (!preset) return null;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/** Whether a file's first bytes are the container its extension claims, for the types the asset API takes as-is. */
2+
export function bytesMatchExtension(ext: string, head: Buffer): boolean {
3+
const at = (offset: number, text: string) =>
4+
head.toString("latin1", offset, offset + text.length) === text;
5+
switch (ext) {
6+
case ".mp3":
7+
return at(0, "ID3") || (head[0] === 0xff && ((head[1] ?? 0) & 0xe6) === 0xe2);
8+
case ".wav":
9+
return at(0, "RIFF") && at(8, "WAVE");
10+
case ".webp":
11+
return at(0, "RIFF") && at(8, "WEBP");
12+
case ".png":
13+
return head.subarray(0, 4).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47]));
14+
case ".jpg":
15+
case ".jpeg":
16+
return head[0] === 0xff && head[1] === 0xd8 && head[2] === 0xff;
17+
case ".webm":
18+
return head.subarray(0, 4).equals(Buffer.from([0x1a, 0x45, 0xdf, 0xa3]));
19+
case ".mov":
20+
return ["ftyp", "moov", "mdat", "wide", "free"].some((box) => at(4, box));
21+
case ".mp4":
22+
case ".m4v":
23+
// An audio-only M4A/M4B brand is not a video the API stores as video/mp4.
24+
return at(4, "ftyp") && !at(8, "M4A ") && !at(8, "M4B ") && !at(8, "M4P ");
25+
default:
26+
return false;
27+
}
28+
}

‎packages/cli/src/commands/cloud/render.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ describe("cloud render --dry-run with heavy media", () => {
136136
`<!doctype html><html><body><div data-composition-id="main" data-width="1920" data-height="1080"><video src="clip.mp4"></video></div></body></html>`,
137137
"utf-8",
138138
);
139-
writeFileSync(join(dir, "clip.mp4"), Buffer.alloc(6 * 1024 * 1024, 1));
139+
const mp4Header = Buffer.concat([Buffer.from([0, 0, 0, 24]), Buffer.from("ftypisom")]);
140+
writeFileSync(
141+
join(dir, "clip.mp4"),
142+
Buffer.concat([mp4Header, Buffer.alloc(6 * 1024 * 1024 - mp4Header.length, 1)]),
143+
);
140144
const result = spawnSync(
141145
"bun",
142146
["run", cliEntry, "cloud", "render", dir, "--dry-run", "--json"],

0 commit comments

Comments
 (0)