This project has moved and is read-only. For the latest updates, please go here.

Joining MP3 files

Jan 28, 2012 at 11:19 AM

Hi,

I am basically trying to write a small utility to join MP3 files. I found some samples on Stackoverflow showing how to do it using NAudio:

http://stackoverflow.com/questions/1160888/how-do-i-merge-join-mp3-files-with-c

This is the code from my application doing the job:

var path = Path.Combine(Path.GetDirectoryName(Files[0]) ?? "", "joined.mp3");
using (var output = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
{
	foreach (string file in Files)
	{
		using (var reader = new Mp3FileReader(file))
		{
			if ((output.Position == 0) && (reader.Id3v2Tag != null))
			{
				output.Write(reader.Id3v2Tag.RawData, 0, reader.Id3v2Tag.RawData.Length);
			}
			Mp3Frame frame;
			while ((frame = reader.ReadNextFrame()) != null)
			{
				output.Write(frame.RawData, 0, frame.RawData.Length);
			}
		}
	}
}

It runs fine and I get a file with the expected file name and size, and tags taken from the first file. However when I play the joined file in Windows Media Player, the length seems to be roughly doubled (a littlebit less than double actually). If I jump to a position in the second half it just stops and jumps back to start. Up until then the file plays just fine. I tried a lot of different files and it seems to apply to all, so I don't believe this is related to the specific files.

I found this discussion leading me to think it might be related to CBR vs. VBR:
http://naudio.codeplex.com/discussions/76928
However I don't know if this still applies since the discussion on Stackoverflow is from 2010.

Best regards,

Hendrik

Jan 28, 2012 at 11:27 AM

can you actually hear the whole file (i.e. both parts you concatenated)?

Windows Media Player is probably using the the Xing header that appears at the start of many VBR files to work out duration. However, when you reposition, it realises it has calculated the total length wrong. The answer is to either strip out xing headers, or fix the one on the first file.

Jan 28, 2012 at 12:52 PM

Hi Mark,

Thanks for the swift reply. Yes, I am able to hear both songs if I merge them. I noticed Windows Explorer gets the length right, while Media Player doesn't. I merged two songs with lengths of 4:01 and 4:29. Windows Explorer reports the length to be 8:31. Media Player playlist reports the length to be 16:30, but stops after 8:31.

It might be the Xing header as suggested confusing Media Player. Is there a way handle Xing headers in NAudio; preferrably by editing it, alternatively by skipping it. Will it for example be a single Mp3Frame, that I could just skip?

Jan 28, 2012 at 2:38 PM

I did some changes to try to find if there were any Xing header in the source files: According to the article in the link bytes on index 4 thorugh 7 of the raw frame data would then be either "Xing" or "Info" if decoded as ASCII. In that case I would just skip the entire frame since it should be an empty audio frame. I wasn't able to find that in any of the files, but all the sudden the length seems to be correct in Media Player (8:32 is close enough). Don't know what happened, maybe it was a caching problem inside Media Player. Will report back if the problem shows up again.

Jan 28, 2012 at 3:17 PM

it might be that in the absence of a xing header, Windows Media Player guesses that this is a CBR file and estimates a duration based on the bitrate of the first frame.

Feb 27, 2014 at 2:52 AM
markheath wrote:
can you actually hear the whole file (i.e. both parts you concatenated)? Windows Media Player is probably using the the Xing header that appears at the start of many VBR files to work out duration. However, when you reposition, it realises it has calculated the total length wrong. The answer is to either strip out xing headers, or fix the one on the first file.
I am having some of the same issues. I have concatenated the files together, and I have added an essentially blank XING header to the file. With the blank XING header, Windows Media player reports the wrong length of file, and there are some other oddities. My question is, how can I recreate a proper XING header? I have confused myself greatly by reading the documentation on various sites, and I'm hoping there is a nice OO means to recreate the XING header and rebuild the toc in said header to properly display playback percentage and file length.
Feb 27, 2014 at 10:10 PM
concatenating MP3s by simply putting the frames one after the other is a hack that sort of works, but as you have discovered, doesn't really work for VBR files. I'd recommend converting to PCM, concatenating and then re-encoding to MP3 for the best results.
Feb 27, 2014 at 10:15 PM
It should work fine but you would need to create a new Xing header with the correct audio length etc..