最後我找到了解決方案。使用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,對我來說也很有趣,但它的工作:) –