ID3V2 Detect and Manipulate MP3 file format using C# with Idsharp Library

Long time ago I wrote an article Detect file type or mime type based on content. If you look at the XML file that contained magic signature of different file types, one of the type was MP3 files. And magic signature for MP3 files is that first 3 bytes of file content are ID3. The format of MP3 files is very well defined and explained at ID3.org. Although it is very well defined but it is not something very straight forward and not a job for faint heart to understand it.

For one of new development adventure I am tasked with digging little bit deep into file structure of MP3 files and find out some meta data. Things like Artist Name, Title, Track Number, Year Of Release etc. So I looked around the site and found reference to IDSharp library developed for .Net and is COM visible as well. So you can use it in any application that can invoke COM interfaces. The library is available on sourceforge. But there is no source code for the actual core library. It is only available as .Net assembly. I really needed source code for it because I was curious about implementation and I had to make some modifications as well for the kind of work I was doing.

So I fired up good old tool Reflector and dis-assembled the assembly to generate the source code for it. I had to fix few compile errors after disassembly. But finally I made it to work as expected. So I thought I will share the source code of IdSharp library with people who are interested in it.

Download Source Code For IdSharp Library

From the following code snippet you can see how you can load a MP3 file to get all information about the file. And then you can actually change ID3 tag information as well and save the file with new information. In the sample code, I changed the name of the artist as well as album name. You can see from the screen shot that it did work.

static void Main(string[] args)
{
var id3v2 = ID3v2Helper.CreateID3v2("Hello.mp3");
Console.WriteLine("Artist: {0}", id3v2.Artist);
Console.WriteLine("Title: {0}", id3v2.Title);
Console.WriteLine("Album: {0}", id3v2.Album);
Console.WriteLine("Genre: {0}", id3v2.Genre);
Console.WriteLine("Year: {0}", id3v2.Year);
Console.WriteLine("TrackNumber: {0}", id3v2.TrackNumber);
Console.WriteLine("Media Type: {0}", id3v2.MediaType);
Console.WriteLine("CD Identifier: {0}", id3v2.MusicCDIdentifier.TOC);
id3v2.Artist = "Byteblocks.com";
id3v2.Title = "ID3v2 Song";
id3v2.Save("Bytes.mp3");
}

comments powered by Disqus

Blog Tags