C# YoutubeExplode与Azure语音到文本
我必须为YouTube视频创建翻译音频版本,因此我使用C# YoutubeExplode与Azure语音到文本,c#,azure,asp.net-core,speech-to-text,C#,Azure,Asp.net Core,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 audioStream
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);
然后我创建了一个语音Azure认知服务
来生成翻译后的音频文件,下面是我的代码:
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();
}
var speechTranslateConfig=SpeechTranslationConfig.FromSubscription(“键”、“区域”);
var text=等待SpeechToText(speechTranslateConfig,流);
异步任务SpeechToText(SpeechTranslationConfig配置,流)
{
config.SpeechRecognitionLanguage=“en-US”;
配置AddTargetLanguage(“ro”);
使用var audioInputStream=audioInputStream.CreatePushStream();
使用var audioConfig=audioConfig.FromStreamInput(audioInputStream);
使用var识别器=新的TranslationRecognizer(配置、音频配置);
var bytes=streamToByteArray(流);
audioInputStream.Write(字节);
var result=await recognizer.RecognizeOnceAsync();
返回结果.Text;
}
私有静态字节[]streamToByteArray(流输入)
{
MemoryStream ms=新的MemoryStream();
输入。复制到(毫秒);
返回ToArray女士();
}
我尝试使用流
,因为我不想保存原始音频文件,但我面临的障碍是翻译结果总是空字符串
我还尝试保存原始文件并进行翻译(而不是将流转换为字节数组),就像这样,一切正常
我无法理解我遗漏了什么,因为我遵循了规则。溪流就像管道中的水。你不能一次喝完所有的水,你需要保持水龙头打开,直到你喝够为止。同样,您需要一直读取流,直到结束。我猜您已经阅读了流的第一部分(空)并翻译了它。创建一个
StreamReader
,并在循环中不断读取流,直到stream.EndOfStream==true
这也是我的第一个想法,但在转换后,这就是。我还尝试使用将流转换为字节数组,但结果是一样的。在我这方面,似乎流不能直接从youtubeClient使用,如果我直接使用流,结果会得到一个空字符串。如果我将steam另存为文件并使用此文件,则会出现错误:SPXERR\u无效\u头
。仅以16位16000hz和momo传输此文件,一切正常。从YouTube客户端如何获取16位(16000hz)文件?