来自 NAudio 的原始音频

Ken*_*Ken 5 c# ffmpeg naudio

我想通过 NAudio 录制来自 WASAPI 环回的原始音频,并通过管道传输到 FFmpeg 以通过内存流进行流式传输。从这个文档开始,FFmpeg 可以作为原始输入但是,我得到了 8~10 倍的结果速度!这是我的代码:

waveInput = new WasapiLoopbackCapture();
waveInput.DataAvailable += new EventHandler<WaveInEventArgs>((object sender, WaveInEventArgs e) => 
{
    lock (e.Buffer)
    {
        if (waveInput == null)
            return;
        try
        {
            using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
            {
                memoryStream.Write(e.Buffer, 0, e.Buffer.Length);
                memoryStream.WriteTo(ffmpeg.StandardInput.BaseStream);
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
});
waveInput.StartRecording();
Run Code Online (Sandbox Code Playgroud)

FFmpeg 参数:

ffmpegProcess.StartInfo.Arguments = String.Format("-f s16le -i pipe:0 -y output.wav");
Run Code Online (Sandbox Code Playgroud)

1.有人可以解释这种情况并给我一个解决方案吗?
2. 我是否应该将 Wav 标头添加到内存流,然后以 Wav 格式通过管道传输到 FFmpeg?

工作解决方案

waveInput = new WasapiLoopbackCapture();
waveInput.DataAvailable += new EventHandler<WaveInEventArgs>((object sender, WaveInEventArgs e) => 
{
    lock (e.Buffer)
    {
        if (waveInput == null)
            return;
        try
        {
            using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
            {
                memoryStream.Write(e.Buffer, 0, e.BytesRecorded);
                memoryStream.WriteTo(ffmpeg.StandardInput.BaseStream);
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
});
waveInput.StartRecording();
Run Code Online (Sandbox Code Playgroud)

FFMpeg 参数:

ffmpegProcess.StartInfo.Arguments = string.Format("-f f32le -ac 2 -ar 44.1k -i pipe:0 -c:a copy -y output.wav");
Run Code Online (Sandbox Code Playgroud)

Mar*_*ath 1

确保将正确的波形参数传递给 FFMpeg。您将查看FFmpeg 文档以获取详细信息。WASAPI 捕获将是立体声 IEEE 浮点(32 位),并且可能是 44.1kHz 或 48kHz。你也应该使用e.BytesRecordednot e.Buffer.Length