2013-09-16 92 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文件的例子,因为我从来没有解码和重新编码。 –