Skip to content

Commit c094d43

Browse files
committed
fix(cli): an audio-only MP4 named as video is re-encoded as audio
1 parent f525369 commit c094d43

2 files changed

Lines changed: 77 additions & 8 deletions

File tree

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,38 @@ describe("offloadHeavyMedia", () => {
372372
});
373373
});
374374

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+
375407
it("only takes bytes as-is when they are the container the extension names", () => {
376408
const wave = Buffer.from("RIFF\0\0\0\0WAVEfmt ");
377409
expect(bytesMatchExtension(".wav", wave)).toBe(true);

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

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ export interface Reencoder {
181181
choose: (sourcePath: string, kind: MediaKind) => Promise<PresetName | null>;
182182
/** Positions per second a cut may land on (the frame rate), or null when the file cannot be cut. */
183183
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>;
184186
run: (
185187
sourcePath: string,
186188
preset: PresetName,
@@ -451,7 +453,34 @@ async function trimGrid(sourcePath: string, kind: MediaKind): Promise<number | n
451453
return Number.isFinite(fps) && Math.abs(fps - rate(average)) < 0.01 ? fps : null;
452454
}
453455

454-
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+
};
455484

456485
async function stagedSha256(file: StagedFile): Promise<string> {
457486
if (Buffer.isBuffer(file)) return createHash("sha256").update(file).digest("hex");
@@ -550,23 +579,31 @@ async function planFile(
550579
path: string,
551580
file: StagedFile,
552581
): Promise<FilePlan | null> {
553-
const kind = mediaKind(path);
582+
let kind = mediaKind(path);
554583
const size = stagedSize(file);
555584
if (!kind || size < OFFLOAD_MIN_BYTES) return null;
556-
const accepted = ACCEPTED[extname(path).toLowerCase()];
585+
const ext = extname(path).toLowerCase();
586+
const accepted = ACCEPTED[ext];
557587
// The API reads the stored bytes, not the name: a file whose bytes differ from its extension is re-encoded.
558-
const asIs =
559-
accepted !== undefined &&
560-
size <= accepted.maxBytes &&
561-
bytesMatchExtension(extname(path).toLowerCase(), readHead(file));
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+
}
562599
const mayBeCached =
563600
ctx.opts.upload !== undefined || (asIs ? ctx.cachedSizes.has(size) : ctx.cachedSizes.size > 0);
564601
const sha256 = mayBeCached ? await stagedSha256(file) : undefined;
565602
const keyFor = (variant: string) => sha256 && `${ctx.opts.apiBaseUrl} ${sha256} ${variant}`;
566603
if (asIs) {
567604
return { key: keyFor("as-is"), sourcePath: "", contentType: accepted?.contentType };
568605
}
569-
const sourcePath = snapshotPath(path, file, ctx.tempFiles);
606+
sourcePath ??= snapshotPath(path, file, ctx.tempFiles);
570607
const preset = await ctx.reencoder.choose(sourcePath, kind);
571608
// Files left as authored stay in the zip; the archive size check names them.
572609
if (!preset) return null;

0 commit comments

Comments
 (0)