-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
167 lines (128 loc) · 4.36 KB
/
MainWindow.xaml.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using YoutubeExplode;
using YoutubeExplode.Models.MediaStreams;
using Microsoft.WindowsAPICodePack.Dialogs;
using System.IO;
namespace YT2MP3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public partial class MainWindow : Window
{
DateTime time = DateTime.Now;
YoutubeClient client = new YoutubeClient();
String currentUrl;
String currentId;
public MainWindow()
{
InitializeComponent();
currentUrl = "https://www.youtube.com/watch?v=sNPnbI1arSE";
}
private void DownloadBtnClick(object sender, RoutedEventArgs e)
{
String dir = directoryText.Text;
DownloadVid(currentUrl, dir);
}
async void DownloadVid(String url, String dir)
{
Action<double> a = new Action<double>(updateProgressBar);
Progress<double> p = new Progress<double>(a);
try
{
var streamInfoSet = await client.GetVideoMediaStreamInfosAsync(currentId);
var streamInfo = streamInfoSet.Audio.WithHighestBitrate();
// Get file extension based on stream's container
var ext = "wav";
// Download stream to file
string[] array = { dir, $"test.{ext}" };
string fullPath = System.IO.Path.Combine(array);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
await client.DownloadMediaStreamAsync(streamInfo, fullPath, p);
}
catch (Exception e)
{
if (e is FormatException)
{
messageTxt.Text = "error parsing link";
}
}
}
private void SelectDir(object sender, RoutedEventArgs e)
{
if (CommonOpenFileDialog.IsPlatformSupported)
{
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
dialog.Title = "Choose Directory";
dialog.IsFolderPicker = true;
dialog.EnsurePathExists = true;
dialog.Multiselect = false;
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
string selectedFolder = dialog.FileName;
directoryText.Text = selectedFolder;
}
}
}
private void updateProgressBar(double p)
{
dlProgress.Value = p;
if (p == 1)
{
messageTxt.Text = "done";
dlProgress.Value = 0;
}
}
private async void LoadData()
{
try
{
currentId = YoutubeClient.ParseVideoId(currentUrl);
currentUrl = urlTextBox.Text;
var video = await client.GetVideoAsync(currentId);
messageTxt.Text = video.Title;
}
catch (System.FormatException)
{
messageTxt.Text = "Couldn't parse URL";
}
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
currentUrl = ((TextBox)sender).Text;
LoadData();
}
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
LoadData();
}
}
private void directoryPicker_TextChanged(object sender, TextChangedEventArgs e)
{
}
private void Album_Button_Click(object sender, RoutedEventArgs e)
{
Window albumWindow = new AlbumWindow(currentUrl);
albumWindow.Show();
}
}
}