1

I have merged 3 mp3 files into one mp3 file simply using the following code

using (var fs = File.OpenWrite(Path.Combine(txtOutputFile.Text, "new.mp3")))
            {
                var buffer = File.ReadAllBytes(Path.Combine(txtIntroLoc.Text, fileName1));
                fs.Write(buffer, 0, buffer.Length);
                buffer = File.ReadAllBytes(Path.Combine(txtOutroloc.Text, fileName2));
                fs.Write(buffer, 0, buffer.Length);
                buffer = File.ReadAllBytes(Path.Combine(txtFileThree.Text, fileName3));
                fs.Write(buffer, 0, buffer.Length);
                fs.Flush();
            }

What i want is to overlay another mp3 file which will play in the background of this newly created mp3 file. I know its possible but not getting right way to go to acheive this. Any help will be great. thanks

Jibran Khan
  • 3,236
  • 4
  • 37
  • 50
  • I'd be surprised if merging MP3 files like this actually works. Have you tried playing the resulting file? – Surfbutler Sep 16 '13 at 12:19
  • Second link from googling [overlay mp3 c#](http://stackoverflow.com/questions/9604165/expression-encoder-sdk-how-to-add-audio-track-on-a-video). – Sayse Sep 16 '13 at 12:20
  • @Surfbutler, Well its funny to me as well, but its working :) – Jibran Khan Sep 16 '13 at 12:22
  • Ok, well great then :) I imagine overlaying another track would be a lot harder, as it would involve sampling both original and new, then crating a new combined file. I'm guessing there's an MP3 api out there somewhere that might help. – Surfbutler Sep 16 '13 at 12:22
  • If you're looking at overlaying the files, check `FFMpegConverter` out. All you need to do is install the NuGet package (`NReco.VideoConverter`) and call `.Invoke("{arguments found in link}");`. Link to commands: http://stackoverflow.com/a/11783474 – Micah Vertal Jul 09 '16 at 03:41

2 Answers2

1

On the question of "overlaying":

There is no way of doing this without decoding the original to PCM, mixing in your overlay, and then re-encoding the whole thing to MP3.

On your existing code:

You can just about get away with concatenating MP3 files like this. Usually though I would at recommend ditching the ID3 tags, and just making one file that has the MP3 frames from each file. If you are using NAudio, then you can use the ReadNextFrame() method on Mp3FileReader to get each MP3 frame and write its RawBytes out to the file.

For best results, you'd want all the MP3 files to use the same sample rate and channel count. Also, if these are VBR, you'll be invalidating the information in the XING or VBRI headers, so it might even be best to ditch those as well.

Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • Thanks Mark, i have now shifted to NAudio to merge the two mp3 files. I want to know any example on overlaying a mp3 file over another as i have never worked with decoding and re encoding. – Jibran Khan Sep 17 '13 at 18:47
1

Finally i found a solution. Using NAudio we can mix the wav stream so first converting the mp3 to wav and then mixing the wav files and then re convert the resulted wav file to mp3 using the lame.exe.

Convert MP3 to WAV can be performed using the following piece of code using NAudio library thanks to Mark Heath.

string file = "new.mp3";
            Mp3FileReader readers = new Mp3FileReader(file);
            WaveFormat targetFormat = new WaveFormat();
            WaveStream convertedStream = new WaveFormatConversionStream(targetFormat, readers);
            WaveFileWriter.CreateWaveFile("firstwav.wav", convertedStream);

Now mixing it with another wav file can be performed using this code consuming the NAudio classes.

string[] inputFiles = new string[2];
            Stream output = new MemoryStream();
            inputFiles[0] = "firstwav.wav";
            inputFiles[1] = "secondwav.wav";
mixWAVFiles(inputFiles);

The mixWAVFiles Method

public void mixWAVFiles(string[] inputFiles)
        {
            int count = inputFiles.GetLength(0);
            WaveMixerStream32 mixer = new WaveMixerStream32();
            WaveFileReader[] reader = new WaveFileReader[count];
            WaveChannel32[] channelSteam = new WaveChannel32[count];
            mixer.AutoStop = true;

            for (int i = 0; i < count; i++)
            {
                reader[i] = new WaveFileReader(inputFiles[i]);
                channelSteam[i] = new WaveChannel32(reader[i]);
                mixer.AddInputStream(channelSteam[i]);
            }
            mixer.Position = 0;
            WaveFileWriter.CreateWaveFile("mixedWavFile.wav", mixer);
        }

And now finally converting the finalwav file to mp3 using lame.exe found here

public void convertWAVtoMP3(string wavfile)
        {
            //string lameEXE = @"C:\Users\Jibran\Desktop\MP3 Merger\bin\Debug\lame.exe";
            string lameEXE = Path.GetDirectoryName(Application.ExecutablePath) +"/lame.exe";
            string lameArgs = "-V2";

            string wavFile = wavfile;
            string mp3File = "mixed.mp3";

            Process process = new Process();
            process.StartInfo = new ProcessStartInfo();
            process.StartInfo.FileName = lameEXE;
            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            process.StartInfo.Arguments = string.Format(
                "{0} {1} {2}",
                lameArgs,
                wavFile,
                mp3File);

            process.Start();
            process.WaitForExit();

            int exitCode = process.ExitCode;
}
Jibran Khan
  • 3,236
  • 4
  • 37
  • 50