Detects the media format of a file from its magic bytes (file signature).
Returns a lowercase format string (e.g. 'webp', 'mp3') or null if the signature is not recognised. Requires at least 12 bytes.
'webp'
'mp3'
null
Use this to validate that a file's actual content matches its extension, or to detect the format when the extension is missing or untrusted.
Supported: jpeg, png, gif, webp, avif, mp3, wav, flac, ogg, aac
const buffer = await file.arrayBuffer();const format = detectFormat(buffer); // e.g. 'webp' Copy
const buffer = await file.arrayBuffer();const format = detectFormat(buffer); // e.g. 'webp'
Detects the media format of a file from its magic bytes (file signature).
Returns a lowercase format string (e.g.
'webp','mp3') ornullif the signature is not recognised. Requires at least 12 bytes.Use this to validate that a file's actual content matches its extension, or to detect the format when the extension is missing or untrusted.
Supported: jpeg, png, gif, webp, avif, mp3, wav, flac, ogg, aac