微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

YoutubeExplode 使用 Azure Speech to Text

如何解决YoutubeExplode 使用 Azure Speech to Text

我必须为 YouTube 视频创建翻译的音频版本,因此我使用 YoutubeExplode 下载音频文件

var youtubeClient = new YoutubeClient();

var video = await youtubeClient.Videos.GetAsync(videoUrl);

var streamManifest = await youtubeClient.Videos.Streams.GetManifestAsync(video.Id);
var audioStreamInfo = streamManifest.GetAudioOnlyStreams().GetWithHighestBitrate();
var stream = await youtubeClient.Videos.Streams.GetAsync(audioStreamInfo);

然后我创建了一个 Speech Azure Cognitive Service生成翻译后的音频文件,这是我的代码

var speechTranslateConfig = SpeechTranslationConfig.FromSubscription("key","region");
var text = await SpeechToText(speechTranslateConfig,stream);

async Task<string> SpeechToText(SpeechTranslationConfig config,Stream stream)
{
     config.SpeechRecognitionLanguage = "en-US";
     config.AddTargetLanguage("ro");

     using var audioInputStream = AudioInputStream.CreatePushStream();
     using var audioConfig = AudioConfig.FromStreamInput(audioInputStream);
     using var recognizer = new TranslationRecognizer(config,audioConfig);

     var bytes = streamToByteArray(stream);
     audioInputStream.Write(bytes);

     var result = await recognizer.RecognizeOnceAsync();

     return result.Text;
}

private static byte[] streamToByteArray(Stream input)
{
     MemoryStream ms = new MemoryStream();
     input.copyTo(ms);
     return ms.ToArray();
}

我尝试使用 Stream 是因为我不想保存原始音频文件,但我面临的障碍是翻译结果始终为空字符串。

我还尝试保存原始文件并进行翻译(而不是将流转换为字节数组),像这样,一切正常。

我不明白我错过了什么,因为我跟着 documentation

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。