mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
* 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
29 lines
1.2 KiB
TypeScript
29 lines
1.2 KiB
TypeScript
/** Whether a file's first bytes are the container its extension claims, for the types the asset API takes as-is. */
|
|
export function bytesMatchExtension(ext: string, head: Buffer): boolean {
|
|
const at = (offset: number, text: string) =>
|
|
head.toString("latin1", offset, offset + text.length) === text;
|
|
switch (ext) {
|
|
case ".mp3":
|
|
return at(0, "ID3") || (head[0] === 0xff && ((head[1] ?? 0) & 0xe6) === 0xe2);
|
|
case ".wav":
|
|
return at(0, "RIFF") && at(8, "WAVE");
|
|
case ".webp":
|
|
return at(0, "RIFF") && at(8, "WEBP");
|
|
case ".png":
|
|
return head.subarray(0, 4).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47]));
|
|
case ".jpg":
|
|
case ".jpeg":
|
|
return head[0] === 0xff && head[1] === 0xd8 && head[2] === 0xff;
|
|
case ".webm":
|
|
return head.subarray(0, 4).equals(Buffer.from([0x1a, 0x45, 0xdf, 0xa3]));
|
|
case ".mov":
|
|
return ["ftyp", "moov", "mdat", "wide", "free"].some((box) => at(4, box));
|
|
case ".mp4":
|
|
case ".m4v":
|
|
// An audio-only M4A/M4B brand is not a video the API stores as video/mp4.
|
|
return at(4, "ftyp") && !at(8, "M4A ") && !at(8, "M4B ") && !at(8, "M4P ");
|
|
default:
|
|
return false;
|
|
}
|
|
}
|