0

I have to create a translated audio version for a YouTube video so I'm using YoutubeExplode to download the audio file:

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);

Then I've created a Speech Azure Cognitive Service to generate the translated audio file and here is my code:

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();
}

I'm trying to use Stream because I don't want to save the original audio file, but the impediment I'm facing is that the translation result is always an empty string.

I also tried to save the original file and translate it (instead of converting the stream to a byte array) and like this, all works fine.

I can't understand what I'm missing, because I followed the documentation.

Sergiu Molnar
  • 865
  • 1
  • 11
  • 22
  • A stream is like water in a pipe. You don't get all the water at once, you need to keep the tap open until you have had enough. In the same way, you need to keep reading the stream until the end. I'm guessing that you have read the first (empty) part of the stream, and translated that. Create a `StreamReader`, and keep reading the stream in a loop until `stream.EndOfStream == true` – Neil May 03 '21 at 15:39
  • This was also my first thought but after conversion, this is the [byte array](https://ibb.co/2v2V5gL). – Sergiu Molnar May 03 '21 at 15:59
  • I also tried to use [this method](https://stackoverflow.com/questions/1080442/how-to-convert-an-stream-into-a-byte-in-c) to convert the stream to byte array, but the result is the same. – Sergiu Molnar May 03 '21 at 16:05
  • Seems the stream can't be used directly from youtubeClient, on my side,if I use the stream directly, I got an empty string as result. If I save the steam as a file and use this file, I got error :`SPXERR_INVALID_HEADER` . Only transfer this file as 16Bit 16000hz and momo, everything works. – Stanley Gong May 04 '21 at 10:06
  • And from YoutubeClient how I can get the 16bit (16000hz) file? – Sergiu Molnar May 04 '21 at 10:24

0 Answers0