2013-11-01 168 views
1

我能夠在WasapiLoopbackCapture(naudio)的幫助下捕獲揚聲器生成的系統音頻。但問題是它捕獲的wav文件和wav文件的大小非常大(幾乎10到15 MB /分鐘)。我必須捕捉2-3小時的音頻,這將會太高。 我正在尋找一種解決方案,將WAV流轉換爲MP3,然後將其保存到磁盤上,由WasapiLoopbackCapture捕獲。我嘗試了LAME.exe或其他解決方案,但沒有成功。任何工作代碼。將WasapiLoopbackCapture wav音頻流轉換爲MP3文件

這裏是我的代碼:

private void button1_Click(object sender, EventArgs e){ 
    LoopbackRecorder obj = new LoopbackRecorder(); 
    string a = textBox1.Text; 
    obj.StartRecording(@"e:\aman.mp3"); 

} 

public class LoopbackRecorder 
{ 
    private IWaveIn _waveIn; 
    private Mp3WaveFormat _mp3format; 
    private WaveFormat _wavFormat; 


    private WaveFileWriter _writer; 
    private bool _isRecording = false; 


    /// <summary> 
    /// Constructor 
    /// </summary> 
    public LoopbackRecorder() 
    { 

    } 

    /// <summary> 
    /// Starts the recording. 
    /// </summary> 
    /// <param name="fileName"></param> 
    public void StartRecording(string fileName) 
    { 
     // If we are currently record then go ahead and exit out. 
     if (_isRecording == true) 
     { 
      return; 
     } 

     _fileName = fileName; 
     _waveIn = new WasapiLoopbackCapture(); 
     // _waveIn.WaveFormat = new WaveFormat(16000, 16 , 2); 
     _writer = new WaveFileWriter(fileName, _waveIn.WaveFormat); 

     _waveIn.DataAvailable += OnDataAvailable; 
     // _waveIn.RecordingStopped += OnRecordingStopped; 
     _waveIn.StartRecording(); 
     _isRecording = true; 
    } 




    private void OnDataAvailable(object sender, WaveInEventArgs waveInEventArgs) 
    { 
     if (_writer == null) 
     { 
      _writer = new WaveFileWriter(@"e:\aman.mp3", _waveIn.WaveFormat); 
     } 

     _writer.Write(waveInEventArgs.Buffer, 0, waveInEventArgs.BytesRecorded); 

     byte[] by= Float32toInt16(waveInEventArgs.Buffer, 0, waveInEventArgs.BytesRecorded); 

    } 

    private string _fileName = ""; 
    /// <summary> 
    /// The name of the file that was set when StartRecording was called. E.g. the current file being written to. 
    /// </summary> 
    public string FileName 
    { 
     get 
     { 
      return _fileName; 
     } 
    } 
} 

回答

2

下面是一個使用NAudio.Lame(在控制檯應用程序)來捕獲從聲卡回送數據直接寫入到MP3文件的例子:

using System; 
using NAudio.Lame; 
using NAudio.Wave; 

namespace MP3Rec 
{ 
    class Program 
    { 
     static LameMP3FileWriter wri; 
     static bool stopped = false; 

     static void Main(string[] args) 
     { 
      // Start recording from loopback 
      IWaveIn waveIn = new WasapiLoopbackCapture(); 
      waveIn.DataAvailable += waveIn_DataAvailable; 
      waveIn.RecordingStopped += waveIn_RecordingStopped; 
      // Setup MP3 writer to output at 32kbit/sec (~2 minutes per MB) 
      wri = new LameMP3FileWriter(@"C:\temp\test_output.mp3", waveIn.WaveFormat, 32); 
      waveIn.StartRecording(); 

      stopped = false; 

      // Keep recording until Escape key pressed 
      while (!stopped) 
      { 
       if (Console.KeyAvailable) 
       { 
        var key = Console.ReadKey(true); 
        if (key != null && key.Key == ConsoleKey.Escape) 
         waveIn.StopRecording(); 
       } 
       else 
        System.Threading.Thread.Sleep(50); 
      } 

      // flush output to finish MP3 file correctly 
      wri.Flush(); 
      // Dispose of objects 
      waveIn.Dispose(); 
      wri.Dispose(); 
     } 

     static void waveIn_RecordingStopped(object sender, StoppedEventArgs e) 
     { 
      // signal that recording has finished 
      stopped = true; 
     } 

     static void waveIn_DataAvailable(object sender, WaveInEventArgs e) 
     { 
      // write recorded data to MP3 writer 
      if (wri != null) 
       wri.Write(e.Buffer, 0, e.BytesRecorded); 
     } 
    } 
} 

目前的NAudio.Lame包裝上的NuGet編譯爲86只有,因此請確保您的應用程序已設置爲定位該平臺。

+0

嗨,我無法添加使用NAudio.Lame; 在我的項目中我嘗試添加lame_enc.dll它也沒有添加到我的項目中,你可以告訴我如何在我的項目中添加這個名稱空間。 –

+0

嗨,不,我可以將naudio.lame的ref添加到我的項目中,現在顯示錯誤「在聲明之前不能使用局部變量'waveIn'」。我嘗試像這樣//從迴環開始記錄 IWaveIn waveIn = new WasapiLoopbackCapture(); wri = new LameMP3FileWriter(@「C:\ temp \ test_output.mp3」,waveIn.WaveFormat,128); waveIn.DataAvailable + = waveIn_DataAvailable; waveIn.RecordingStopped + = waveIn_RecordingStopped; waveIn.StartRecording(); stopped = false; 但它仍然顯示錯誤: - 無支持的編碼格式可擴展 –

+0

嗨科裏感謝它現在爲我工作。是的,它在基於64位的機器上顯示錯誤。解決此問題的任何解決方法。 –

1

要將音頻轉換成MP3上的蒼蠅,最簡單的方法之一是使用,讓您通過標準輸入傳遞音頻到LAME.exe的命令行選項。我描述瞭如何在this article中做到這一點。

您可能也有興趣Corey Murtagh創建了a LAME package for NAudio。我自己並沒有嘗試過,但它看起來應該也能完成這項工作。文檔是here

+1

嗨,我的第一個插件:P – Corey

+0

HI我試圖使用此代碼但未獲得成功(從您提供的文章複製) string outputFileName = @「c:\ users \ mark \ documents \ test.mp3 「; 進程p = new Process(); p.StartInfo.FileName = @「lame.exe」; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.Arguments =「-r -s 16 -m m - \」「+ outputFileName +」\「」; p.StartInfo.CreateNoWindow = true; p.Start(); //現在將原始PCM音頻寫入標準輸入流並在完成後關閉它 p.StandardInput.BaseStream; –