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 (将#修改为@)