2013-09-16 93 views
1

我只是用下面的代碼C#:覆蓋MP3文件到另一個

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

我想是疊加另一個MP3文件,該文件將在這一背景新近發揮合併3個MP3文件轉換成一個MP3文件創建的MP3文件。我知道它有可能,但沒有正確的方法去實現這一目標。任何幫助都會很棒。 感謝

+0

我會感到驚訝,如果合併的MP3文件這樣的實際作品。你有沒有嘗試播放結果文件? – Surfbutler

+0

谷歌搜索的第二個鏈接[overlay mp3 c#](http://stackoverflow.com/questions/9604165/expression-encoder-sdk-how-to-add-audio-track-on-a-video)。 – Sayse

+0

@Surfbutler,對我來說也很有趣,但它的工作:) –

回答

1

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

在「覆蓋」的問題:

沒有這樣做沒有原始解碼爲PCM,在您的覆蓋混合的方式,然後重新編碼整個事情到MP3。

在現有代碼:

你可以只是僥倖串聯MP3文件這樣的。儘管我會推薦放棄ID3標籤,並且只是從每個文件製作一個具有MP3幀的文件。如果您使用NAudio,則可以使用Mp3FileReader上的ReadNextFrame()方法獲取每個MP3幀並將其RawBytes寫入文件。

爲了獲得最佳效果,您希望所有MP3文件使用相同的採樣率和通道數。另外,如果這些是VBR,您將會使XING or VBRI headers中的信息失效,所以甚至可以最好拋棄這些信息。

+0

感謝馬克,我現在已經轉移到NAudio合併兩個MP3文件。我想知道任何覆蓋另一個mp3文件的例子,因爲我從來沒有解碼和重新編碼。 –