C# – How to read MP3 header?

Today i am trying to write a small application to merge some mp3 files into one. Owing to the fact that I am listening to some audio books. You know a book is always categorized into many chapters and is very long. Therefore the audio version is splitted into many parts too. I don’t like that because i can easily lose one of them and when I hear them I must sort them into exact order. Because of the above points I would like to write a small tool to merge all files into one.

As I start my project I know it must be pretty complex because Mp3 format was invented long time ago and it has different versions since its first publishcation. So I made a search to MP3 file format specification and I found that it is badly documented and the website is not professionally designed. I thought I went into wrong link but after more searching I think that is may be the best one (???).  I started to read the documentation and found this interesting paragraph

An MPEG audio file is built up from smaller parts called frames. Generally, frames are independent items. Each frame has its own header and audio informations. As there is no file header, you can cut any part of MPEG file and play it correctly

Ah ha, I thought I can simply join two mp3 files and they will work like a charm.


FileStream fs0001 = File.Open("D:\\Downloads\\0001.mp3", FileMode.Append);
FileStream fs0002 = File.Open("D:\\Downloads\\0002.mp3", FileMode.Open);
byte[] baOut = new byte[fs0002.Length];
fs0002.Read(baOut, 0, (int)baOut.Length);
fs0001.Write(baOut, 0, (int)baOut.Length);
fs0001.Close();
fs0002.Close();

After running the code above, the output file can be played with WMP11. I thought I was successfull. But you know, life is not easy as we hope. Although WMP11 can play the whole output file but the information of file was wrong. For example I merged a 3-minutes length mp3 files with a 2-minutes length one but the duration of output was only 3-minutes.  That means it takes the duration of first file for the new one and it is of course wrong. So I manage to rewrite the duration of output file .I make another search for a library working with ID3 Tag. Writing an own one must be a big work. There are a lot of kinds of this library, I chose to use TagLib Sharp because I think this library will be continuously developed in future. Below is a small code snippet for reading ID3 Tag from a mp3 file.


TagLib.File fiMp3 = TagLib.File.Create("D:\\Downloads\\0001.mp3");
Console.WriteLine(fiMp3.Tag.Performers[0].ToString());
Console.WriteLine(fiMp3.Properties.Duration);

However until now I can still NOT to edit the duration of the output file. I am reading the mp3 documentation forward and hope that I can find a way.

UPDATE 17.03.2012
If you want to read or write the picture of album cover of the mp3 files you can use listing below

private static void Main(string[] args)
{
	TagLib.File file = TagLib.File.Create("UMTD.mp3");
	Console.WriteLine("Current picture count: " + file.Tag.Pictures.Length);

	file.Tag.Pictures = new Picture[] { new Picture("Pic.png") };
	file.Save();

	Console.WriteLine("New picture count: " + file.Tag.Pictures.Length);
	Console.ReadLine();
}

Source : http://hintdesk.com/Web/Source/How%20to%20read%20MP3%20header.zip

One thought on “C# – How to read MP3 header?”

Leave a Reply

Your email address will not be published. Required fields are marked *