Example #1
0
 private void Session_DownloadCompleted(VideoDownloadSession sender, DownloadCompletedEventArgs e)
 {
     currentDownloadSession = null;
     sender.Dispose();
     if (!e.Cancelled)
     {
         if (e.Error != null)
         {
             this.Dispatcher.BeginInvoke(new Session_ProgressChangedA(() =>
             {
                 this.SetValue(IsYoutubeDownloadingProperty, false);
                 MessageBox.Show(this, e.Error.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
             }));
         }
         else
         {
             this.Dispatcher.BeginInvoke(new Session_ProgressChangedA(() =>
             {
                 this.SetValue(IsYoutubeDownloadingProperty, false);
                 if (MessageBox.Show(this, $"Do you want to open output folder?", "Prompt", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                 {
                     Process.Start("explorer.exe", $"/select,\"{e.DownloadDestination}\"");
                 }
             }));
         }
     }
     else
     {
         this.Dispatcher.BeginInvoke(new Session_ProgressChangedA(() => this.SetValue(IsYoutubeDownloadingProperty, false)));
     }
 }
Example #2
0
 private void Session_ProgressChanged(VideoDownloadSession sender, double e)
 {
     this.Dispatcher.BeginInvoke(new Session_ProgressChangedA(() =>
     {
         this.SetValue(YoutubeDownloadingProgressProperty, e);
     }));
 }
Example #3
0
        private async void ButtonYoutubeLink_Click(object sender, RoutedEventArgs e)
        {
            string youtubeurl   = this.TextBoxYoutubeLink.Text;
            bool   disablefixup = (this.disableFixup.IsChecked == true);

            if (currentDownloadSession != null || string.IsNullOrWhiteSpace(youtubeurl))
            {
                return;
            }

            youtubeurl = youtubeurl.Trim();

            this.SetValue(YoutubeDownloadingTextProperty, "Preparing");
            this.SetValue(IsYoutubeDownloadingIndeterminateProperty, true);
            this.SetValue(IsYoutubeDownloadingProperty, true);

            YoutubeVideoInfo something;

            if (!this.cache_youtubeinfo.TryGetValue(youtubeurl, out something))
            {
                try
                {
                    something = await this.youtubeTool.GetYoutubeVideoInformationAsync(youtubeurl);

                    this.cache_youtubeinfo.Add(youtubeurl, something);
                }
                catch (Exception ex)
                {
                    this.SetValue(IsYoutubeDownloadingProperty, false);
                    MessageBox.Show(this, ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }

            var formatStrings = new List <string>(something.Formats.Count + 1);
            Dictionary <string, YoutubeVideoFormat> filterData = new Dictionary <string, YoutubeVideoFormat>(StringComparer.OrdinalIgnoreCase);

            await Task.Run(() =>
            {
                StringBuilder sb = new StringBuilder(160);
                List <YoutubeVideoFormat> audioOnly = new List <YoutubeVideoFormat>(),
                videoOnly = new List <YoutubeVideoFormat>(),
                video     = new List <YoutubeVideoFormat>();

                for (int i = 0; i < something.Formats.Count; i++)
                {
                    var format    = something.Formats[i];
                    bool hasAudio = !string.IsNullOrEmpty(format.AudioCodec),
                    hasVideo      = !string.IsNullOrEmpty(format.VideoCodec);
                    if (hasAudio && hasVideo)
                    {
                        video.Add(format);
                    }
                    else
                    {
                        if (hasVideo)
                        {
                            videoOnly.Add(format);
                        }
                        else
                        {
                            audioOnly.Add(format);
                        }
                    }
                }
                audioOnly.Sort(YoutubeVideoFormatComparer.Revert);
                videoOnly.Sort(YoutubeVideoFormatComparer.Revert);
                video.Sort(YoutubeVideoFormatComparer.Revert);

                Action <YoutubeVideoFormat> handleData = (format) =>
                {
                    string formatName = YoutubeVideoFormatComparer.GetFriendlyFormatExtension(format);
                    sb.Append(formatName);
                    sb.Append(" ");
                    bool hasAudio = (format.AudioCodec != null),
                    hasVideo      = (format.VideoCodec != null);
                    if (hasVideo && hasAudio)
                    {
                        var theHeight = format.VideoResolution.Height;
                        if (theHeight == 0)
                        {
                            sb.Append(format.FormatNote);
                        }
                        else
                        {
                            sb.Append(theHeight);
                            sb.Append("p");
                            if (format.FPS.HasValue && format.FPS.Value != 30)
                            {
                                sb.Append(format.FPS.Value);
                            }
                        }

                        sb.Append(" Video");
                    }
                    else
                    {
                        if (hasAudio)
                        {
                            // Audio only
                            sb.Append(format.AudioBirate);
                            sb.Append("k [Audio Only]");
                        }
                        else
                        {
                            // Video only
                            var theHeight = format.VideoResolution.Height;
                            if (theHeight == 0)
                            {
                                sb.Append(format.FormatNote);
                            }
                            else
                            {
                                sb.Append(theHeight);
                                sb.Append("p");
                                if (format.FPS.HasValue && format.FPS.Value != 30)
                                {
                                    sb.Append(format.FPS.Value);
                                }
                            }
                            sb.Append(" [Video Only]");
                        }
                    }

                    if (string.Equals(format.Protocol, Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase) || string.Equals(format.Protocol, Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase))
                    {
                        if (formatName == "Vorbis")
                        {
                            sb.Append(" (*.ogg)|*.ogg");
                        }
                        else if (formatName == "Opus")
                        {
                            sb.Append(" (*.opus)|*.opus");
                        }
                        else if (format.FileExtension.Equals("webm", StringComparison.OrdinalIgnoreCase))
                        {
                            sb.Append(" (*.mkv)|*.mkv");
                        }
                        else
                        {
                            sb.Append(" (*.");
                            sb.Append(format.FileExtension);
                            sb.Append(")|*.");
                            sb.Append(format.FileExtension);
                        }
                    }
                    else
                    {
                        sb.Append(" (*ts).|*.ts");
                    }

                    string filterName = sb.ToString();
                    sb.Clear();
                    formatStrings.Add(filterName);
                    filterData[filterName] = format;
                };

                foreach (var format in video)
                {
                    handleData(format);
                }
                foreach (var format in videoOnly)
                {
                    handleData(format);
                }
                foreach (var format in audioOnly)
                {
                    handleData(format);
                }
            });

            sfd.Filter   = string.Join("|", formatStrings);
            sfd.Title    = "Download '" + something.Title + "'";
            sfd.FileName = this.regex_InvalidPathCharacters.Replace(something.Title + "-" + something.VideoID, "-").Normalize();

            if (sfd.ShowDialog(this) == true)
            {
                string formatString = formatStrings[sfd.FilterIndex - 1];
                if (filterData.TryGetValue(formatString, out var selectedFormat))
                {
                    this.Session_ProgressChanged(null, 0);
                    this.SetValue(IsYoutubeDownloadingIndeterminateProperty, false);
                    if (string.Equals(selectedFormat.Protocol, Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase) || string.Equals(selectedFormat.Protocol, Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase))
                    {
                        this.SetValue(YoutubeDownloadingTextProperty, $"Downloading: {formatString}\nTo: {sfd.FileName}");
                    }
                    else
                    {
                        this.SetValue(YoutubeDownloadingTextProperty, $"Streaming: {formatString}\nTo: {sfd.FileName}");
                    }
                    currentDownloadSession = this.youtubeTool.PrepareVideoDownload(selectedFormat);
                    currentDownloadSession.DisableFixup       = disablefixup;
                    currentDownloadSession.ProgressChanged   += Session_ProgressChanged;
                    currentDownloadSession.DownloadCompleted += Session_DownloadCompleted;
                    try
                    {
                        currentDownloadSession.StartDownload(sfd.FileName);
                    }
                    catch (Exception ex)
                    {
                        currentDownloadSession.Dispose();
                        currentDownloadSession = null;
                        this.SetValue(IsYoutubeDownloadingProperty, false);
                        MessageBox.Show(this, ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
                else
                {
                    this.SetValue(IsYoutubeDownloadingProperty, false);
                    this.cache_youtubeinfo.Remove(youtubeurl);
                    MessageBox.Show(this, "Unknown error occured. Please try again after a moment.", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                }
            }
            else
            {
                this.SetValue(IsYoutubeDownloadingProperty, false);
            }
        }