예제 #1
0
        private void btnDowload_Click(object sender, EventArgs e)
        {
            progressBar.Minimum = 0;
            progressBar.Maximum = 100;
            IEnumerable <VideoInfo> videos = DownloadUrlResolver.GetDownloadUrls(txtUrl.Text);
            VideoInfo video = videos.First(p =>
                                           p.VideoType == VideoType.Mp4 && p.Resolution == Convert.ToInt32(cboxResolution.Text));

            if (video.RequiresDecryption)
            {
                DownloadUrlResolver.DecryptDownloadUrl(video);
            }
            VideoDownloader downloader = new VideoDownloader(video, Path.Combine(txtPath.Text + "\\", video.Title + video.VideoExtension));

            downloader.DownloadProgressChanged += Downloader_DownloadProgressChanged;
            Thread thread = new Thread(() => { downloader.Execute(); })
            {
                IsBackground = true
            };

            thread.Start();
        }
예제 #2
0
        private void SaveVideoToDisk(string youtubeLinkUrl)
        {
            Task.Factory.StartNew(() =>
            {
                var tempPath = FileHelper.GetTempFileName();

                Mp3Model mp3Model;

                using (var outFile = File.OpenWrite(tempPath))
                {
                    using (var videoDownloader = new VideoDownloader(youtubeLinkUrl, outFile))
                    {
                        var destPath = FileHelper.GetMp3FilePath(videoDownloader.CurrentVideo.FullName);

                        mp3Model = new Mp3Model
                        {
                            Name  = Path.GetFileNameWithoutExtension(destPath),
                            Path  = destPath,
                            Url   = youtubeLinkUrl,
                            State = Mp3ModelState.Downloading,
                        };

                        if (File.Exists(mp3Model.Path))
                        {
                            shortToastMessage.ShowInformation(Consts.FileAlreadyExistsInfo);
                            return;
                        }

                        FileHelper.EnsureDirectoryExist(mp3Model.Path);

                        Application.Current.Dispatcher.BeginInvoke(new Action(() => _mp3List.Add(mp3Model)));

                        videoDownloader.ProgressChanged += (s, a) =>
                        {
                            mp3Model.CurrentProgress = a.CurrentProgress;
                            Debug.WriteLine($"{a.CurrentProgress}% of video downloaded");
                        };

                        videoDownloader.Download();
                    }
                }


                mp3Model.State = Mp3ModelState.Converting;
                DispatchService.Invoke(() => shortToastMessage.ShowInformation("Converting..."));

                var converter = new Converter();

                converter.ProgressChanged += (s, a) => mp3Model.CurrentProgress = a.CurrentProgress;


                converter.ExtractAudioMp3FromVideo(tempPath, mp3Model.Path, QualityModel.Quality);

                File.Delete(tempPath);

                DispatchService.Invoke(() =>
                {
                    longToastMessage.ShowSuccess(mp3Model.Name);
                });

                mp3Model.State = Mp3ModelState.Done;
            });
        }
예제 #3
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();
            }
        }
예제 #4
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.";
            }
        }