【问题标题】:Convert WasapiLoopbackCapture wav audio stream to MP3 file将 WasapiLoopbackCapture wav 音频流转换为 MP3 文件
【发布时间】:2013-11-12 08:59:11
【问题描述】:

我能够在 WasapiLoopbackCapture (naudio) 的帮助下捕获扬声器生成的系统音频。但问题是它捕获 wav 文件和 wav 文件的大小非常大(几乎 10 到 15 MB/分钟)。我必须捕捉 2-3 小时的音频,这太高了。 我正在寻找一种解决方案,它将由 WasapiLoopbackCapture 捕获的 wav 流转换为 MP3,然后将其保存到磁盘。我尝试使用 LAME.exe 或其他解决方案进行 loat,但没有成功。任何工作代码。

这是我的代码:

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

【问题讨论】:

    标签: c# audio naudio


    【解决方案1】:

    这是一个使用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);
            }
        }
    }
    

    目前,NuGet 上的 NAudio.Lame 包仅针对 x86 进行编译,因此请确保您的应用程序设置为针对该平台。

    【讨论】:

    • 您好,我无法使用 NAudio.Lame 添加;在我的项目中,我尝试添加 lame_enc.dll 它也没有添加到我的项目中,你能告诉我如何在我的项目中添加这个命名空间。
    • 嗨,不,我现在可以将 naudio.lame 的引用添加到我的项目中,它显示错误“在声明之前不能使用局部变量 '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();停止=假;但它仍然显示错误:-不支持的编码格式可扩展
    • 嗨,Cory 感谢它现在为我工作。是的,它在基于 64 位的机器上显示错误。解决此问题的任何解决方法。
    • 我猜我最终将不得不做 32 位和 64 位软件包。目前,从 GitHub 获取源代码并针对任何平台进行编译,然后添加 64 位或 32 位版本的 libmp3lame.dll。
    • 我刚刚在 NuGet 上放了一个预发布版本。它会根据程序当前运行的位宽自动选择正确的 DLL。应该适用于开箱即用的所有平台目标(x86、x64 和 AnyCPU)。试试看(在 NuGet 窗口中选择 Include prerelease),让我知道它是否适合您。
    【解决方案2】:

    要将音频即时转换为 MP3,最简单的方法之一是使用命令行选项,让您通过标准输入将音频传递到 LAME.exe。我在this article 中描述了如何做到这一点。

    您可能还对 Corey Murtagh 创建的 a LAME package for NAudio 感兴趣。我自己没有尝试过,但看起来它也应该可以完成这项工作。文档是here

    【讨论】:

    • 嘿,我的第一个插件:P
    • 您好我正在尝试使用此代码但没有成功(从您提供的文章中复制) 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;
    猜你喜欢
    • 2019-01-07
    • 2023-03-23
    • 2021-08-15
    • 2022-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    相关资源
    最近更新 更多