azure - 如何将 SpeechToText 结果上传到 Azure 容器?
问题描述
我正在使用 Azure 认知服务语音转文本来获取 Azure Blob 的转录。
得到结果后,我试图将其上传回另一个 Azure 容器。
服务方式:
public async Task<MemoryStream> TextToSpeech(string subscriptionKey, string region, string text)
{
var speechTranslateConfig = SpeechTranslationConfig.FromSubscription(subscriptionKey, region);
using var synthesizer = new SpeechSynthesizer(speechTranslateConfig, null);
var speechSynthesisResult = await synthesizer.SpeakTextAsync(text);
using var audioDataStream = AudioDataStream.FromResult(speechSynthesisResult);
audioDataStream.SetPosition(0);
byte[] buffer = new byte[16000];
while (audioDataStream.ReadData(buffer) > 0) ;
var stream = new MemoryStream(buffer);
return stream;
}
在 Controller 中,得到结果后,我试图将结果上传到另一个容器中:
var translatedStream = await _speechService.TextToSpeech(_cognitiveServiceConfig.SubscriptionKey, _cognitiveServiceConfig.Region, text);
var translatedStorageFile = new StorageFile() { Stream = translatedStream, Name = $"{fileName}-TRANSLATED", Extension = audioExtension };
var translatedBlobUrl = _azureBlobStorageService.UploadFileAsync(translatedStorageFile, "translated").Result;
上传方式:
public async Task<string> UploadFileAsync(StorageFile storageFile, string container)
{
var containerClient = new BlobContainerClient(_cloudStorageAccountConfig.ConnectionString, container);
var blobClient = containerClient.GetBlobClient($"{storageFile.Name}.{storageFile.Extension}");
if (!blobClient.Exists())
{
await blobClient.UploadAsync(storageFile.Stream);
}
return blobClient.Uri.AbsoluteUri;
}
我认为我的这部分代码没有按预期工作(即使我在他们的文档中找到了这一点)并且流结果不是正确的:
audioDataStream.SetPosition(0);
byte[] buffer = new byte[16000];
while (audioDataStream.ReadData(buffer) > 0) ;
var stream = new MemoryStream(buffer);
我这样说是因为如果我下载结果文件,它只有 16kb,无法播放。
解决方案
我设法通过使用AudioData
from解决了这个问题,并从中speechSynthesisResult
创建了一个流:
var buffer = speechSynthesisResult.AudioData;
MemoryStream stream = new MemoryStream(buffer);
推荐阅读
- javascript - 如何使用 Puppeteer 捕获渲染过程?
- python - Python pandas:通过重复连接两个具有不同行数的DataFrame
- javascript - 使用 getElementById 禁用链接,其中有许多可以单击的链接
- presto - 在哪里可以找到 presto 最新版本存储库和 presto 发行说明
- javascript - 使用 PixiLoader 加载带有数据 URI 的图像
- reactjs - 如何使用 next.config.js 从 node_modules 加载 svg?
- svg - svg 元素的属性的 XPath 是什么?
- javascript - 使用 .then 从 async/await 函数中提取数据
- python - 尝试实现将电话簿的嵌套列表转换为字典的功能
- json - 如何使用shell处理json数据