checking file types

Status
Not open for further replies.
Originally posted by petrov:
if you want to check file _types_ and not just file names, than you might want to look into the file(1) command which uses a complicated heuristic (involving true magic) to determine the type of the file regardless of the name.
Except it's not all that magic, as this file demonstrates. Because certain files -- including MP3s -- don't actually have headers, or indeed any genuine identifying marks -- the only way to actually know what a file is is to use a priori knowledge. You can't work it out from the file itself. MPEG audio files, for instance, have nothing more than a byte and a half of set bits as their identifier. This is what the MPEG audio decoder uses to locate data frames, and it represents the only fixed part of an MPEG audio file's structure. Even this isn't perfect, as so-called "MPEG 2.5" files only have a byte and three eights of set bits.
 

knoxy

Wise, Aged Ars Veteran
188
Because certain files -- including MP3s -- don't actually have headers, or indeed any genuine identifying marks -- the only way to actually know what a file is is to use a priori knowledge. You can't work it out from the file itself. MPEG audio files, for instance, have nothing more than a byte and a half of set bits as their identifier. This is what the MPEG audio decoder uses to locate data frames, and it represents the only fixed part of an MPEG audio file's structure. Even this isn't perfect, as so-called "MPEG 2.5" files only have a byte and three eights of set bits.

I'm actually looking for mp3s. I can list the files in a directory, I'm just looking for an efficient way of determining if they're mp3s. I was just going to parse the filename to see if it has an mp3 extension but I get the feeling there's a better way.

Any pointers on identifying mp3s?
 
You can't.

It's that simple.

Your best bet is to read the file extension.

There is no sequence of bytes that identifies a file as being an MP3 with any high degree of accuracy.

The byte and a half of set bits -- which is what MPEG audio frame decoders look for -- can be located anywhere within the file (ID3v2 tags, for instance, tend to prepend the MPEG audio frames, though they can be interleaved). And they are by no means a surefire way of determining the filetype -- as my linked file demonstrates.

If you insisted on doing it this way, your best bet would probably be to take an approach like this.

One: Read the first few bytes of the file. If the first few bytes are MPEG sync bits procede to step three. If the first few bytes are an ID3v2 header, precede to step two. Else it's not an MPEG audio file.
Two: Seek ahead the length of the ID3v2 information (IIRC the total size is stored in the header), and locate the MPEG sync bits. If they're not found, it's not an MPEG audio file. Beware, some ID3v2 tags out there seem not to be syncsafe, and so it's possible to misdetect them as being MPEG audio frame sync bits.
Three: Read the frame header and determine the frame's length.
Four: Seek ahead by that amount and see if you find another MPEG audio frame. If you do, the file is an MPEG audio file. If you don't, it probably isn't (although it might be, as they're tolerant of corruption and can have other data interleaved without harming the audio).
 

petrov

Ars Tribunus Militum
2,714
Originally posted by PeterB:
Except it's not all that magic, as http://www.anti-flash.co.uk/downloads/unicode.txt demonstrates. Because certain files -- including MP3s -- don't actually have headers, or indeed any genuine identifying marks -- the only way to actually know what a file is is to use a priori knowledge.

hence the part about the heuristic.

--sam
 
hence the part about the heuristic.
Which will be error-prone. As that text file demonstrates. If I padded the text file to the right length, it would be a valid MPEG audio frame. There would be no way -- short of making a person listen to it -- to tell otherwise. The heuristic cannot tell, unless it's going to make value judgements on a piece of decoded audio.
 
Status
Not open for further replies.