Detection of valid audio files

David Place David.Place at
Fri Jun 3 18:18:35 CEST 2011

Hi, one of the requirements we have here is to detect if an audio file
is valid or not. E.g: if an EXE file was renamed to .MP3, we need to
know that it's bad! I've tested this on several of the supported file
formats and the behaviour of TagLib seems to vary. Sometimes
FileRef.IsNull() is true, sometimes I have to check the Tag() pointer
and even sometimes I need to check the audioProperties() pointer.
However, with MP3 files, the results from a valid MP3 file without any
tags (ID3v1, ID3v2 & APE) and an invalid MP3 file are the same so I
can't detect if it's truely a valid MP3 file or not. Even the
audioProperties() gives me results.
I have found a way around this by a small modification to 'mpegfile.cpp'
in MPEG::File::read:
  //if no tag was found at all, make sure this is a valid MP3 file by
checking the MP3 header

  if (!d->hasID3v2 && ! d->hasID3v1 && !d->hasAPE) {

       //goto the beginning of the file and read the header


       MPEG::Header header(readBlock(4));



So after the tags have tried to be read, if no tags could be read, check
the MPEG header. This seems ok to me providing the first 4 bytes of the
file would ALWAYS be the start of an MPEG header for valid MP3 file.
Is this approach any good?
-------------- next part --------------
An HTML attachment was scrubbed...

More information about the taglib-devel mailing list