Detection of valid audio files

David Place David.Place at tomtom.com
Thu Oct 6 14:09:37 UTC 2011


Attached is the patch of the change to mpegfile.cpp for improved validity detection of mp3 files.

Regards,
David Place

________________________________
From: David Place
Sent: 05 September 2011 10:43
To: taglib-devel at kde.org
Subject: RE: Detection of valid audio files

Hi all. I'm picking up this old thread I started a while ago because I've now done some more changes.
Since then we've discovered some MP3 files were detected as invalid after my previous patch.
This was because the MP3 files were inside a WAV container and didn't contain any tags. These additional changes fix that:

  //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
      seek(0);
      MPEG::Header header(readBlock(4));
      bool valid = header.isValid();

      //if still not valid then maybe the MP3 stream is in a WAV container?...
      if (!valid) {
            RIFF::WAV::File* wav = new RIFF::WAV::File(name(), readProperties, propertiesStyle);
            valid = wav->isValid();

            //now test to make sure this isn't a WAV file renamed to MP3...
            if (valid) {
                  //current position will be the beginning of the "data" chunk
                  long pos = wav->tell();

                  //we need to seek over the chunk id ("data") and the chunk size (4 bytes)
                  wav->seek(pos + 8);
                  MPEG::Header header(wav->readBlock(4));
                  valid = header.isValid();
            }

            delete wav;
      }

      setValid(valid);
  }

Does anyone know a better way of handling MP3 validity?

Regards,
David

________________________________
From: David Place [mailto:David.Place at tomtom.com]
Sent: 03 June 2011 17:19
To: taglib-devel at kde.org
Subject: Detection of valid audio files

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
       seek(0);
       MPEG::Header header(readBlock(4));
       setValid(header.isValid());
  }

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?

Thanks,
David

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/taglib-devel/attachments/20111006/895e8776/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch0.patch
Type: application/octet-stream
Size: 1565 bytes
Desc: patch0.patch
URL: <http://mail.kde.org/pipermail/taglib-devel/attachments/20111006/895e8776/attachment.obj>


More information about the taglib-devel mailing list