/// <summary> /// Adds a downloadItem to the panel /// </summary> /// <param name="item"></param> /// <param name="pnl"></param> public static DownloadItem AddDownloadItem(YoutubeExplode.Models.Video video, Panel pnl) { try { DownloadItem toAddItem = new DownloadItem(video); //Determines where to place the downloadItem int y = 0; if (downloadItems.Count > 0) { y = downloadItems.Count * downloadItems.Where(itm => !itm.IsDisposed).ToList()[0].Height; } pnl.Invoke((MethodInvoker)(() => { pnl.Controls.Add(toAddItem); })); toAddItem.Invoke((MethodInvoker)(() => { toAddItem.Location = new Point(toAddItem.Location.X, y); })); downloadItems.Add(toAddItem); return(toAddItem); } catch (Exception) { MessageFormManager.MakeMessagePopup("Something went wrong!", "Could not find a video with that youtube url!\r\nPlease try again.", 5); } return(null); }
private static async void AddDownloadItems(string url, Panel pnl) { try { YoutubeClient client = new YoutubeClient(); //Get the playlist object from the id YoutubeExplode.Models.Playlist playList = await client.GetPlaylistAsync(url); //We now have a playlist with every video from that playlist. (.Videos) foreach (YoutubeExplode.Models.Video video in playList.Videos) { DownloadItem item = new DownloadItem(video); //Determines where to place the downloadItem int y = 0; if (downloadItems.Count > 0) { y = downloadItems[downloadItems.Count - 1].Location.Y + item.Height; } pnl.Controls.Add(item); item.Location = new Point(item.Location.X, y); downloadItems.Add(item); } } catch (Exception) { MessageFormManager.MakeMessagePopup("Something went wrong!", "Could not find a video with that youtube url!\r\nPlease try again.", 5); } }
private void tmrCheckProgress_Tick(object sender, EventArgs e) { if (convertTask.IsCompleted) { if (!isHistory) { pbLoad.Image = null; pbLoad.BackgroundImage = Properties.Resources.Check; btnDownload.BackgroundImage = Properties.Resources.folderIcon; btnDownload.Image = null; lblStatus.Text = "Done."; isConverting = false; btnDownload.Enabled = true; //Play the sound if (BLSettings.PlaySound) { System.Media.SoundPlayer player; if (File.Exists(BLSettings.SoundFile)) { player = new System.Media.SoundPlayer(BLSettings.SoundFile); } else { player = new System.Media.SoundPlayer(Simple_Youtube2Mp3.Properties.Resources.DefaultNotification); // here WindowsFormsApplication1 is the namespace and Connect is the audio file name } player.Play(); } if (!IsActive(Application.OpenForms["Form1"].Handle) && BLSettings.ShowNotification) //Form currently not on the foreground? show message form! { MessageFormManager.MakeMessagePopup("Download Complete", theVideo.Title + "." + BLSettings.AudioType + " Complete!", 2); } } else { MessageFormManager.MakeMessagePopup("Succesfully Re-Downloaded " + historyRecord.Title + "!", 5); lblStatus.Text = historyRecord.DownloadDate; lblStatus.Location = pbLoad.Location; btnDownload.Enabled = true; pbLoad.Visible = false; pbDownload.Value = 0; } tmrCheckProgress.Stop(); } }
/// <summary> /// Adds a downloadItem to the panel /// </summary> /// <param name="item"></param> /// <param name="pnl"></param> public static DownloadItem AddDownloadItem(string url, Panel pnl) { try { string playlistId = ""; string videoId = ""; YoutubeClient.TryParsePlaylistId(url, out playlistId); YoutubeClient.TryParseVideoId(url, out videoId); //VideoId AND playlistId valid? Then this is a song inside a playlist if (!string.IsNullOrEmpty(playlistId) && !string.IsNullOrEmpty(videoId) && MsgBox.Show("Attention", "This video is part of a playlist. Do you wish to download the entire playlist?", MsgBoxReason.YesNo) == DialogResult.Yes) { AddDownloadItems(playlistId, pnl); } else if (!string.IsNullOrEmpty(videoId)) { DownloadItem toAddItem = new DownloadItem(url); if (downloadItems.Count > 0) { toAddItem.Location = new Point(0, downloadItems[downloadItems.Count - 1].Location.Y + toAddItem.Height); } downloadItems.Add(toAddItem); pnl.Controls.Add(toAddItem); return(toAddItem); } else if (!string.IsNullOrEmpty(playlistId)) { AddDownloadItems(playlistId, pnl); return(null); } } catch (Exception) { MessageFormManager.MakeMessagePopup("Something went wrong!", "Could not find a video with that youtube url!\r\nPlease try again.", 5); } return(null); }
public async void Download() { try { isDownloading = true; if (!canDownload) { return; } pbLoad.Image = null; if (!downloadTask.IsCanceled) { btnDownload.Enabled = false; if (theVideo == null) { lblStatus.Location = pbLoad.Location; MessageFormManager.MakeMessagePopup("Could not download that video!\r\nIt might be unavailable", 6); return; } //Set the image to zzz - enqueue'd. Then, if it can download(if there aren't already a few threads downloading) it will change to downloading... pbLoad.BackgroundImage = Properties.Resources.zzz; lblStatus.Visible = true; lblStatus.Text = "Enqueued."; lblExit.Enabled = false; MediaStreamInfoSet streamInfoSet = await client.GetVideoMediaStreamInfosAsync(theVideo.Id); var streamInfo = streamInfoSet.Muxed.WithHighestVideoQuality(); if (streamInfo == null) { lblStatus.Text = "Error."; pbLoad.Image = null; pbLoad.BackgroundImage = Properties.Resources.error; lblTitle.Text = "Could not download this video"; lblExit.Enabled = true; return; } if (BLSettings.KeepMp4) {//Audio only } else {//Video too } pbLoad.BackgroundImage = null; pbLoad.Image = Properties.Resources.load25x25; lblStatus.Text = "Downloading..."; pathToVideo = GetValidFilePath(BLIO.rootFolder + "\\Video\\", theVideo.Title, "." + BLSettings.VideoType); downloadTask = Task.Run(async() => { lblExit.Invoke((MethodInvoker)(() => { lblExit.Enabled = true; })); ctsDownload.Token.ThrowIfCancellationRequested(); await client.DownloadMediaStreamAsync(streamInfo, pathToVideo, new Progress <double>(d => setProgressbarValue(pbDownload, (int)Math.Round(Convert.ToDouble(d.ToString()) * 100))), ctsDownload.Token); }, ctsDownload.Token); if (!downloadTask.IsCanceled) { await downloadTask; } pbLoad.Image = null; pbLoad.BackgroundImage = Properties.Resources.load25x25; lblStatus.Text = "Preparing..."; ConvertToMp3(pathToVideo); } } catch (System.Threading.Tasks.TaskCanceledException ex) { //Probably a timeout! multiple other threads downloading. gotta wait for them to finish, re-try this one. lblStatus.Invoke((MethodInvoker)(() => { lblStatus.Text = "Failed."; })); Download(); } catch (VideoUnavailableException ex) { if (ex.Message.Contains("Country")) //Video not available in your country --code 150 { MessageFormManager.MakeMessagePopup(theVideo.Title + " Is not available in your country", 7); lblStatus.Invoke((MethodInvoker)(() => { lblStatus.Text = "Failed."; })); } else { //Re-try! lblStatus.Invoke((MethodInvoker)(() => { lblStatus.Text = "Retrying..."; })); Download(); } } catch (HttpRequestException ex) { //Re-try! lblStatus.Invoke((MethodInvoker)(() => { lblStatus.Text = "Retrying..."; })); Download(); } }
private static void ShowError(Exception ex, string message, string description) { MessageFormManager.MakeMessagePopup("Error.", ex.GetType().ToString() + "\r\nWhoops! Something went wrong...\r\n" + message, 8); }