HEIC and MP4 share the same ISO Base Media File Format container — both start with ftyp. mime-bytes matches it to video/mp4.
I can't attach a .heic or .heif file to this ticket because they're not supported by GitHub. Any photo from a recent iPhone will work.
import { detectFromBuffer } from 'mime-bytes'
const buf = readFileSync('/path/to/my.heif')
await mimeBytes.detectFromBuffer(buf)
// { name: 'mp4', ... confidence: 1 }
The problem seems to be that mp4, heic, and heif all share a common prefix and mp4 comes first. I can fix it by sorting fileTypes such that any strict prefixes come after:
import { FileTypeDetector } from 'mime-bytes'
const detector = new FileTypeDetector()
const isPrefix = (shorter: string[], longer: string[]) =>
shorter.length < longer.length && shorter.every((b, i) => b === longer[i])
detector.fileTypes.sort((a, b) =>
isPrefix(a.magicBytes, b.magicBytes) ? 1
: isPrefix(b.magicBytes, a.magicBytes) ? -1
: 0
)
await detector.detectFromBuffer(buf)
// { name: 'heic', ..., confidence: 1 }
Sorting by magicBytes.length descending is sufficient to fix the bug. I assumed there's some reason to pick the order you did, so I kept it as stable as possible.
HEIC and MP4 share the same ISO Base Media File Format container — both start with ftyp. mime-bytes matches it to video/mp4.
I can't attach a
.heicor.heiffile to this ticket because they're not supported by GitHub. Any photo from a recent iPhone will work.The problem seems to be that
mp4,heic, andheifall share a common prefix andmp4comes first. I can fix it by sortingfileTypessuch that any strict prefixes come after:Sorting by
magicBytes.lengthdescending is sufficient to fix the bug. I assumed there's some reason to pick the order you did, so I kept it as stable as possible.