Skip to content

Skip leading ID3v2 data in MP3 files to allow processing #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions MetadataExtractor/Formats/Mpeg/Mp3Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,24 @@ public sealed class Mp3Reader
{
// http://id3.org/mp3Frame
// https://www.loc.gov/preservation/digital/formats/fdd/fdd000105.shtml
// https://id3.org/id3v2.4.0-structure

public Directory Extract(SequentialReader reader)
{
var directory = new Mp3Directory();

var header = reader.GetInt32();

// If the file starts with ID3v2 data, try to skip over it for now.
// Eventually we should extract this data properly.
if ((header & 0xFFFFFF00) == 0x49443300) // "ID3"
{
// Adjust start to end of header
var id3Bytes = reader.GetBytes(6);
reader.Skip(id3Bytes[2] * 0x200000 + id3Bytes[3] * 0x4000 + id3Bytes[4] * 0x80 + id3Bytes[5]);
header = reader.GetInt32();
}

// ID: MPEG-2.5, MPEG-2, or MPEG-1
int id = 0;
switch ((header & 0x000180000) >> 19)
Expand Down Expand Up @@ -49,8 +60,7 @@ public Directory Extract(SequentialReader reader)
break;
}


int protectionBit = (header & 0x00010000) >> 16;
// int protectionBit = (header & 0x00010000) >> 16;

// Bitrate: depends on ID and Layer
int bitrate = (header & 0x0000F000) >> 12;
Expand All @@ -75,8 +85,7 @@ public Directory Extract(SequentialReader reader)
frequency = frequencyMapping[1, frequency];
}


int paddingBit = (header & 0x00000200) >> 9;
// int paddingBit = (header & 0x00000200) >> 9;

// Encoding type: Stereo, Joint Stereo, Dual Channel, or Mono
int mode = (header & 0x000000C0) >> 6;
Expand Down
8 changes: 8 additions & 0 deletions MetadataExtractor/Formats/Mpeg/MpegAudioTypeChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@ 2 Layer description
11 - Layer I

Additional bits contain more information, but are not required for file type identification.

MP3 with ID3V2-Tags https://id3.org/id3v2.4.0-structure
MP3-File with ID3V2-Tagging at start
*/

public int ByteCount => 3;

public Util.FileType CheckType(byte[] bytes)
{

// MP3-File with "ID3" at start
if (bytes[0] == 0x49 && bytes[1] == 0x44 && bytes[2] == 0x33)
return Util.FileType.Mp3;
Comment on lines +36 to +38
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think other types of file can start with ID3 too, though this is an improvement over what we had before.


// MPEG audio requires the first 11 bits to be set
if (bytes[0] != 0xFF || (bytes[1] & 0xE0) != 0xE0)
return Util.FileType.Unknown;
Expand Down