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
| 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 YoutubeExtractor;
using System.Threading;
using System.IO;
using System.Net;
namespace Youtube_Downloader
{
public partial class Form1 : Form
{
public int res;
public string path;
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
// Юзаем трай катч, если пользователь забыл выбрать разрешение
try {
res = Int32.Parse(comboBox1.SelectedItem.ToString()); // Из нашего комбобокса айтемы в инт
}
catch
{
MessageBox.Show("Вы забыли выбрать разрешение");
}
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
//юзаем трай катч, если вдруг пользователь забыл ссылку
try
{
IEnumerable<VideoInfo> videos = DownloadUrlResolver.GetDownloadUrls(textBox1.Text); // наша ссылка
VideoInfo video = videos.First(info => info.VideoType == VideoType.Mp4 && info.Resolution == res);
// тут мы указываем тип нашего видео(mp4) и разрешение, которое мы получаем из комбобокса
if (video.RequiresDecryption)
DownloadUrlResolver.DecryptDownloadUrl(video);
// тут мы указываем путь куда будет сохранен ролик+его название. Сам path, мы получаем через FolderBrowserDialog
VideoDownloader downloader = new VideoDownloader(video,Path.Combine(path,video.Title+video.VideoExtension));
downloader.DownloadProgressChanged += Downloader_ProgressChanged;
// мутим поток
Thread threading = new Thread(() =>
{
// тут с вебклиентом, что бы нас не выбивала ошибка 403(вроде работает)
WebClient webcl = new WebClient { Encoding = Encoding.UTF8 };
webcl.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
downloader.Execute();
})
{
IsBackground = true
};
//Запускаем
threading.Start();
}
catch
{
MessageBox.Show("Вы забыли ссылку");
}
}
private void Downloader_ProgressChanged(object sender, ProgressEventArgs e)
{
// Наш прогресс бар во время скачивания + данные выводятся в Label4
Invoke(new MethodInvoker(delegate ()
{
progressBar1.Value = (int)e.ProgressPercentage;
label4.Text = $"{string.Format("{0:0.##}", e.ProgressPercentage)}%";
progressBar1.Update();
if (label4.Text == "100%")
{
MessageBox.Show("Готово! :)");
}
}));
}
private void button2_Click(object sender, EventArgs e)
{
// тут мы указываем путь сохранения через FolderBrowserDialog
folderBrowserDialog1.ShowDialog();
path = folderBrowserDialog1.SelectedPath;
textBox2.Text = folderBrowserDialog1.SelectedPath;
}
private void label5_Click(object sender, EventArgs e)
{
//Уходим из программы
Application.Exit();
}
private void label6_Click(object sender, EventArgs e)
{
//Прячем программу
this.WindowState = FormWindowState.Minimized;
}
//Код без стыда и совести стырил, помогает управлять формой при bsNone
private Point MouseHook;
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) MouseHook = e.Location;
Location = new Point((Size)Location - (Size)MouseHook + (Size)e.Location);
}
}
} |