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

4

2 回答 2

1

关于“叠加”的问题:

如果不将原始文件解码为 PCM,在叠加层中混合,然后将整个内容重新编码为 MP3,就无法做到这一点。

在您现有的代码上:

您可以像这样连接 MP3 文件。通常虽然我会建议放弃 ID3 标签,只制作一个包含每个文件的 MP3 帧的文件。如果您使用的是 NAudio,那么您可以使用ReadNextFrame()Mp3FileReader 上的方法来获取每个 MP3 帧并将其写入RawBytes文件。

为获得最佳效果,您希望所有 MP3 文件使用相同的采样率和通道数。此外,如果这些是 VBR,您将使XING 或 VBRI 标头中的信息无效,因此最好也放弃这些信息。

于 2013-09-16T14:20:18.067 回答
1

最后我找到了解决方案。使用NAudio我们可以混合 wav 流,因此首先将 mp3 转换为 wav,然后混合 wav 文件,然后使用lame.exe.

感谢 Mark Heath,可以使用以下代码使用 NAudio 库将 MP3 转换为 WAV。

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

现在可以使用此代码使用 NAudio 类将其与另一个 wav 文件混合。

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

mixWAVFiles方法_

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);
        }

现在终于使用 lame.exe 将 finalwav 文件转换为 mp3 在这里找到

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;
}
于 2013-09-18T20:53:52.487 回答