MP3This is a page for MP3 file analysis (Header and for (ID3 ). Try sample file: Try an example (MP3 without ID3 header)Try an example (with ID3 header MP3)
A MP3 file has a well defined header. |
File contents analysis. |
Sample code
public string getMp3(System.IO.Stream file) { string TextBox1 = ""; // AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM byte[] b = ReadFully(file, file.Length); long val = b[0] << 24 + b[1] << 16 + b[2] << 8 + b[3]; if (b[0] == 0xff) { TextBox1 = "<pre>Format is: AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM<br/>"; TextBox1 += "Binary is: " + Global.intToBinary(b[0]) + " " + Global.intToBinary(b[1]) + " " + Global.intToBinary(b[2]) + " " + Global.intToBinary(b[3]) + "<br/></pre>"; TextBox1 += "-- AAAAAAAA AAA -Frame sync (all bits set)<br/>"; TextBox1 += "-- BB - MPEG Audio version ID (eg 00 - MPEG Version 2.5)<br/>"; TextBox1 += "-- C - Layer description (eg 01 - Layer III)<br/>"; TextBox1 += "-- D - Protection Bit (eg 1 - No protection)<br/>"; TextBox1 += "-- EEEE - Bit rate (eg 0110 - 192kbps)<br/>"; string vala = Global.intToBinary(b[0]) + Global.intToBinary(b[1] & 0xe0).Substring(0, 3); TextBox1 += "<br/>Frame sync: " + vala + "<br/>"; // A A A B B C C D b[1] TextBox1 += "MPEG Audio Version: " + Global.intToBinary((b[1] & 0x18)).Substring(3, 2) + "<br/>"; TextBox1 += "Layer description: " + Global.intToBinary((b[1] & 0x6)).Substring(5, 2) + "<br/>"; TextBox1 += "Protection Bit: " + Global.intToBinary((b[1] & 0x1)).Substring(7, 1) + "<br/>"; // EEEEFFGH TextBox1 += "Bit rate: " + Global.intToBinary((b[1] & 0xf0)).Substring(0, 4) + "<br/>"; } else if ((char)b[0] == 'I' && (char)b[1] == 'D') { // Then comes two bytes that encode the major version and revision of the ID3 specification to which the // tag purports to conform. They're followed by a single byte whose individual bits are treated as flags. TextBox1 = "<h3>ID Header</h3>"; TextBox1 += "<p>Bytes 1-3, ID3 Header: " + (char)b[0] + (char)b[1] + (char)b[2] + "<BR/>"; TextBox1 += "Byte 4, Version (Major): " + b[3] + "<BR/>"; TextBox1 += "Byte 5, Version (Minor): " + b[4] + "<BR/>"; TextBox1 += "Byte 6, Version (Flags): " + b[5] + "<BR/>"; TextBox1 += "Bytes 7-10, Size: " + b[6] + b[7] + b[8] + b[9] + "<BR/>"; int found = 0; for (int i = 0; i < 20; i++) { if (b[i] == 'T' && b[i + 1] == 'T') { found = i; break; } } TextBox1 += "<h3>ID Frame</h3>"; string title = ""; TextBox1 += "<p>Bytes 1-3, ID3 Tag (should be TT2): " + (char)b[found] + (char)b[found + 1] + (char)b[found + 2] + "<BR/>"; for (int i = 0; i < 200; i++) { if ((char)b[i] == 'T' && (char)b[i + 1] == 'A' && (char)b[i + 2] == 'L' && (char)b[i + 3] == 'B') { title = ""; for (int j = i + 4; j < i + 34; j++) { if ((char)b[j] == 'T') break; else title += (char)b[j]; } title = title.Replace("\0", string.Empty); TextBox1 += "Tag TALB found, Title: " + title + "<BR/>"; ; } if ((char)b[i] == 'T' && (char)b[i + 1] == 'Y' && (char)b[i + 2] == 'E' && (char)b[i + 3] == 'R') { title = ""; for (int j = i + 4; j < i + 16; j++) { if ((char)b[j] == 'T') break; else title += (char)b[j]; } title = title.Replace("\0", string.Empty); TextBox1 += "Tag TYER found, Year: " + title + "<BR/>"; ; } } /* header 3 "TAG" title 30 30 characters of the title artist 30 30 characters of the artist name album 30 30 characters of the album name year 4 A four-digit year comment 28[3] or 30 The comment. zero-byte[3] 1 If a track number is stored, this byte contains a binary 0. track[3] 1 The number of the track on the album, or 0. Invalid, if previous byte is not a binary 0. genre 1 Index in a list of genres, or 255 */ } else { TextBox1 = "This is not an MP3 file"; } return (TextBox1); }