headermp3detectdecodingflac

Detect if a file is an MP3 file?


I'm writing a C++ library for decoding and encoding audio between different formats/codecs. I have a routine for quickly detecting the format before loading the required codec library.

For WAV files one can simple look for the ASCII values "RIFF" and "WAVE" at the start of the file. The same applies to FLAC, we can simply read in the first 4 bytes, which will be "fLaC".

But how can I quickly detect if a file is MP3? I can't rely on the file extension. I also can't try to decode the first MP3 frame, since there might be additional data at the start of the file (eg: ID3, cover image, etc).


Solution

  • Detecting if a file is an MP3 is more complicated than searching for a fixed pattern in the file.

    Some concepts

    (See http://www.codeproject.com/Articles/8295/MPEG-Audio-Frame-Header for details)

    Algorithm to determine if a file is MP3 or not