最后我找到了解决方案。使用NAudio
,我们可以混合wav流,首先将mp3转换为wav,然后混合wav文件,然后使用lame.exe
将所得wav文件转换为mp3。
感谢Mark Heath,将MP3转换为WAV可以使用以下代码片段使用NAudio库执行。
string file = "new.mp3";
Mp3FileReader readers = new Mp3FileReader(file);
WaveFormat targetFormat = new WaveFormat();
WaveStream convertedStream = new WaveFormatConversionStream(targetFormat, readers);
WaveFileWriter.CreateWaveFile("firstwav.wav", convertedStream);
现在将它与另一个wav文件混合使用此代码可以使用NAudio类。
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);
}
现在终于使用finalwav文件转换为MP3 lame.exe发现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;
}
我会感到惊讶,如果合并的MP3文件这样的实际作品。你有没有尝试播放结果文件? – Surfbutler
谷歌搜索的第二个链接[overlay mp3 c#](http://stackoverflow.com/questions/9604165/expression-encoder-sdk-how-to-add-audio-track-on-a-video)。 – Sayse
@Surfbutler,对我来说也很有趣,但它的工作:) –