I am resampling MP4 (Audio) file into WAV using MediaFoundationReader and ResamplerDmoStream to WaveFormat(16000, 16, 1). After resampling I send the stream to speech API for transcription, but the API fails because the stream does not contain the WAV header. If I save the stream to WAV file (using WaveFileWriter), read the file again and then send the stream to API then the API works because the stream contains the WAV header. Is there a way to prepend the WAV header to the stream after ResamplerDmoStream resampling in order to prevent the time-costly I/O?
You can use WaveFileWriter
to write to a Stream
. So in this case, I'd write to a MemoryStream
, and the WaveFileWriter
must be disposed in order to properly finalize the WAV headers. But then you'll have an in-memory representation of a WAV file.
One caveat, is that you might end up disposing the MemoryStream
when you dispose the WaveFileWriter
, so I have a convenience class in NAudio called IgnoreDisposeStream
which you can wrap the MemoryStream
in to stop it getting disposed before you use it.
来源:https://stackoverflow.com/questions/39328396/prepend-wav-header-in-naudio