Skip to content

mime-bytes identifies HEIF image as video/mp4 #967

@jamesarosen

Description

@jamesarosen

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions