How do I merge/join MP3 files with C#?

11,918

Solution 1

MP3 files consist of "frames", that each represent a short snippet (I think around 25 ms) of audio.

So yes, you can just concatenate them without a problem.

Solution 2

here's how you can concatenate MP3 files using NAudio:

public static void Combine(string[] inputFiles, Stream output)
{
    foreach (string file in inputFiles)
    {
        Mp3FileReader 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);
        }
    }
}

see here for more info

Share:
11,918
James
Author by

James

A C# developer working for a bank. Don't think I could live without this website!!!

Updated on June 05, 2022

Comments

  • James
    James almost 2 years

    I have a library of different words/phrases, and in order to build sentences at present I add a combination of these phrases into a playlist to make a sentence. Unfortunately if the user is running CPU intensive applications (which most of my users are) there can be a lag of a few seconds mid-sentence (in between phrases).

    In order to combat this I was thinking of an approach which will merge the right combination of MP3 files on the fly into an appropriate phrase, save this in the %temp% directory, and then play this 1 MP3 file which should overcome the problem I'm experiencing with gaps.

    What is the easiest way to do this in C#? Is there an easy way to do this? The files are fairly small, 3-4 seconds long each, and a sentence can consist of 3-20ish phrases.

  • James
    James almost 15 years
    I will mostly have control over which mp3 files are used, would there ever be a problem if different bit rates were used?
  • bart
    bart almost 15 years
    No problem if your audio player can handle VBR files, because that's exactly how VBR works.
  • Brad
    Brad almost 13 years
    This is completely incorrect. You cannot simply stick frames up against each other, due to the bit reservoir. See section 4 of this: lame.sourceforge.net/tech-FAQ.txt
  • Brad
    Brad almost 13 years
    This isn't correct. You can split/merge an MP3 stream, but it needs to be done very carefully due to the bit reservoir. See lame.sourceforge.net/tech-FAQ.txt
  • MonsterMMORPG
    MonsterMMORPG about 9 years
    where is the rest mark ? how do you save stream output as a new mp3 file ? tell me please ty how do you call this Combine function where is the before ?
  • NoOne
    NoOne over 8 years
    @Brad So, if I understand correctly (without reading the whole article you posted), it is perfectly fine to join existing MP3s (since they should be still able to find the previous frames - in my logic at least), just it is not OK to mix and match their frames (which are the building blocks of the complete MP3s). E.g. it is fine to stick the one after the other, but not fine to insert the one in the middle of the other. Am I getting it right? But the title of section 4 seems to imply (under my bad understanding of english) that even joining 2 whole MP3s is not OK. :(
  • Brad
    Brad over 8 years
    You are correct. As long as the two streams of MP3 data are of the same channel count and sample rate, then you can concatenate then together. They must also contain no ID3 tags or other add-ons. Section 4 is simply stating that the original streams to be spliced cannot be due to how they were cut from their own bit reservoir data. If your streams don't have this problem (because they are complete or the bit reservoir is disabled) then you can concatenate no problem.
  • Niklas Arbin
    Niklas Arbin about 8 years
    Concatinating the mp3 files would produce some wierd output considering that each file might have headers and tags
  • Ambrose Leung
    Ambrose Leung almost 6 years
    Warning: concatenating mp3 files does not play in Google Chrome HTML5 player - it plays fine in other media players though
  • Ambrose Leung
    Ambrose Leung almost 6 years
    @MonsterMMORPG save stream to file : stackoverflow.com/questions/411592/… For those of you using the Mp3FileReader(stream) constructor, make sure you set stream.Position = 0; before calling the constructor