-
Notifications
You must be signed in to change notification settings - Fork 74
/
Main.xaml.cs
708 lines (671 loc) · 34.8 KB
/
Main.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
using Libs;
using Libs.Yaml;
using Microsoft.Toolkit.Uwp.Notifications;
using Newtonsoft.Json;
using Swordfish.NET.Collections.Auxiliary;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using WK.Libraries.SharpClipboardNS;
using yt_dlp_gui.Models;
using yt_dlp_gui.Wrappers;
namespace yt_dlp_gui.Views {
public partial class Main :Window {
private readonly ViewData Data = new();
private List<DLP> RunningDLP = new();
public Main() {
InitializeComponent();
DataContext = Data;
//Load Configs
InitGUIConfig();
Topmost = Data.AlwaysOnTop;
if (Data.RememberWindowStatePosition) {
Top = Data.Top;
Left = Data.Left;
}
if (Data.RememberWindowStateSize) {
Width = Data.Width;
Height = Data.Height;
}
//Configuration Checking
InitConfiguration();
//ScanDeps
ScanDepends();
//if `Target` Not exist, default app location
if (!Directory.Exists(Data.TargetPath)) {
Data.TargetPath = App.AppPath;
}
InitClipboard();
//run update check
Task.Run(Inits);
}
private Regex _frgPat = new Regex("<!--StartFragment-->(.*)<!--EndFragment-->", RegexOptions.Multiline | RegexOptions.Compiled);
private Regex _matchUrls = new Regex(@"(https?|ftp|file)\://[A-Za-z0-9\.\-]+(/[A-Za-z0-9\?\&\=;\+!'\(\)\*\-\._~%]*)*", RegexOptions.Compiled);
public void InitClipboard() {
Data.PropertyChanged += (s, e) => {
switch (e.PropertyName) {
case nameof(Data.ClipboardText):
int maxTries = 10;
int delayTime = 1000; // milliseconds
int numTries = 0;
while (numTries < maxTries) {
try {
var content = System.Windows.Clipboard.GetText(System.Windows.TextDataFormat.Html);
if (!string.IsNullOrWhiteSpace(content)) {
content = _frgPat.Match(content).Groups?[1].Value.Trim() ?? "";
} else {
content = System.Windows.Clipboard.GetText(System.Windows.TextDataFormat.Text);
}
var m = _matchUrls.Match(content);
if (m.Success) {
var capUrl = m.Value;
if (Util.UrlVaild(capUrl)) {
Data.Url = capUrl;
Analyze_Start();
}
}
numTries = 0;
break;
} catch (Exception) {
numTries++;
Thread.Sleep(delayTime);
}
}
/*
var content = System.Windows.Clipboard.GetText(System.Windows.TextDataFormat.Html);
if (!string.IsNullOrWhiteSpace(content)) {
content = _frgPat.Match(content).Groups?[1].Value.Trim() ?? "";
} else {
content = System.Windows.Clipboard.GetText(System.Windows.TextDataFormat.Text);
}
var m = _matchUrls.Match(content);
if (m.Success) {
var capUrl = m.Value;
if (Util.UrlVaild(capUrl)) {
Data.Url = capUrl;
Analyze_Start();
}
}
*/
//Debug.WriteLine($"Clipboard Change To: {ClipboardText}");
break;
case nameof(Data.AlwaysOnTop):
Topmost = Data.AlwaysOnTop;
break;
}
};
var sc = new SharpClipboard();
sc.ClipboardChanged += (s, e) => {
if (!Data.IsMonitor || Data.IsAnalyze || Data.IsDownload) return;
if (e.ContentType == SharpClipboard.ContentTypes.Text) {
var text = System.Windows.Clipboard.GetText(System.Windows.TextDataFormat.Text);
Data.ClipboardText = text;
}
};
}
public void InitGUIConfig() {
Data.GUIConfig.Load(App.Path(App.Folders.root, App.AppName + ".yaml"));
Util.PropertyCopy(Data.GUIConfig, Data);
//Loaded and enabled auto save config
Data.AutoSaveConfig = true;
}
public void InitConfiguration() {
Data.Configs.Clear();
Data.Configs.Add(new Config() { name = App.Lang.Main.ConfigurationNone });
var cp = App.Path(App.Folders.configs);
var fs = Directory.Exists(cp)
?Directory.EnumerateFiles(cp).OrderBy(x => x)
:Enumerable.Empty<string>();
fs.ForEach(x => {
Data.Configs.Add(new Config() {
name = Path.GetFileNameWithoutExtension(x),
file = x
});
});
Data.selectedConfig = Data.Configs.FirstOrDefault(x => x.file == Data.GUIConfig.ConfigurationFile, Data.Configs.First());
}
public void ScanDepends() {
var isYoutubeDl = @"^youtube-dl\.exe";
if (!string.IsNullOrWhiteSpace(Data.PathYTDLP) && File.Exists(Data.PathYTDLP)) {
DLP.Path_DLP = Data.PathYTDLP;
}
if (!string.IsNullOrWhiteSpace(Data.PathAria2) && File.Exists(Data.PathAria2)) {
DLP.Path_Aria2 = Data.PathAria2;
}
if (!string.IsNullOrWhiteSpace(Data.PathFFMPEG) && File.Exists(Data.PathFFMPEG)) {
FFMPEG.Path_FFMPEG = Data.PathFFMPEG;
}
if (string.IsNullOrWhiteSpace(DLP.Path_DLP) ||
string.IsNullOrWhiteSpace(DLP.Path_Aria2) ||
string.IsNullOrWhiteSpace(FFMPEG.Path_FFMPEG)) {
var deps = Directory.EnumerateFiles(App.AppPath, "*.exe", SearchOption.AllDirectories).ToList();
deps = deps.Where(x => Path.GetFileName(App.AppExe) != Path.GetFileName(x)).ToList();
var dep_ytdlp = deps.FirstOrDefault(x => Regex.IsMatch(Path.GetFileName(x), @"^(yt-dlp(_min|_x86|_x64)?|ytdl-patched.*?)\.exe"), "");
var dep_ffmpeg = deps.FirstOrDefault(x => Regex.IsMatch(Path.GetFileName(x), @"^ffmpeg"), "");
var dep_aria2 = deps.FirstOrDefault(x => Regex.IsMatch(Path.GetFileName(x), @"^aria2"), "");
var dep_youtubedl = deps.FirstOrDefault(x => Regex.IsMatch(Path.GetFileName(x), isYoutubeDl), "");
if (string.IsNullOrWhiteSpace(DLP.Path_DLP)) {
if (!string.IsNullOrWhiteSpace(dep_ytdlp)) {
Data.PathYTDLP = DLP.Path_DLP = dep_ytdlp;
} else if (!string.IsNullOrWhiteSpace(dep_youtubedl)) {
Data.PathYTDLP = DLP.Path_DLP = dep_youtubedl;
}
}
if (Regex.IsMatch(DLP.Path_DLP, isYoutubeDl)) DLP.Type = DLP.DLPType.youtube_dl;
if (string.IsNullOrWhiteSpace(DLP.Path_Aria2)) {
Data.PathAria2 = DLP.Path_Aria2 = dep_aria2;
}
if (string.IsNullOrWhiteSpace(FFMPEG.Path_FFMPEG)) {
Data.PathFFMPEG = FFMPEG.Path_FFMPEG = dep_ffmpeg;
}
}
}
public async void Inits() {
//check update
var needcheck = false;
var currentDate = DateTimeOffset.UtcNow.ToString("yyyy-MM-dd"); //"";
if (!string.IsNullOrWhiteSpace(Data.LastVersion)) needcheck = true; //not yaml
if (currentDate != Data.LastCheckUpdate) needcheck = true; //cross date
if (needcheck) {
var releaseData = await Web.GetLastTag();
var last = releaseData.FirstOrDefault();
if (last != null) {
Data.ReleaseData = releaseData;
Data.LastVersion = last.tag_name;
Data.LastCheckUpdate = currentDate;
}
}
if (string.Compare(App.CurrentVersion, Data.LastVersion) < 0) {
Data.NewVersion = true;
}
}
private void Button_Analyze(object sender, RoutedEventArgs e) {
Analyze_Start();
}
private void Analyze_Start() {
Data.IsAnalyze = true;
cc.SelectedIndex = -1;
cv.SelectedIndex = -1;
ca.SelectedIndex = -1;
cs.SelectedIndex = -1;
Data.Thumbnail = null;
Data.Video = new();
Data.NeedCookie = Data.UseCookie == UseCookie.Always;
Task.Run(() => {
GetInfo();
Data.IsAnalyze = false;
if (Data.AutoDownloadAnalysed) {
Download_Start();
}
});
}
private void GetInfo() {
//Analyze
var dlp = new DLP(Data.Url);
if (Data.NeedCookie) dlp.Cookie(Data.CookieType);
if (Data.ProxyEnabled && !string.IsNullOrWhiteSpace(Data.ProxyUrl)) dlp.Proxy(Data.ProxyUrl);
dlp.GetInfo();
if (!string.IsNullOrWhiteSpace(Data.selectedConfig.file)) {
dlp.LoadConfig(Data.selectedConfig.file);
}
if (Data.UseOutput) dlp.Output("%(title)s.%(ext)s"); //if not used config, default template
ClearStatus();
dlp.Exec(null, std => {
//取得JSON
Data.Video = JsonConvert.DeserializeObject<Video>(std, new JsonSerializerSettings() {
NullValueHandling = NullValueHandling.Ignore
});
//Reading Chapters
{
Data.Chapters.Clear();
if (Data.Video.chapters != null && Data.Video.chapters.Any()) {
Data.Chapters.Add(new Chapters() { title = App.Lang.Main.ChaptersAll, type = ChaptersType.None });
Data.Chapters.Add(new Chapters() { title = App.Lang.Main.ChaptersSplite, type = ChaptersType.Split });
Data.Chapters.AddRange(Data.Video.chapters);
} else {
Data.Chapters.Add(new Chapters() { title = App.Lang.Main.ChaptersNone, type = ChaptersType.None });
}
//Data.selectedChapter = Data.Chapters.First();
}
//读取 Formats 与 Thumbnails
{
Debug.WriteLine(JsonConvert.SerializeObject(Data.Video.chapters, Formatting.Indented));
Data.Formats.LoadFromVideo(Data.Video.formats);
Data.Thumbnails.Reset(Data.Video.thumbnails);
Data.RequestedFormats.LoadFromVideo(Data.Video.requested_formats);
}
//读取 Subtitles
{
var subs = Data.Video.subtitles.Select(x => {
var s = x.Value.FirstOrDefault(y => y.ext == "vtt");
if (s == null) return null;
s.key = x.Key;
return s;
}).Where(x => x != null).ToList();
Data.Subtitles.Clear();
if (subs.Any()) {
Data.Subtitles.Add(new Subs() { name = App.Lang.Main.SubtitleIgnore });
} else {
Data.Subtitles.Add(new Subs() { name = App.Lang.Main.SubtitleNone });
}
Data.Subtitles.AddRange(subs);
}
var BestUrl = Data.Thumbnails.LastOrDefault()?.url;
//var ThumbUrl = string.Empty;
if (BestUrl != null && Web.Head(BestUrl)) {
Data.Thumbnail = BestUrl;
//ThumbUrl = BestUrl;
} else {
Data.Thumbnail = Data.Video.thumbnail;
//ThumbUrl = Data.Video.thumbnail;
}
//Download Thumb to Temp Folder
/*
Directory.CreateDirectory(App.Path(App.Folders.temp));
var ThumbPath = App.Path(App.Folders.temp, Path.ChangeExtension(Data.Video.id, ".jpg"));
FFMPEG.DownloadUrl(ThumbUrl, ThumbPath);
Data.Thumbnail = ThumbPath;
*/
Data.SelectFormatBest(); //Make ComboBox Selected Item
var full = string.Empty;
if (Path.IsPathRooted(Data.Video._filename)) {
full = Path.GetFullPath(Data.Video._filename);
} else {
full = Path.Combine(Data.TargetPath, Data.Video._filename);
}
//Data.TargetName = GetValidFileName(Data.Video.title) + ".tmp"; //预设挡案名称
Data.TargetName = full; //预设挡案名称
});
dlp.Err(DLP.DLPError.Sign, () => {
if (Data.UseCookie == UseCookie.WhenNeeded) {
Data.NeedCookie = true;
GetInfo();
} else if (Data.UseCookie == UseCookie.Ask) {
var mb = System.Windows.Forms.MessageBox.Show(
"Cookies are required, Use it?\n",
"yt-dlp-gui",
MessageBoxButtons.YesNo);
if (mb == System.Windows.Forms.DialogResult.Yes) {
Data.NeedCookie = true;
GetInfo();
}
}
});
}
private void ClearStatus() {
Data.DNStatus_Infos.Clear();
Data.DNStatus_Video = new();
Data.DNStatus_Audio = new();
Data.VideoPersent = Data.AudioPersent = 0;
}
private Regex regDLP = new Regex(@"^\[yt-dlp]");
private Regex regAria = new Regex(@"(?<=\[#\w{6}).*?(?<downloaded>[\w]+).*?\/(?<total>[\w]+).*?(?<persent>[\w.]+)%.*?CN:(?<cn>\d+).*DL:(?<speed>\w+)(.*?ETA:(?<eta>\w+))?");
private Regex regFF = new Regex(@"frame=.*?(?<frame>\d+).*?fps=.*?(?<fps>[\d.]+).*?size=.*?(?<size>\w+).*?time=(?<time>\S+).*?bitrate=(?<bitrate>\S+)");
private Regex regYTDL = new Regex(@"^\[download\].*?(?<persent>[\d\.]+).*?(?<=of).*?(?<total>\S+).*?(?<=at).*?(?<speed>\S+).*?(?<=ETA).*?(?<eta>\S+)");
private void GetStatus(string std, int chn = 0) {
//Debug.WriteLine(std, "STATUS");
if (regDLP.IsMatch(std)) {
// yt-dlp
if (!Data.DNStatus_Infos.ContainsKey("Downloader")) Data.DNStatus_Infos["Downloader"] = "Native";
var d = std.Split(',');
var s = (chn == 0)?Data.DNStatus_Video : Data.DNStatus_Audio;
if (decimal.TryParse(d[4], out decimal d_total)) {
s.Total = d_total;
s.Persent = decimal.Parse(d[3]) / d_total * 100; ;
} else {
if (decimal.TryParse(d[1].TrimEnd('%'), out decimal d_persent)) {
s.Persent = d_persent;
}
}
s.Downloaded = decimal.Parse(d[3]);
if (decimal.TryParse(d[5], out decimal d_speed)) s.Speed = d_speed;
if (decimal.TryParse(d[6], out decimal d_elapsed)) s.Elapsed = d_elapsed;
if (chn == 0) {
Data.VideoPersent = s.Persent;
} else {
Data.AudioPersent = s.Persent;
}
if (Data.DNStatus_Infos.ContainsKey("Downloader") && Data.DNStatus_Infos["Downloader"] != "Native") return;
Data.DNStatus_Infos["Downloaded"] = Util.GetAutoUnit((long)Data.DNStatus_Video.Downloaded + (long)Data.DNStatus_Audio.Downloaded);
Data.DNStatus_Infos["Total"] = Util.GetAutoUnit((long)Data.DNStatus_Video.Total + (long)Data.DNStatus_Audio.Total);
Data.DNStatus_Infos["Speed"] = Util.GetAutoUnit((long)Data.DNStatus_Video.Speed + (long)Data.DNStatus_Audio.Speed);
Data.DNStatus_Infos["Elapsed"] = Util.SecToStr(Data.DNStatus_Video.Elapsed + Data.DNStatus_Audio.Elapsed);
Data.DNStatus_Infos["Status"] = "Downloading";
} else if (regAria.IsMatch(std)) {
// aria2
if (!Data.DNStatus_Infos.ContainsKey("Downloader")) Data.DNStatus_Infos["Downloader"] = "aria2c";
var d = Util.GetGroup(regAria, std);
if (chn == 0) {
if (decimal.TryParse(d["persent"], out decimal o_persent)) Data.VideoPersent = o_persent;
Data.DNStatus_Infos["Downloaded"] = d["downloaded"];
Data.DNStatus_Infos["Total"] = d["total"];
Data.DNStatus_Infos["Speed"] = d["speed"];
Data.DNStatus_Infos["Elapsed"] = d.GetValueOrDefault("eta", "0s");
Data.DNStatus_Infos["Connections"] = d["cn"];
} else {
if (decimal.TryParse(d["persent"], out decimal o_persent)) Data.AudioPersent = o_persent;
}
Data.DNStatus_Infos["Status"] = "Downloading";
} else if (regFF.IsMatch(std)) {
// ffmpeg
if (!Data.DNStatus_Infos.ContainsKey("Downloader")) Data.DNStatus_Infos["Downloader"] = "FFMPEG";
var d = Util.GetGroup(regFF, std);
Data.DNStatus_Infos["Downloaded"] = d.GetValueOrDefault("size", "");
Data.DNStatus_Infos["Speed"] = d.GetValueOrDefault("bitrate", "");
Data.DNStatus_Infos["Frame"] = d.GetValueOrDefault("frame", "");
Data.DNStatus_Infos["FPS"] = d.GetValueOrDefault("fps", "");
Data.DNStatus_Infos["Time"] = d.GetValueOrDefault("time", "");
Data.DNStatus_Infos["Status"] = "Downloading";
} else if (regYTDL.IsMatch(std)) {
// youtube-dl
if (!Data.DNStatus_Infos.ContainsKey("Downloader")) Data.DNStatus_Infos["Downloader"] = "youtube-dl";
var d = Util.GetGroup(regYTDL, std);
if (chn == 0) {
if (decimal.TryParse(d["persent"], out decimal o_persent)) Data.VideoPersent = o_persent;
} else {
if (decimal.TryParse(d["persent"], out decimal o_persent)) Data.AudioPersent = o_persent;
}
Data.DNStatus_Infos["Total"] = d.GetValueOrDefault("total", "");
Data.DNStatus_Infos["Speed"] = d.GetValueOrDefault("speed", "");
Data.DNStatus_Infos["Elapsed"] = d.GetValueOrDefault("eta", "");
Data.DNStatus_Infos["Status"] = "Downloading";
}
}
private void Button_SaveVideo(object sender, RoutedEventArgs e) {
SaveStream(0);
}
private void Button_SaveAudio(object sender, RoutedEventArgs e) {
SaveStream(1);
}
private void SaveStream(int ch = 0) {
var dialog = new SaveFileDialog();
dialog.InitialDirectory = Path.GetDirectoryName(Data.TargetFile);
dialog.DefaultExt = ch == 0
? Data.selectedVideo.video_ext
: Data.selectedAudio.audio_ext;
dialog.Filter = "MediaFile | *." + dialog.DefaultExt;
dialog.FileName = Path.ChangeExtension(Path.GetFileName(Data.TargetFile), "." + dialog.DefaultExt);
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
var target = Path.ChangeExtension(dialog.FileName, "." + dialog.DefaultExt);
RunningDLP.Clear();
Data.IsDownload = true;
ClearStatus();
Task.Run(() => {
//任務池
List<Task> tasks = new();
tasks.Add(Task.Run(() => {
var dlp = new DLP(Data.Url);
RunningDLP.Add(dlp);
if (!string.IsNullOrWhiteSpace(Data.selectedConfig.file)) dlp.LoadConfig(Data.selectedConfig.file);
if (Data.NeedCookie) dlp.Cookie(Data.CookieType);
if (Data.ProxyEnabled && !string.IsNullOrWhiteSpace(Data.ProxyUrl)) dlp.Proxy(Data.ProxyUrl);
if (Data.UseAria2) dlp.UseAria2();
if (!string.IsNullOrWhiteSpace(Data.LimitRate)) dlp.LimitRate(Data.LimitRate);
dlp.IsLive = Data.Video.is_live;
var vid = ch == 0
?Data.selectedVideo.format_id
:Data.selectedAudio.format_id;
dlp.DownloadFormat(vid, target);
dlp.Exec(std => {
GetStatus(std, ch);
});
}));
//WaitAll Downloads, Merger Video and Audio
Task.WaitAll(tasks.ToArray());
if (!Data.IsAbouted) Data.DNStatus_Infos["Status"] = "Done";
Data.IsDownload = false;
});
}
}
private void Button_ExplorerTarget(object sender, RoutedEventArgs e) {
//Debug.WriteLine(Data.TargetDisplay, "TargetDisplay");
//Debug.WriteLine(Data.TargetFile, "TargetFile");
//Debug.WriteLine(Data.TargetName, "TargetName");
//Debug.WriteLine(Data.TargetPath, "TargetPath");
Util.Explorer(Data.TargetFile);
}
private void Button_Cancel(object sender, RoutedEventArgs e) {
if (Data.IsDownload) {
Data.IsAbouted = true;
foreach (var dlp in RunningDLP) {
dlp.Close();
}
}
}
private void Button_Download(object sender, RoutedEventArgs e) {
Download_Start();
}
private void Download_Start() {
Data.CanCancel = false;
Data.IsAbouted = false;
if (Data.IsDownload) {
Data.IsAbouted = true;
foreach (var dlp in RunningDLP) {
dlp.Close();
}
} else {
var overwrite = true;
RunningDLP.Clear();
//如果檔案已存在
if (File.Exists(Data.TargetFile)) {
var mb = System.Windows.Forms.MessageBox.Show(
"File Already exist. Overwrite it?\n",
"yt-dlp-gui",
MessageBoxButtons.YesNo);
overwrite = mb == System.Windows.Forms.DialogResult.Yes;
if (!overwrite) return; //不要复写
}
Data.IsDownload = true;
//進度更新為0
ClearStatus();
Data.CheckExtension();
var tr = Data.TimeRange.Trim();
var isSingle = false;
if (Data.selectedVideo.type == FormatType.package) isSingle = true;
if (!string.IsNullOrWhiteSpace(tr)) isSingle = true;
if (Data.selectedChapter != null && Data.selectedChapter.type == ChaptersType.Segment) isSingle = true;
Task.Run(() => {
//任務池
List<Task> tasks = new();
var tmp_video_path = string.Empty;
var tmp_audio_path = string.Empty;
//Download Video (or Packaged)
tasks.Add(Task.Run(() => {
var dlp = new DLP(Data.Url);
RunningDLP.Add(dlp);
if (!string.IsNullOrWhiteSpace(Data.selectedConfig.file)) dlp.LoadConfig(Data.selectedConfig.file);
if (Data.NeedCookie) dlp.Cookie(Data.CookieType);
if (Data.ProxyEnabled && !string.IsNullOrWhiteSpace(Data.ProxyUrl)) dlp.Proxy(Data.ProxyUrl);
if (Data.UseAria2) dlp.UseAria2();
if (!string.IsNullOrWhiteSpace(Data.LimitRate)) dlp.LimitRate(Data.LimitRate);
dlp.IsLive = Data.Video.is_live;
var vid = Data.selectedVideo.format_id;
if (!string.IsNullOrWhiteSpace(tr)) {
vid = Data.selectedVideo.format_id + "+" + Data.selectedAudio.format_id;
dlp.DownloadSections(tr);
}
if (Data.selectedChapter != null && Data.selectedChapter.type == ChaptersType.Segment) {
vid = Data.selectedVideo.format_id + "+" + Data.selectedAudio.format_id;
dlp.DownloadSections(Data.selectedChapter.title);
}
tmp_video_path = Path.Combine(App.AppPath, $"{Data.Video.id}.{vid}.{Data.selectedVideo.video_ext}");
if (dlp.IsLive) {
tmp_video_path = Data.TargetFile;
}
dlp.DownloadFormat(vid, tmp_video_path);
Debug.WriteLine("Download Video");
dlp.Exec(std => {
Debug.WriteLine(std, "V");
GetStatus(std, 0);
});
}));
//Download Audio
if (!isSingle) {
tasks.Add(Task.Run(() => {
var dlp = new DLP(Data.Url);
RunningDLP.Add(dlp);
if (!string.IsNullOrWhiteSpace(Data.selectedConfig.file)) dlp.LoadConfig(Data.selectedConfig.file);
if (Data.NeedCookie) dlp.Cookie(Data.CookieType);
if (Data.ProxyEnabled && !string.IsNullOrWhiteSpace(Data.ProxyUrl)) dlp.Proxy(Data.ProxyUrl);
if (Data.UseAria2) dlp.UseAria2();
if (!string.IsNullOrWhiteSpace(Data.LimitRate)) dlp.LimitRate(Data.LimitRate);
dlp.IsLive = Data.Video.is_live;
var aid = Data.selectedAudio.format_id;
tmp_audio_path = Path.Combine(App.AppPath, $"{Data.Video.id}.{aid}.{Data.selectedAudio.audio_ext}");
dlp.DownloadFormat(aid, tmp_audio_path);
Debug.WriteLine("Download Audio");
dlp.Exec(std => {
//Debug.WriteLine(std, "A");
GetStatus(std, 1);
});
}));
}
//Download Subtitle
var subpath = string.Empty;
if (!string.IsNullOrWhiteSpace(Data.selectedSub?.url)) {
Data.SubtitlePersent = 0;
subpath = Path.ChangeExtension(Data.TargetFile, Data.selectedSub.key + ".srt");
FFMPEG.DownloadUrl(Data.selectedSub.url, subpath);
Data.SubtitlePersent = 100;
}
//Download Thumbnail
if (Data.SaveThumbnail) {
if (!string.IsNullOrWhiteSpace(Data.Thumbnail)) {
var thumbpath = Path.ChangeExtension(Data.TargetFile, ".jpg");
FFMPEG.DownloadUrl(Data.Thumbnail, thumbpath);
}
}
//WaitAll Downloads, Merger Video and Audio
Data.CanCancel = true;
Task.WaitAll(tasks.ToArray());
if (!Data.IsAbouted) {
//Download Complete
if (!isSingle) {
if (Data.EmbedSub && File.Exists(subpath)) {
//Subtitle
FFMPEG.Merger(overwrite, Data.TargetFile, tmp_video_path, tmp_audio_path, subpath);
File.Delete(subpath);
} else {
FFMPEG.Merger(overwrite, Data.TargetFile, tmp_video_path, tmp_audio_path);
}
if (File.Exists(tmp_video_path)) File.Delete(tmp_video_path);
if (File.Exists(tmp_audio_path)) File.Delete(tmp_audio_path);
} else {
if (Data.EmbedSub && File.Exists(subpath)) {
//Subtitle
FFMPEG.Merger(overwrite, Data.TargetFile, tmp_video_path, subpath);
File.Delete(subpath);
} else {
File.Move(tmp_video_path, Data.TargetFile, true);
}
if (File.Exists(tmp_video_path)) File.Delete(tmp_video_path);
}
//Splite By Chapters
if (Data.selectedChapter != null && Data.selectedChapter.type == ChaptersType.Split) {
var tar_info = new FileInfo(Data.TargetFile);
var tar_name = Path.GetFileNameWithoutExtension(tar_info.Name);
var tar_path = tar_info.Directory.FullName;
var tar_exts = tar_info.Extension;
var cidx = 0;
foreach (var c in Data.Video.chapters) {
cidx++;
var tar_seg_path = Path.Combine(tar_path, $"{tar_name} - {cidx}{tar_exts}");
Debug.WriteLine(tar_seg_path);
FFMPEG.Split(tar_seg_path, Data.TargetFile, c);
}
if (File.Exists(Data.TargetFile)) File.Delete(Data.TargetFile);
}
Data.DNStatus_Infos["Status"] = "Done";
//Send notification when download completed
try {
if (Data.UseNotifications) {
new ToastContentBuilder()
.AddArgument("conversationId", 2333)
.AddText(Data.Video.title)
.AddText("Video Download Completed!")
.Show();
}
} catch (Exception ex) { }
}
//Clear downloading status
Data.IsDownload = false;
});
}
}
private void Button_Browser(object sender, RoutedEventArgs e) {
if (string.IsNullOrWhiteSpace(Data.TargetName)) {
var dialog = new FolderBrowserDialog();
dialog.SelectedPath = Data.TargetPath;
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
Data.TargetPath = dialog.SelectedPath;
}
} else {
var dialog = new SaveFileDialog();
dialog.InitialDirectory = Path.GetDirectoryName(Data.TargetFile);
dialog.FileName = Path.GetFileName(Data.TargetFile);
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
Data.TargetPath = Path.GetDirectoryName(dialog.FileName);
if ((new string[] { ".mp4", ".webm", ".3gp", ".mkv" }).Any(x => Path.GetExtension(dialog.FileName).ToLower() == x)) {
Data.TargetName = Path.GetFileName(dialog.FileName);
} else {
Data.TargetName = Path.GetFileName(dialog.FileName) + ".tmp";
}
}
}
}
private static Regex RegexValues = new Regex(@"\${(.+?)}", RegexOptions.Compiled);
private string GetValidFileName(string filename) {
var regexSearch = new string(Path.GetInvalidFileNameChars());
return Regex.Replace(filename, string.Format("[{0}]", Regex.Escape(regexSearch)), "_");
}
private void CommandBinding_SaveAs_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e) {
var dialog = new SaveFileDialog();
dialog.InitialDirectory = Path.GetDirectoryName(Data.TargetFile);
dialog.DefaultExt = ".jpg";
dialog.Filter = "JPEG | *.jpg";
dialog.FileName = Path.ChangeExtension(Path.GetFileName(Data.TargetFile), ".jpg");
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
var target = Path.ChangeExtension(dialog.FileName, ".jpg");
FFMPEG.DownloadUrl(Data.Thumbnail, target);
}
}
private void CommandBinding_SaveAs_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e) {
e.CanExecute = !string.IsNullOrWhiteSpace(Data.Thumbnail);
}
private void Button_Subtitle(object sender, RoutedEventArgs e) {
var dialog = new SaveFileDialog();
dialog.InitialDirectory = Path.GetDirectoryName(Data.TargetFile);
dialog.DefaultExt = ".srt";
dialog.Filter = "SubRip | *.srt";
dialog.FileName = Path.ChangeExtension(Path.GetFileName(Data.TargetFile), Data.selectedSub.key + ".srt");
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
var target = Path.ChangeExtension(dialog.FileName, ".srt");
FFMPEG.DownloadUrl(Data.selectedSub.url, target);
}
}
private void MenuItem_About_Click(object sender, RoutedEventArgs e) {
var win = new About();
win.Owner = GetWindow(this);
win.ShowDialog();
}
private void Button_Release(object sender, RoutedEventArgs e) {
var win = new Release();
win.Owner = GetWindow(this);
win.ShowDialog();
}
private void Window_Closed(object sender, EventArgs e) {
Data.Left = Left;
Data.Top = Top;
Data.Width = Width;
Data.Height = Height;
}
}
}