在NAudio中添加WAV标头 [英] Prepend WAV Header in NAudio
问题描述
我正在使用MediaFoundationReader和ResamplerDmoStream将MP4(音频)文件重新采样为WAV,转换为WaveFormat(16000,16,1).重新采样后,我将该流发送到语音API进行转录,但是API失败,因为该流不包含WAV标头. 如果我将流保存到WAV文件(使用WaveFileWriter),请再次读取文件,然后将流发送到API,则该API可以正常工作,因为该流包含WAV标头. 是否可以在ResamplerDmoStream重采样之后将WAV标头添加到流之前,以防止耗时的I/O?
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?
推荐答案
您可以使用WaveFileWriter
写入Stream
.因此,在这种情况下,我将写一个MemoryStream
,并且必须放置WaveFileWriter
才能正确完成WAV标头.但是,然后您将获得一个WAV文件的内存表示形式.
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.
一个警告,是您可能在处置WaveFileWriter
时最终处置了MemoryStream
,所以我在NAudio中有一个方便的类,称为IgnoreDisposeStream
,您可以将MemoryStream
包装在其中以阻止它得到在使用前将其丢弃.
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.
这篇关于在NAudio中添加WAV标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!