I want to record raw audio from WASAPI loopback by NAudio and pipe to FFmpeg for streaming via memory stream. As from this document, FFmpeg can get input as Raw However, I got result speed at 8~10x! Here is my code:
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();
FFmpeg Arguments:
ffmpegProcess.StartInfo.Arguments = String.Format("-f s16le -i pipe:0 -y output.wav");
1. Can someone please explain this situation and give me a solution?
2. Should I add Wav header to the Memory Stream then pipe to FFmpeg as Wav format?
The Working Solution
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();
FFMpeg Arguments:
ffmpegProcess.StartInfo.Arguments = string.Format("-f f32le -ac 2 -ar 44.1k -i pipe:0 -c:a copy -y output.wav");
make sure you pass the correct waveformat parameters to FFMpeg. You'll to check the FFmpeg documentation for details of this. WASAPI capture is going to be stereo IEEE float (32 bit), and probably 44.1kHz or 48kHz. Also you should use e.BytesRecorded
not e.Buffer.Length
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With