0
我有一個需要發送到Speech-To-Text API的MP4音頻文件的URL。該API只接受WAV流。我使用n音訊1.7.3和下面的代碼下載的文件,並獲得相應的流被髮送到API:在內存中創建WAV流
string filePath = "C:\Windows\Temp\file.wav";
using (MediaFoundationReader reader = new MediaFoundationReader(audioFileURL))
{
WaveFileWriter.CreateWaveFile(filePath, reader);
}
System.IO.FileStream fs = new FileStream(filePath, FileMode.Open);
然後我的fs
流發送到API和一切工作正常,雖然很慢因爲I/O到/從磁盤。
我決定重寫這段代碼並執行所有在內存中所需的代碼。爲此我寫了下面的代碼(即不提供我一個正確的視頻流):
using (MediaFoundationReader reader = new MediaFoundationReader(audioLocation)){
MemoryStream ms = new MemoryStream();
IgnoreDisposeStream ids = new IgnoreDisposeStream(ms);
WaveFileWriter writer = new WaveFileWriter(ids, reader.WaveFormat);
//Doing one of the following (both provide the same outcome):
//1. reader.CopyTo(ids);
//or
//2. this code from NAudio source:
var buffer = new byte[reader.WaveFormat.AverageBytesPerSecond * 4];
while (true)
{
int bytesRead = reader.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
{
// end of source provider
break;
}
// Write will throw exception if WAV file becomes too large
writer.Write(buffer, 0, bytesRead);
}
writer.Dispose();
Stream streamToSendToAPI = ids.SourceStream;
//Send streamToSendToAPI to Speech-To-Text API
}
我意料的是,使用第二個代碼示例,我在那裏創建WAV頭流,然後將數據添加到流,會爲我提供一個有效的WAV流。但是,當我將它發送到語音到文本API時,該API會給出指示該流無法處理的錯誤(表示該流無效)。
請告知如何解決在內存中的代碼示例創建一個有效的WAV流
謝謝,馬克。有效! –