tryFileTypeFromHeader

April 23, 2008 in Mac OS X by chip

This is a fallback if all other methods fail (e.g., extension, filetype, etc.)


function tryFileTypeFromHeader tPathAndFile
    open file tPathAndFile for binary read
    read from file tPathAndFile for 256
    put it into tHeader
    close file tPathAndFile
    put empty into tType
    if "H.264" is in tHeader then put "H.264 movie" into tType
    if char 1 to 3 of tHeader = "GIF" then put "GIF image" into tType
    if char 1 to 3 of tHeader = "FLV" then put "FLV movie" into tType
    if char 1 to 3 of tHeader = "MP3" then put "MP3 audio" into tType
    if char 1 to 3 of tHeader = "ID3" then put "MP3 audio" into tType
    if "JFIF" is in tHeader then put "JPEG/JFIF image" into tType
    if "IMG" is in tHeader and "JPEG" is in tHeader then put "JPEG image" into tType
    if "wide" is in tHeader  and "mdat" is in tHeader then put "QT movie" into tType
    if "mp4" is in tHeader  and "mdat" is in tHeader then put "mp4" into tType
    if "mjpg" is in tHeader and "JPEG" is in tHeader then put "mjpg movie" into tType
    if "ftypmp4" is in tHeader then put "mp4 movie" into tType
    if "HDV" is in tHeader then put "HDV movie" into tType
    if "ftypqt" is in tHeader then put "QT movie" into tType
    if "M4V" is in tHeader then put "m4v movie" into tType
    if "AIFF" is in tHeader then put "AIFF audio" into tType
    if "AIFC" is in tHeader and "sowt" is in tHeader then put "AIFC audio" into tType
    if "WAVE" is in tHeader then put "WAVE audio" into tType
    return tType
end tryFileTypeFromHeader