-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
198 lines (167 loc) · 7.17 KB
/
Program.cs
File metadata and controls
198 lines (167 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.IO;
using System.Threading.Tasks;
using YoutubeExplode;
using YoutubeExplode.Videos;
using YoutubeExplode.Videos.Streams;
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
public static class Program
{
public static async Task<int> Main(string[] args)
{
// Get the YouTube video URL
var videoIdOrUrl = args.Length == 1 ? args[0] : null;
if (string.IsNullOrEmpty(videoIdOrUrl))
{
Console.Error.WriteLine("USAGE: ytd ID");
Console.Error.WriteLine(" OR: ytd URL");
return 1;
}
// Get the Azure Speech endpoint and subscription key
var region = Environment.GetEnvironmentVariable("AZURE_AI_SPEECH_REGION");
var key = Environment.GetEnvironmentVariable("AZURE_AI_SPEECH_KEY");
if (string.IsNullOrEmpty(region) || string.IsNullOrEmpty(key))
{
Console.Error.WriteLine("ERROR: Environment variables AZURE_AI_SPEECH_REGION and/or AZURE_AI_SPEECH_KEY are not set.");
return 2;
}
// Download the video and recognize the speech content
var fileName = await DownloadVideo(videoIdOrUrl);
var transcript = await RecognizeSpeech(region, key, fileName);
return 0;
}
private async static Task<string> DownloadVideo(string videoIdOrUrl)
{
var client = new YoutubeClient();
var streamManifest = await client.Videos.Streams.GetManifestAsync(videoIdOrUrl);
var streamInfo = streamManifest.GetAudioOnlyStreams().GetWithHighestBitrate();
if (streamInfo is null)
{
Console.Error.WriteLine("WARNING! This video has no audio streams.");
System.Environment.Exit(3);
}
var fileName = $"{videoIdOrUrl}.{streamInfo.Container.Name}";
if (!Console.IsOutputRedirected)
{
Console.Write($"Downloading audio to {fileName} ... ");
}
var progress = new ConsoleProgress();
await client.Videos.Streams.DownloadAsync(streamInfo, fileName, progress);
if (!Console.IsOutputRedirected)
{
Console.WriteLine(" ... Done!");
Console.WriteLine();
}
return fileName;
}
private static async Task<string> RecognizeSpeech(string region, string key, string audioFile)
{
// Create a speech config from the subscription key and region
var speechConfig = SpeechConfig.FromSubscription(key, region);
speechConfig.SpeechRecognitionLanguage = "en-US";
// Create an audio config from the audio file
var audioConfig = CreateAudioConfigFromFile(audioFile, "any");
var recognizer = new SpeechRecognizer(speechConfig, audioConfig);
// We'll use a task completion source to know when the recognition is done
var transcript = string.Empty;
var finished = new TaskCompletionSource<int>();
// Handle the events
recognizer.Recognizing += (s, e) => HandleRecognizingEvent(e);
recognizer.Recognized += (s, e) => HandleRecognizedEvent(e, ref transcript);
recognizer.Canceled += (s, e) => HandleCanceledEvent(e, finished);
// Start the recognition, and wait for it to finish
await recognizer.StartContinuousRecognitionAsync();
await finished.Task;
return transcript;
}
private static void HandleRecognizingEvent(SpeechRecognitionEventArgs e)
{
if (!Console.IsOutputRedirected && !string.IsNullOrEmpty(e.Result.Text))
{
// if the text is too big to print with a trailing '...', then, print it with a leading '...' and ensure it doesn't exceed the console width
var text = e.Result.Text.Length > Console.WindowWidth - 9 // (9 = 3 for the leading '...', 3 for the trailing '...', and 3 for the space between the '...' and the text)
? "... " + e.Result.Text.Substring(e.Result.Text.Length - (Console.WindowWidth - 9)) + " ..."
: e.Result.Text + "...";
// erase the last line
Console.CursorLeft = 0;
Console.Write(new string(' ', Console.WindowWidth - 1));
// print the new text
Console.CursorLeft = 0;
Console.Write(text);
}
}
private static void HandleRecognizedEvent(SpeechRecognitionEventArgs e, ref string transcript)
{
if (e.Result.Reason == ResultReason.RecognizedSpeech && !string.IsNullOrEmpty(e.Result.Text))
{
// append the new text
transcript += e.Result.Text + "\n";
// erase the last line
Console.CursorLeft = 0;
Console.Write(new string(' ', Console.WindowWidth - 1));
// print the new text
Console.CursorLeft = 0;
Console.WriteLine($"{e.Result.Text}\n");
}
}
private static void HandleCanceledEvent(SpeechRecognitionCanceledEventArgs e, TaskCompletionSource<int> finished)
{
if (e.Reason == CancellationReason.EndOfStream)
{
finished.SetResult(0);
}
else if (e.Reason == CancellationReason.Error)
{
Console.Error.WriteLine($"CANCELED: ErrorCode={e.ErrorCode}");
Console.Error.WriteLine($"CANCELED: ErrorDetails={e.ErrorDetails}");
Console.Error.WriteLine($"CANCELED: Did you update the subscription info?");
System.Environment.Exit(4);
}
else
{
System.Environment.Exit(5);
}
}
private static AudioConfig CreateAudioConfigFromFile(string file, string? format)
{
return !string.IsNullOrEmpty(format)
? AudioConfig.FromStreamInput(CreatePushStream(file, format))
: AudioConfig.FromWavFileInput(file);
}
static public PushAudioInputStream CreatePushStream(string file, string format)
{
return !string.IsNullOrEmpty(format)
? CreatePushStream(file, ContainerFormatFrom(format))
: CreatePushStream(file);
}
static public AudioStreamContainerFormat ContainerFormatFrom(string format)
{
return format switch {
"any" => AudioStreamContainerFormat.ANY,
"alaw" => AudioStreamContainerFormat.ALAW,
"amrnb" => AudioStreamContainerFormat.AMRNB,
"amrwb" => AudioStreamContainerFormat.AMRWB,
"flac" => AudioStreamContainerFormat.FLAC,
"mp3" => AudioStreamContainerFormat.MP3,
"ogg" => AudioStreamContainerFormat.OGG_OPUS,
"mulaw" => AudioStreamContainerFormat.MULAW,
_ => AudioStreamContainerFormat.ANY
};
}
static public PushAudioInputStream CreatePushStream(string file, AudioStreamContainerFormat containerFormat)
{
var pushFormat = AudioStreamFormat.GetCompressedFormat(containerFormat);
var push = AudioInputStream.CreatePushStream(pushFormat);
push.Write(File.ReadAllBytes(file));
push.Close();
return push;
}
static public PushAudioInputStream CreatePushStream(string file)
{
var push = AudioInputStream.CreatePushStream();
push.Write(File.ReadAllBytes(file));
push.Close();
return push;
}
}