forked from Bluegrams/YoutubeDLSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
107 lines (95 loc) · 3.52 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
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using YoutubeDLSharp;
using YoutubeDLSharp.Metadata;
using YoutubeDLSharp.Options;
namespace WpfDemoApp
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private bool isNotDownloading = true;
private bool audioOnly = false;
private IProgress<DownloadProgress> progress;
private IProgress<string> output;
public MainWindow()
{
this.YoutubeDL = new YoutubeDL() { YoutubeDLPath = "yt-dlp.exe" };
this.DataContext = this;
InitializeComponent();
progress = new Progress<DownloadProgress>((p) => showProgress(p));
output = new Progress<string>((s) => txtOutput.AppendText(s + Environment.NewLine));
}
public YoutubeDL YoutubeDL { get; }
public bool IsNotDownloading
{
get => isNotDownloading;
set
{
isNotDownloading = value;
propertyChanged();
}
}
public bool AudioOnly
{
get => audioOnly;
set
{
audioOnly = value;
propertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void propertyChanged([CallerMemberName] string property = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
private async void DownloadButton_Click(object sender, RoutedEventArgs e)
{
string url = txtUrl.Text;
IsNotDownloading = false;
txtOutput.Clear();
// Parse custom arguments
OptionSet custom = OptionSet.FromString(txtOptions.Text.Split('\n'));
RunResult<string> result;
if (AudioOnly)
{
result = await YoutubeDL.RunAudioDownload(
url, AudioConversionFormat.Mp3, progress: progress,
output: output, overrideOptions: custom
);
}
else
{
result = await YoutubeDL.RunVideoDownload(url, progress: progress, output: output, overrideOptions: custom);
}
if (result.Success)
{
MessageBox.Show($"Successfully downloaded \"{url}\" to:\n\"{result.Data}\".", "YoutubeDLSharp");
}
else showErrorMessage(url, String.Join("\n", result.ErrorOutput));
IsNotDownloading = true;
}
private void showProgress(DownloadProgress p)
{
txtState.Text = p.State.ToString();
progDownload.Value = p.Progress;
txtProgress.Text = $"speed: {p.DownloadSpeed} | left: {p.ETA}";
}
private async void InformationButton_Click(object sender, RoutedEventArgs e)
{
string url = txtUrl.Text;
RunResult<VideoData> result = await YoutubeDL.RunVideoDataFetch(url);
if (result.Success)
{
var infoWindow = new InformationWindow(result.Data);
infoWindow.ShowDialog();
}
else showErrorMessage(url, String.Join("\n", result.ErrorOutput));
}
private void showErrorMessage(string url, string error)
=> MessageBox.Show($"Failed to process '{url}'. Output:\n\n{error}", "YoutubeDLSharp - ERROR",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}