-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
122 lines (111 loc) · 4.48 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using YoutubeDLSharp;
using YoutubeDLSharp.Metadata;
using YoutubeDLSharp.Options;
namespace VidGrab
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
YoutubeDL ytdl;
CancellationTokenSource VCanceller;
CancellationTokenSource ACanceller;
Progress<DownloadProgress> progress;
bool shouldUpdate = false;
public MainWindow()
{
InitializeComponent();
FilePathBox.Text = $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}\\Downloads";
ytdl = new YoutubeDL()
{
YoutubeDLPath = "binaries/yt-dlp.exe",
FFmpegPath = "binaries/ffmpeg/ffmpeg.exe",
OutputFolder = FilePathBox.Text
};
shouldUpdate = true;
progress = new Progress<DownloadProgress>(p => ProgressBar.Value = p.Progress*100);
// progress = new Progress<DownloadProgress>(p => Console.WriteLine(p.Progress));
CancelAudioDownloadButton.Visibility = Visibility.Hidden;
CancelVideoDownloadButton.Visibility = Visibility.Hidden;
VideoInformationHolder.Visibility = Visibility.Hidden;
}
private async void GetVideoInfoButton_Click(object sender, RoutedEventArgs e)
{
string URL = VideoURL.Text;
RunResult<VideoData> vd = await ytdl.RunVideoDataFetch(URL);
if (vd.Success)
{
VideoTitleLabel.Content = vd.Data.Title;
CreatorLabel.Content = $"By: {vd.Data.Channel}";
ThumbnailHolder.Source = new BitmapImage(new Uri(vd.Data.Thumbnail));
VideoInformationHolder.Visibility = Visibility.Visible;
//RunResult<string> res = await ytdl.RunVideoDownload(URL);
}
else
{
System.Windows.MessageBox.Show("Something went wrong. Ensure a valid URL was provided and you have a working internet connecion", "An error occured", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private async void DownloadVideoButton_Click(object sender, RoutedEventArgs e)
{
CancelVideoDownloadButton.Visibility = Visibility.Visible;
string URL = VideoURL.Text;
VCanceller = new CancellationTokenSource();
try
{
RunResult<string> video = await ytdl.RunVideoDownload(URL, ct: VCanceller.Token, progress: progress);
}
catch { }
CancelVideoDownloadButton.Visibility = Visibility.Hidden;
}
private void CancelDownloadVideo(object sender, RoutedEventArgs e){ VCanceller.Cancel(); }
private void CancelDownloadAudio(object sender, RoutedEventArgs e){ ACanceller.Cancel(); }
private async void DownloadAudioButton_Click(object sender, RoutedEventArgs e)
{
CancelAudioDownloadButton.Visibility = Visibility.Visible;
string URL = VideoURL.Text;
ACanceller = new CancellationTokenSource();
try
{
RunResult<string> video = await ytdl.RunAudioDownload(URL, format: AudioConversionFormat.Mp3, ct: ACanceller.Token, progress: progress);
}
catch { }
CancelAudioDownloadButton.Visibility = Visibility.Hidden;
}
private void OpenDownloadDirButton_Click(object sender, RoutedEventArgs e)
{
Process.Start(FilePathBox.Text);
}
private void LocateFolderButton_Click(object sender, RoutedEventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.ShowDialog();
if(fbd.SelectedPath != string.Empty)
{
FilePathBox.Text = fbd.SelectedPath;
}
}
private void OutputPathChanged(object sender, TextChangedEventArgs e)
{
if (shouldUpdate) { ytdl.OutputFolder = FilePathBox.Text; }
}
}
}