最后我找到了解决方案。使用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;
}