using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Linq.Expressions; using System.Runtime.Remoting.Contexts; using System.Security.AccessControl; using System.Security.Policy; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; namespace ytdlp_gui { public partial class Download : Form { public Download(YTdlpProcess process, string workdir, string convert_to) { this.process = process; this.workdir = workdir; this.convert_to = convert_to; InitializeComponent(); } private async void Download_Load(object sender, EventArgs e) { process.Start += (_sender, start_event) => { lblDownload.Text = String.Format( "Connecting to %s...", start_event.provider ); }; process.Error += (_sender, error_event) => { MessageBox.Show(this, error_event.error, "yt-dlp error", MessageBoxButtons.OK, MessageBoxIcon.Error ); this.Close(); }; process.Progress += (_sender, prog_event) => { int progress = (int)(prog_event.progress * 100 + 0.5); this.Text = String.Format( "Downloading {0}/{1} {2}% - {3}", prog_event.playlist_current, prog_event.playlist_total, progress, prog_event.file ); lblDownload.Text = this.Text; pbDownload.Value = progress; }; process.Finished += (_sender, finish_event) => { Regex regex = new Regex(@"^(.+)\s\[[\w-]+\](\.[\w.]+)$"); Match match = regex.Match(finish_event.file); if (!match.Success) { MessageBox.Show(this, "Could not find the file we just downloaded...", "Uhhh..."); this.Close(); } string newFile = match.Groups[1].Value + match.Groups[2].Value; string filePath = Path.Combine(workdir, finish_event.file); string newFilePath = Path.Combine(workdir, newFile); if (File.Exists(newFilePath)) System.IO.File.Delete(newFilePath); if (convert_to == null || Path.GetExtension(filePath) == convert_to) { System.IO.File.Move(filePath, newFilePath); System.IO.File.SetLastWriteTime(newFilePath, DateTime.Now); } else { newFile = Path.ChangeExtension(newFile, convert_to); newFilePath = Path.ChangeExtension(newFilePath, convert_to); FFmpegProcess convertion = (new FFmpeg()).Convert(filePath, newFilePath); convertion.Progress += (_ffmpeg, prog_event) => { int progress = (int)(prog_event.progress * 100 + 0.5); // this.Text = "Converting - " + progress.ToString() + "%"; lblConvertion.Text = String.Format( "Converting {0}/{1} {2}% - {3}", convertion_current, convertion_total, progress, newFile ); pbConvertion.Value = progress; }; convertion.Finished += (_ffmpeg, _ffmpeg_e) => { System.IO.File.Delete(filePath); triggerConvertionQueue(); }; convertion_queue.Enqueue(convertion); convertion_total += 1; triggerConvertionQueue(); } }; // Can run in background :) _ = await process.Run(); } private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } private void Download_FormClosing(object sender, FormClosingEventArgs e) { process.Cancel(); foreach (FFmpegProcess conversion in convertion_queue) conversion.Cancel(); } protected async void triggerConvertionQueue() { if (converting) return; if (convertion_queue.Count() == 0) return; converting = true; convertion_current += 1; await convertion_queue.Dequeue().Run(); converting = false; } protected readonly YTdlpProcess process; protected int convertion_current = 0; protected int convertion_total = 0; protected Queue convertion_queue = new Queue(); private bool converting = false; protected readonly string workdir; protected readonly string convert_to; } }