public TranscribeResponse GetTranscribeResponse(RepeatedField <SpeechRecognitionResult> results) { TranscribeResponse transcript = new TranscribeResponse(); foreach (var result in results) { foreach (var alternative in result.Alternatives) { Console.WriteLine($"Transcript: { alternative.Transcript}"); Console.WriteLine("Word details:"); Console.WriteLine($" Word count:{alternative.Words.Count}"); RspAlternative alt = new RspAlternative(alternative.Transcript) { wordCount = alternative.Words.Count }; int count = 1; foreach (var item in alternative.Words) { alt.words.Add(new RspWord(item.Word, ParseDuration(item.StartTime), ParseDuration(item.EndTime), count++)); Console.WriteLine($" {item.Word}"); Console.WriteLine($" WordStartTime: {item.StartTime}"); Console.WriteLine($" WordEndTime: {item.EndTime}"); } transcript.alternatives.Add(alt); } } return(transcript); }
public TranscribeResponse TranscribeInCloud(string objectName, string language) { var speech = SpeechClient.Create(); string fileOnCloudStorage = "gs://" + GoogleCloudBucketName + "/" + objectName; RecognitionAudio recogAudio = RecognitionAudio.FromStorageUri(fileOnCloudStorage); var longOperation = speech.LongRunningRecognize(new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.Flac, SampleRateHertz = 48000, EnableWordTimeOffsets = true, LanguageCode = language, }, recogAudio); longOperation = longOperation.PollUntilCompleted(); var response = longOperation.Result; // Transform the Google response into a more usable object. // TranscribeResponse transcript = GetLongTranscribeResponse(response); TranscribeResponse transcript = new TranscribeResponse(); foreach (var result in response.Results) { foreach (var alternative in result.Alternatives) { Console.WriteLine($"Transcript: { alternative.Transcript}"); Console.WriteLine("Word details:"); Console.WriteLine($" Word count:{alternative.Words.Count}"); RspAlternative alt = new RspAlternative(alternative.Transcript) { wordCount = alternative.Words.Count }; int count = 1; foreach (var item in alternative.Words) { alt.words.Add(new RspWord(item.Word, ParseDuration(item.StartTime), ParseDuration(item.EndTime), count++)); Console.WriteLine($" {item.Word}"); Console.WriteLine($" WordStartTime: {item.StartTime}"); Console.WriteLine($" WordEndTime: {item.EndTime}"); } transcript.alternatives.Add(alt); } } return(transcript); }
/// <param name="language">Language of the audio. This is the ISO 639 code</param> /// The audio file name may have been shortened. For the cloud object name, we want to use the original full /// name of the video, but change the extension to ".flac". public TranscribeResponse MoveToCloudAndTranscribe(string audiofilePath, string videoFileName, string language) { string objectName = Path.GetFileNameWithoutExtension(videoFileName) + ".flac"; GoogleBucket gb = new GoogleBucket(); if (config.UseAudioFileAlreadyInCloud) { // Only upload if not in cloud if (!gb.IsObjectInBucket(GoogleCloudBucketName, objectName)) { gb.UploadFile(GoogleCloudBucketName, audiofilePath, objectName, "audio /x-flac"); } } TranscribeResponse transcript = TranscribeInCloud(objectName, language); return(transcript); }
// Transcribe a local audio file. We can only use this with audios up to 1 minute long. public TranscribeResponse TranscribeFile(string fileName, string language) { var speech = SpeechClient.Create(); RecognitionAudio recogAudio = RecognitionAudio.FromFile(fileName); var response = speech.Recognize(new RecognitionConfig() { Encoding = RecognitionConfig.Types.AudioEncoding.Flac, SampleRateHertz = 48000, EnableWordTimeOffsets = true, LanguageCode = language, }, recogAudio); // Transform the Google response into a more usable object. TranscribeResponse transcript = GetShortTranscribeResponse(response); return(transcript); }