首页 > 解决方案 > 如何将 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,无法播放。

标签: azure.net-coreazure-cognitive-services

解决方案


我设法通过使用AudioDatafrom解决了这个问题,并从中speechSynthesisResult创建了一个流:

var buffer = speechSynthesisResult.AudioData;
MemoryStream stream = new MemoryStream(buffer);

推荐阅读