Пример #1
0
        static void Main(string[] args)
        {
            if (Console.WindowWidth != 120)
            {
                Console.BufferWidth = 120;
                Console.SetWindowSize(Console.BufferWidth, Console.WindowHeight);
            }
            try
            {
                Console.Write("Enter a Youtube URL: ");
                IEnumerable <VideoInfo>        info         = DownloadUrlResolver.GetDownloadUrls(Console.ReadLine());
                IOrderedEnumerable <VideoInfo> OrderedInfos = info.OrderBy(yes => - yes.Resolution);

                VideoInfo video = OrderedInfos.First();


                if (video.RequiresDecryption)
                {
                    DownloadUrlResolver.DecryptDownloadUrl(video);
                }

                string SanitizedTitle = string.Join("", video.Title.Split(Path.GetInvalidFileNameChars()));
                string savePath       = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), SanitizedTitle + video.VideoExtension);
                var    Downloader     = new VideoDownloader(video, savePath);

                Double PreviousProgress = 0;
                string CurrentSpin      = "|";
                int    SpinCount        = 0;
                Downloader.DownloadStarted         += (s, e) => Console.WriteLine($"Download of {video.Title} started; resolution {video.Resolution}. Saving in {savePath}");
                Downloader.DownloadProgressChanged += (s, e) =>
                {
                    Double LessRoundedPercent = Math.Round(e.ProgressPercentage, 1);
                    Double RoundedPercent     = Math.Round(e.ProgressPercentage);

                    if (LessRoundedPercent != PreviousProgress)
                    {
                        SpinCount++;
                        switch (SpinCount)
                        {
                        case 0: CurrentSpin = "|"; break;

                        case 1: CurrentSpin = "/"; break;

                        case 2: CurrentSpin = "-"; break;

                        case 3: CurrentSpin = "\\"; SpinCount = -1; break;
                        }
                        ;


                        Console.Write("\r{0}         {1}    ", $"Download {RoundedPercent}% complete.", CurrentSpin);
                        PreviousProgress = LessRoundedPercent;
                    }
                };
                Downloader.DownloadFinished += (s, e) => Console.WriteLine($"\nDownload complete... Press any key to continue.");

                Downloader.Execute();

                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred. {ex.Message}");
                Console.ReadKey();
            }
        }
Пример #2
0
        private async void btnDownload_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            string videoURL = txtUrl.Text;
            string YtURL1   = "https://www.youtube.com/";
            string YtURL2   = "youtu.be";
            string fileName = textFName.Text;

            progressBar1.Minimum = 0;
            progressBar1.Maximum = 100;
            if (progressBar1.Value == 100)
            {
                progressBar1.Value = 0;
            }


            StringComparison comp = StringComparison.OrdinalIgnoreCase;

            if (videoURL.Contains(YtURL1) || videoURL.Contains(YtURL2))
            {
                if (radioMP3.Checked || radioMP4.Checked)
                {
                    if (fileName.ContainsSpecialChar() || "" == fileName)
                    {
                        labelStatus.Text = "File Name is invalid.";
                    }
                    else
                    {
                        using (FolderBrowserDialog fbd = new FolderBrowserDialog()
                        {
                            Description = "Select your path."
                        })
                        {
                            if (fbd.ShowDialog() == DialogResult.OK)
                            {
                                IEnumerable <VideoInfo> videos = DownloadUrlResolver.GetDownloadUrls(videoURL);
                                Cursor.Current = Cursors.Default;

                                if (radioMP3.Checked)
                                {
                                    try
                                    {
                                        Cursor.Current = Cursors.WaitCursor;
                                        VideoInfo video = videos
                                                          .Where(info => info.AudioBitrate > 0 && info.AdaptiveType == AdaptiveType.Audio)
                                                          .OrderByDescending(info => info.AudioBitrate).First();

                                        if (video.RequiresDecryption)
                                        {
                                            DownloadUrlResolver.DecryptDownloadUrl(video);
                                        }

                                        VideoDownloader downloader = new VideoDownloader(video, Path.Combine(fbd.SelectedPath + "/" + fileName + ".mp3"));

                                        downloader.DownloadProgressChanged += Downloader_DownloadProgressChanged;

                                        Cursor.Current = Cursors.Default;

                                        Thread thread = new Thread(() =>
                                        {
                                            try
                                            {
                                                downloader.Execute();
                                            }
                                            catch (System.Net.WebException ex)
                                            {
                                                MessageBox.Show("Sorry, this video is not downloadable.");
                                            }
                                        })
                                        {
                                            IsBackground = true
                                        };
                                        thread.Start();
                                    }
                                    catch (System.InvalidOperationException ex)
                                    {
                                        MessageBox.Show("Sorry, this video is not convertable.");
                                    }
                                }

                                else if (radioMP4.Checked)
                                {
                                    Cursor.Current = Cursors.WaitCursor;
                                    bool validReso = false;
                                    foreach (var vid in videos)
                                    {
                                        if (Convert.ToInt32(comboReso.Text) == vid.Resolution)
                                        {
                                            validReso = true;
                                        }
                                    }
                                    if (validReso)
                                    {
                                        try
                                        {
                                            VideoInfo video = videos.First(p => p.VideoType == VideoType.Mp4 && p.Resolution == Convert.ToInt32(comboReso.Text));

                                            if (video.RequiresDecryption)
                                            {
                                                DownloadUrlResolver.DecryptDownloadUrl(video);
                                            }

                                            VideoDownloader downloader = new VideoDownloader(video, Path.Combine(fbd.SelectedPath + "/" + fileName + ".mp4"));

                                            downloader.DownloadProgressChanged += Downloader_DownloadProgressChanged;

                                            Cursor.Current = Cursors.Default;

                                            Thread thread = new Thread(() =>
                                            {
                                                try
                                                {
                                                    downloader.Execute();
                                                }
                                                catch (System.Net.WebException ex)
                                                {
                                                    MessageBox.Show("Sorry, this video is not downloadable.");
                                                }
                                            })
                                            {
                                                IsBackground = true
                                            };
                                            thread.Start();
                                        }
                                        catch (System.InvalidOperationException ex)
                                        {
                                            MessageBox.Show("Sorry, this video is not downloadable.");
                                        }
                                    }
                                    else
                                    {
                                        labelStatus.Text = "Choose another resolution.";
                                    }
                                    Cursor.Current = Cursors.Default;
                                }
                            }
                        }
                    }
                }
                else
                {
                    labelStatus.Text = "You must select format first.";
                }
            }
            else if (videoURL == "")
            {
                labelStatus.Text = "You must enter video URL.";
            }
            else
            {
                labelStatus.Text = "Video URL is invalid.";
            }
        }