将WasapiLoopbackCapture WAV音频流转换为MP3文件 [英] Convert WasapiLoopbackCapture wav audio stream to MP3 file

查看:171
本文介绍了将WasapiLoopbackCapture WAV音频流转换为MP3文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am able to capture system audio which is generated by speaker with the help of WasapiLoopbackCapture (naudio). but the problem is it capture wav file and size of wav file very large (almost 10 to 15 MB/Min). I have to capture 2-3 hour audio and this will be too high. I m looking for a solution that will convert wav stream which is capture by WasapiLoopbackCapture convert to MP3 and then save this to disk. I try a loat with LAME.exe or other solution but not get success. Any working code.

这是我的代码:

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

推荐答案

下面是使用NAudio.Lame(在控制台应用程序中)从声卡回送捕获数据并将其直接写入MP3文件的示例:

Here's an example of using NAudio.Lame (in a console application) to capture data from sound card loopback and write direct to an MP3 file:

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编译,因此请确保您的应用程序设置为针对该平台.

At the moment the NAudio.Lame package on NuGet is compiled for x86 only, so make sure your application is set to target that platform.

这篇关于将WasapiLoopbackCapture WAV音频流转换为MP3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆