3
votes

Hi I want to detect file types in my application. libmagic is a option to do that. But i am not intend to string compare, for example

a mp3 file output using libmagic : Audio file with ID3 version 2.3.0, contains: MPEG ADTS, layer III, v1, 128 kbps, 44.1 kHz, JntStereo

is there any other libraries for True File Type Detection ?

Any help would be appreciated.

2
Do you need some kind of structure as result of detection (mime-type, default extension, details, etc)? - Serge S.
i wanna only numbers if it is available. - iyasar
I presume when you say numbers, you want some kind of int/enumeration which you can compare with a constant? - Benj
such as for mp3 files = 0x765 or and a .iso files = 0x879 - iyasar
Do you only need specific file types such as mp3 files? - ThiefMaster

2 Answers

3
votes

You could try to implement this by own using File Signature Table

Example:

Hex Signature        File Extension           ASCII Signature, File Description

49 44 33                        MP3           ID3
                                              MPEG-1 Audio Layer 3 (MP3) audio file

43 44 30 30 31                  ISO           CD001
                                              ISO-9660 CD Disc Image
                                              This signature usually ...
1
votes

You incorrectly presume that the set of filetypes is enumerable. That's just not how files work. Your example about .mp3 files (audio) naturally leads to video. What is the relation between .AVI, .MKV, MPEG1, MPEG2 and MPEG4 video? You can't capture those relations in a single number.

Similarly, is a CD .ISO the same filetype as a DVD .ISO? Either you assign the same number of you don't. And I specifically wrote "you" in the previous sentence because the answer depends on whether you consider them the same.