-
Notifications
You must be signed in to change notification settings - Fork 6
/
frmYouTubeDownloader.cs
126 lines (115 loc) · 4.36 KB
/
frmYouTubeDownloader.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace YouTubeDownloader
{
public partial class frmYouTubeDownloader : Form
{
string[] videoUrls;
public frmYouTubeDownloader()
{
InitializeComponent();
panel1.Dock = panel2.Dock = DockStyle.Fill;
ShowPanel(0);
}
private void ShowPanel(int windowNo)
{
panel1.Visible = windowNo == 0;
panel2.Visible = windowNo == 1;
}
private void buttonOk_Click(object sender, EventArgs e)
{
try
{
if (listView1.SelectedItems.Count == 0)
throw new Exception("Please select one video");
if (DialogResult.OK != saveFileDialog1.ShowDialog(this)) return;
YouTubeVideoQuality video = listView1.SelectedItems[0].Tag as YouTubeVideoQuality;
new frmFileDownloader(video.DownloadUrl, saveFileDialog1.FileName).Show(this);
// Process.Start(video.DownloadUrl);
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
e.Result = YouTubeDownloader.GetYouTubeVideoUrls(videoUrls);
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
try
{
UseWaitCursor = false;
if (e.Error!=null)
throw e.Error;
List<YouTubeVideoQuality> urls = e.Result as List<YouTubeVideoQuality>;
foreach (var item in urls)
{
ListViewItem listItem = new ListViewItem(item.Extention);
// listItem.SubItems.Add(item.Extention);
listItem.SubItems.Add(formatSize(item.Dimension));
listItem.Tag = item;
listView1.Items.Add(listItem);
this.Text = "YouTube Downloader- " + item.VideoTitle;
}
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
private string formatSize(object value)
{
string s = ((Size)value).Height >= 720 ? " HD" : "";
if (value is Size) return ((Size)value).Width+"x"+((Size)value).Height + s;
return "";
}
private void buttonCancel_Click(object sender, EventArgs e)
{
Close();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if (!Helper.isValidUrl(textBoxUrl.Text) || !textBoxUrl.Text.ToLower().Contains("www.youtube.com/watch?"))
MessageBox.Show("Please Enter valid youtube video url");
else
{
this.videoUrls = new string[] { textBoxUrl.Text };
pictureBox2.ImageLocation = string.Format("http://i3.ytimg.com/vi/{0}/default.jpg", Helper.GetVideoIDFromUrl(textBoxUrl.Text));
listView1.Items.Clear();
backgroundWorker1.RunWorkerAsync();
UseWaitCursor = true;
ShowPanel(1);
}
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
ShowPanel(0);
}
private void button2_Click(object sender, EventArgs e)
{
try
{
if (listView1.SelectedItems.Count == 0)
throw new Exception("Please select one video");
YouTubeVideoQuality video = listView1.SelectedItems[0].Tag as YouTubeVideoQuality;
Clipboard.SetText(video.DownloadUrl);
// Close();
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
private void textBoxUrl_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
e.Handled = true;
button1_Click(null, null);
}
}
}
}