private async Task WebClient_DownloadVideo(VideoInfo videoInfo) { if (videoInfo.RequiresDecryption) { DownloadUrlResolver.DecryptDownloadUrl(videoInfo); } var savePath = Path.Combine(SaveToTextBox.Text, videoInfo.Title.RemoveIllegalPathCharacters() + videoInfo.VideoExtension); if (File.Exists(savePath)) { if (!OverrideExistingFileCheckBox.Checked) { return; } File.Delete(savePath); } var client = new WebClient(); client.DownloadProgressChanged += (sender, args) => { var progress = (int)(args.BytesReceived / args.TotalBytesToReceive * 100); if (progress > DowloadCurrentProgressBar.Value) { DowloadCurrentProgressBar.Value = progress; DowloadCurrentProgressBar.DrawText(videoInfo.Title); } }; client.DownloadFileCompleted += (sender, args) => { DowloadCurrentProgressBar.Value = 100; }; await client.DownloadFileTaskAsync(new Uri(videoInfo.DownloadUrl), savePath); }
private void DownloadItem(DownloadItemSettings settings, Action <string> writeLogOnError) { LogTextBox.Text = string.Empty; var urls = new List <string>(); if (settings.Url.ChannelMode()) { urls = settings.Url.GetChannelItemsUrls(); } else if (settings.Url.ListMode()) { urls = settings.Url.GetPlaylistItemsUrls(); } else { urls.Add(settings.Url); } var maxResolution = ((KeyValuePair <string, int>)MaxQualityComboBox.SelectedItem).Value; DowloadTotalProgressBar.Maximum = urls.Count; DowloadTotalProgressBar.Value = 0; var index = 0; foreach (var url in urls) { ++index; var count = index + " of " + urls.Count; var videoInfos = new List <VideoInfo>(); try { videoInfos = DownloadUrlResolver.GetDownloadUrls(url, false) .Where(i => i.VideoType == VideoType.Mp4 && i.AudioType != AudioType.Unknown) .OrderByDescending(i => i.Resolution).ToList(); var videoInfo = videoInfos.FirstOrDefault(i => i.Resolution <= maxResolution); if (videoInfo != null) { DowloadTotalProgressBar.Value = index; DowloadTotalProgressBar.DrawText(count + " Downloading."); DowloadCurrentProgressBar.Value = 0; //var task = WebClient_DownloadVideo(videoInfo); //task.Wait(); DownloadVideo(videoInfo, progress => { DowloadCurrentProgressBar.Value = progress; DowloadCurrentProgressBar.DrawText(videoInfo.Title); }); DowloadCurrentProgressBar.Value = 100; } } catch (Exception ex) { var error = count + " - " + url; if (videoInfos.Any()) { error += " - " + videoInfos.First().Title; } error += Environment.NewLine; error += ex.Message + Environment.NewLine; if (ex.Message.Contains("403")) { error += "Looks this video contains content which is restricted from playback on certain sites or applications.Try to use https://www.clipconverter.cc/" + Environment.NewLine; } //if (videoInfos.Any()) //{ // foreach (var video in videoInfos) // { // if (video.RequiresDecryption) // { // DownloadUrlResolver.DecryptDownloadUrl(video); // } // } // error += "Possible download urls: " + Environment.NewLine + // string.Join(Environment.NewLine, videoInfos.Select(v => v.DownloadUrl + v.VideoExtension)) + Environment.NewLine; //} writeLogOnError?.Invoke(error); } } DowloadTotalProgressBar.Value = DowloadTotalProgressBar.Maximum; DowloadTotalProgressBar.DrawText("Download compleated!"); DowloadTotalProgressBar.Refresh(); }