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");
-f s32le -i pipe:0 -ar 44100 -ac 2 -sample_fmt s32 -c:a copy -y output.wav
. but got 2x speed. The FFMpeg said: Guessed Channel Layout for Input Stream #0.0 : mono. Stream #0:0: Audio: pcm_s32le, 44100 Hz, mono, s32, 1411 kb/s – Octopus