Repository navigation
Expand file tree
/
Copy pathMainUI.cs
More file actions
387 lines (306 loc) · 14.1 KB
/
Copy pathMainUI.cs
File metadata and controls
387 lines (306 loc) · 14.1 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using YoutubeExplode.Videos;
using YoutubeExplode;
using System.Reflection.Emit;
using System.Net;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using YoutubeExplode.Converter;
using YoutubeExplode.Videos.Streams;
using System.IO;
namespace YoutubeVideoDownloader
{
public partial class MainUI : Form
{
public static Video Video;
public MainUI()
{
InitializeComponent();
try { textBox1.Text = System.IO.File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "CONTACT"); } catch { }
}
private void button1_Click(object sender, EventArgs e)
{
Form form = new YoutubeBrowser();
form.ShowDialog();
if (Program.ValidUrl)
{
textBox1.Text = Program.Url;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
try {
if (textBox1.Text.ToLower().Contains("youtube"))
{
Program.ValidUrl = true;
LoadVideoInfo();
}
else if (textBox1.Text.ToLower().Contains("youtu.be"))
{
Program.ValidUrl = true;
LoadVideoInfo();
}
else
{
Program.ValidUrl = false; foreach (Control control in this.Controls)
{
control.Visible = false;
textBox1.Visible = true;
button1.Visible = true;
}
textBox1.Visible = true;
button1.Visible = true;
}
}
catch {
Program.ValidUrl = false; foreach (Control control in this.Controls)
{
control.Visible = false;
textBox1.Visible = true;
button1.Visible = true;
}
textBox1.Visible = true;
button1.Visible = true;
}
}
private async void Form1_Shown(object sender, EventArgs e)
{
do
{
try
{
}
catch(Exception ex) { MessageBox.Show(ex.Message); }
await Task.Delay(1000);
}
while (true);
}
private async void LoadVideoInfo()
{
var YoutubeClient = new YoutubeExplode.YoutubeClient();
Video = await YoutubeClient.Videos.GetAsync(textBox1.Text);
label1.Text = Video.Duration.ToString();
label2.Text = Video.Title;
label3.Text = Video.Author.ToString();
label4.Text = "View: " +Video.Engagement.ViewCount.ToString() + " Like Count: " + Video.Engagement.LikeCount;
richTextBox1.Text = Video.Description;
string LastUrl = null;
foreach (var thumbnail in Video.Thumbnails)
{
try
{
Console.WriteLine($"{thumbnail.Resolution} {thumbnail.Url}");
LastUrl = thumbnail.Url;
//pictureBox1.BackgroundImage = new Bitmap(thumbnail.Url);
}
catch { }
}
pictureBox1.LoadAsync(LastUrl);
LastUrl = null;
var youtube = new YoutubeClient();
var channel = await youtube.Channels.GetAsync(Video.Author.ChannelId);
foreach (var thumbnail in channel.Thumbnails)
{
try
{
Console.WriteLine($"{thumbnail.Resolution} {thumbnail.Url}");
LastUrl = thumbnail.Url;
//pictureBox1.BackgroundImage = new Bitmap(thumbnail.Url);
}
catch{ }
}
pictureBox2.LoadAsync(LastUrl);
foreach (Control control in this.Controls)
{
control.Visible = true;
textBox1.Visible = true;
button1.Visible = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.ShowIcon = false;
}
private async void button2_Click(object sender, EventArgs e)
{
HideAll();
var youtube = new YoutubeClient();
progressBar1.Value = 1;
// Get the video ID
var videoId = VideoId.Parse(textBox1.Text ?? "");
progressBar1.Value = 20;
// Get available streams and choose the best muxed (audio + video) stream
var streamManifest = await youtube.Videos.Streams.GetManifestAsync(videoId);
var streamInfo = streamManifest.GetAudioStreams().TryGetWithHighestBitrate();
progressBar1.Value = 40;
if (streamInfo is null)
{
// Available streams vary depending on the video and it's possible
// there may not be any muxed streams at all.
// See the readme to learn how to handle adaptive streams.
return;
}
// Download the stream
progressBar1.Value = 63;
var fileName = $"{Video.Title}.{streamInfo.Container.Name}";
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + fileName))
{
File.Delete(AppDomain.CurrentDomain.BaseDirectory + fileName);
}
await youtube.Videos.Streams.DownloadAsync(streamInfo, fileName);
MessageBox.Show("Audio have Saved into " + AppDomain.CurrentDomain.BaseDirectory + fileName);
progressBar1.Value = 0;
ShowAll();
}
private async void Download(string url,bool High)
{
try
{
HideAll();
var youtube = new YoutubeClient();
progressBar1.Value = 1;
// Get the video ID
var videoId = VideoId.Parse(url ?? "");
progressBar1.Value = 20;
// Get available streams and choose the best muxed (audio + video) stream
var streamManifest = await youtube.Videos.Streams.GetManifestAsync(videoId);
var streamInfo = streamManifest.GetMuxedStreams().TryGetWithHighestVideoQuality();
progressBar1.Value = 40;
if (streamInfo is null)
{
// Available streams vary depending on the video and it's possible
// there may not be any muxed streams at all.
// See the readme to learn how to handle adaptive streams.
return;
}
// Download the stream
progressBar1.Value = 63;
if (!High)
{
var fileName = $"{Video.Title}.{streamInfo.Container.Name}";
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + fileName))
{
File.Delete(AppDomain.CurrentDomain.BaseDirectory + fileName);
}
await youtube.Videos.Streams.DownloadAsync(streamInfo, fileName);
MessageBox.Show("Video have Saved into " + AppDomain.CurrentDomain.BaseDirectory + fileName);
progressBar1.Value = 0;
ShowAll();
}
else
{
/*
var streamInfo1 = streamManifest.GetVideoOnlyStreams().TryGetWithHighestVideoQuality();
var fileName = $"{videoId}.{streamInfo.Container.Name}";
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + fileName))
{
File.Delete(AppDomain.CurrentDomain.BaseDirectory + fileName);
}
await youtube.Videos.Streams.DownloadAsync(streamInfo1, fileName);*/
var streamManifest1 = await youtube.Videos.Streams.GetManifestAsync(url);
var fileName = $"{Video.Title}.{streamInfo.Container.Name}";
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + fileName))
{
File.Delete(AppDomain.CurrentDomain.BaseDirectory + fileName);
}
var te = streamManifest1.GetVideoStreams().TryGetWithHighestVideoQuality();
// Select streams (1080p60 / highest bitrate audio)
var audioStreamInfo = streamManifest.GetAudioStreams().GetWithHighestBitrate();
//var videoStreamInfo = streamManifest.GetVideoStreams().First(s => s.VideoQuality.Label == "1080p60");
var streamInfos = new IStreamInfo[] { audioStreamInfo, te };
// Download and process them into one file
await youtube.Videos.DownloadAsync(streamInfos, new ConversionRequestBuilder(fileName).Build());
MessageBox.Show("Video have Saved into "+AppDomain.CurrentDomain.BaseDirectory+fileName);
/*
var streamInfo2 = streamManifest.GetAudioStreams().TryGetWithHighestBitrate();
Program.BetaAudioPath= $"{videoId}_1.{streamInfo.Container.Name}";
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + Program.BetaAudioPath))
{
File.Delete(AppDomain.CurrentDomain.BaseDirectory + Program.BetaAudioPath);
}
await youtube.Videos.Streams.DownloadAsync(streamInfo2, Program.BetaAudioPath);
string args = $"-i \"{Program.BetaVideoOnlyPath}\" -i \"{Program.BetaAudioPath}\" -shortest {$"{videoId}.{streamInfo.Container.Name}"}";
Process startInfo = new Process();
startInfo.StartInfo.CreateNoWindow = true;
startInfo.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory+ "ffmpeg-2022-08-10-git-8fc7f0fdec-full_build\\bin\\ffmpeg.exe";
startInfo.StartInfo.WorkingDirectory = @"" + AppDomain.CurrentDomain.BaseDirectory+ " ffmpeg-2022-08-10-git-8fc7f0fdec-full_build\\bin";
startInfo.StartInfo.Arguments = args;
File.Copy(AppDomain.CurrentDomain.BaseDirectory+Program.BetaAudioPath,AppDomain.CurrentDomain.BaseDirectory+ "ffmpeg-2022-08-10-git-8fc7f0fdec-full_build\\"+Program.BetaAudioPath, true);
File.Copy(AppDomain.CurrentDomain.BaseDirectory + Program.BetaVideoOnlyPath, AppDomain.CurrentDomain.BaseDirectory + "ffmpeg-2022-08-10-git-8fc7f0fdec-full_build\\" + Program.BetaVideoOnlyPath, true);
startInfo.Start();
startInfo.WaitForExit();
try { File.Copy("ffmpeg-2022-08-10-git-8fc7f0fdec-full_build\\" + $"{videoId}.{streamInfo.Container.Name}", AppDomain.CurrentDomain.BaseDirectory + $"{videoId}.{streamInfo.Container.Name}", true); } catch { } */
}
// await youtube.Videos.Streams.DownloadAsync(streamInfo, fileName);
progressBar1.Value = 100;
progressBar1.Value = 0;
ShowAll();
}
catch (Exception ex) { MessageBox.Show("Some Error on Downloading Youtube Video\n" + ex.Message); progressBar1.Value = 0; ShowAll(); }
}
private void button3_Click(object sender, EventArgs e)
{
Download(textBox1.Text,false);
}
private void button5_Click(object sender, EventArgs e)
{
Download(textBox1.Text, true);
}
private async void button4_Click(object sender, EventArgs e)
{
HideAll();
var youtube = new YoutubeClient();
progressBar1.Value = 1;
// Get the video ID
var videoId = VideoId.Parse(textBox1.Text ?? "");
progressBar1.Value = 20;
// Get available streams and choose the best muxed (audio + video) stream
var streamManifest = await youtube.Videos.Streams.GetManifestAsync(videoId);
var streamInfo = streamManifest.GetVideoOnlyStreams().TryGetWithHighestBitrate();
progressBar1.Value = 40;
if (streamInfo is null)
{
// Available streams vary depending on the video and it's possible
// there may not be any muxed streams at all.
// See the readme to learn how to handle adaptive streams.
return;
}
// Download the stream
progressBar1.Value = 63;
var fileName = $"{Video.Title}.{streamInfo.Container.Name}";
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + fileName))
{
File.Delete(AppDomain.CurrentDomain.BaseDirectory + fileName);
}
await youtube.Videos.Streams.DownloadAsync(streamInfo, fileName);
MessageBox.Show("Audio have Saved into " + AppDomain.CurrentDomain.BaseDirectory + fileName);
progressBar1.Value = 0;
ShowAll();
}
private void HideAll()
{
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
textBox1.Enabled = false;
}
private void ShowAll()
{
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
button5.Enabled = true;
textBox1.Enabled = true;
}
}
}