2016-08-30 271 views
0

我必须将spx音频文件(ogg格式)转换为mp3文件。我已经尝试了几件事,至今没有任何工作。将spx音频文件转换为mp3

我试过使用Naudio.Lame库中的LameMP3FileWriter。

private void WriteOggStreamToMp3File(Stream oggStream, string mp3FileName) 
{ 
    var format = new WaveFormat(8000, 1); 
    using (var mp3 = new LameMP3FileWriter(mp3FileName, format, LAMEPreset.ABR_128)) 
    { 
     oggStream.Position = 0; 
     oggStream.CopyTo(mp3); 
    } 
} 

由于输出的mp3文件只是静态噪声,所以效果不佳。

我也发现从NSpeex CodePlex上页(https://nspeex.codeplex.com/discussions/359730)此示例:

private void WriteOggStreamToMp3File(Stream oggStream, string mp3FileName) 
{ 
    SpeexDecoder decoder = new SpeexDecoder(BandMode.Narrow); 
    Mp3WriterConfig config = new Mp3WriterConfig(); 

    using (Mp3Writer mp3 = new Mp3Writer(new FileStream(mp3FileName, FileMode.Create), config)) 
    { 
     int i = 0; 
     int bytesRead = 0; 
     while (i < speexMsg.SpeexData.Length) 
     { 
      short[] outData = new short[160]; 
      bytesRead = decoder.Decode(speexMsg.SpeexData, i, speexMsg.FrameSize, outData, 0, false); 

      for (int x = 0; x < bytesRead; x++) 
       mp3.Write(BitConverter.GetBytes(outData[x])); 

      i += speexMsg.FrameSize; 
     } 

     mp3.Flush(); 
    } 
} 

不幸的是,Mp3WriterConfig和Mp3Writer不是当前库(NSpeex)的一部分。我不知道“speexMsg”应该是什么。

所以我的问题是:如何使用c#将spx(在ogg文件中)转换为mp3?

回答

0

这样的转换需要分两个阶段完成。首先从ogg解码到PCM。然后从PCM编码到WAV。所以如果出现问题,一个好的调试方法是首先从解码的ogg创建一个WAV文件。这可以让你聆听解码后的音频并检查它是否正常。然后你可以解决MP3编码的第二阶段。您可以使用NAudio WaveFileWriter类创建您的WAV文件。

+0

如何解码ogg文件到PCM? – Kinetic

+0

我认为这是可能的NSpeex。最好在项目现场询问。 –

+0

是的,我已经做到了。 NSpeex codeplex页面似乎并不活跃,所以我没有保持希望。同时,我们的用户将使用VLC手动进行转换。如果我找到它,我会发布完整的解决方案。 – Kinetic

相关问题