Example #1
0
        static void Main(string[] args)
        {
            try
            {
                string url         = args[0];
                string path        = args[1];
                bool   isPlayList  = (url.ToLower().Contains("list"));
                string stringCount = (args.Length == 3) ? args[2] : "0";
                int    count       = int.TryParse(stringCount, out count) ? count : 0; //in case try parse fails
                if (isPlayList)
                {
                    YoutubePlaylist.DownloadPlayListInformationOneAtATime(url, path, count).Wait();
                }
                else
                {
                    YoutubeVideo.DownloadYoutubeInformation(args).Wait();
                }
                Console.WriteLine("Task Completed");
            }
            catch (Exception e)
            {
                Console.WriteLine("Error");
                Console.WriteLine(e);
                Console.WriteLine();
                Console.WriteLine("There was a problem with the inputs. \n" +
                                  "Input ->  Required { url:string path:string } || NOT-Required {count:int} ");

                Console.WriteLine();
                Console.WriteLine("The inputs taken in.");
                for (int i = 0; i < args.Length; i++)
                {
                    Console.WriteLine($"{i} : {args[i]}");
                }
            }
        }
        private void SaveVideoToDisk()
        {
            Task.Factory.StartNew(() =>
            {
                var CurrentFile = new FileHelper();
                var Mp3Model    = new Mp3Model();

                using (var service = Client.For(YouTube.Default))
                {
                    if (IsWholeListChecked)
                    {
                        var youtubePlaylist = new YoutubePlaylist();
                        var playlist        = youtubePlaylist.GetVideosFromPlaylist(YoutubeLinkUrl);

                        if (playlist != null)
                        {
                            foreach (var audio in playlist)
                            {
                                using (var video = service.GetVideo(audio))
                                {
                                    CurrentFile.DefaultTrackName       = video.FullName;
                                    CurrentFile.DefaultTrackPath       = CurrentFile.Path + "\\" + CurrentFile.DefaultTrackName;
                                    CurrentFile.DefaultTrackHiddenPath = CurrentFile.HiddenPath + "\\" + CurrentFile.DefaultTrackName;
                                    CurrentFile.TmpTrackPath           = CurrentFile.PreparePathForFFmpeg(CurrentFile.DefaultTrackHiddenPath);

                                    Mp3Model = new Mp3Model()
                                    {
                                        Name = CurrentFile.CheckVideoFormat(video.FullName, FormatModel.Format),
                                        IsProgressDownloadVisible   = Visibility.Visible,
                                        IsPercentLabelVisible       = Visibility.Visible,
                                        IsConvertingLabelVisible    = Visibility.Hidden,
                                        IsOperationDoneLabelVisible = Visibility.Hidden,
                                        ConvertingLabelText         = Consts.ConvertingPleaseWait,
                                        CurrentProgress             = 0,
                                    };

                                    Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                                    {
                                        this._mp3List.Add(Mp3Model);
                                    }));

                                    using (var outFile = File.OpenWrite(CurrentFile.TmpTrackPath))
                                    {
                                        using (var progressStream = new ProgressStream(outFile))
                                        {
                                            var streamLength = (long)video.StreamLength();

                                            progressStream.BytesMoved += (sender, args) =>
                                            {
                                                Mp3Model.CurrentProgress = args.StreamLength * 100 / streamLength;
                                                Debug.WriteLine($"{Mp3Model.CurrentProgress}% of video downloaded");
                                            };

                                            video.Stream().CopyTo(progressStream);
                                        }
                                    }
                                    BeforeConversion(Mp3Model);
                                    ExtractAudioFromVideo(CurrentFile);
                                    AfterConversion(Mp3Model, CurrentFile);
                                }
                            }
                        }
                    }
                    else
                    {
                        using (var video = service.GetVideo(YoutubeLinkUrl))
                        {
                            CurrentFile.DefaultTrackName       = video.FullName;
                            CurrentFile.DefaultTrackPath       = CurrentFile.Path + "\\" + CurrentFile.DefaultTrackName;
                            CurrentFile.DefaultTrackHiddenPath = CurrentFile.HiddenPath + "\\" + CurrentFile.DefaultTrackName;
                            CurrentFile.TmpTrackPath           = CurrentFile.PreparePathForFFmpeg(CurrentFile.DefaultTrackHiddenPath);

                            Mp3Model = new Mp3Model()
                            {
                                Name = CurrentFile.CheckVideoFormat(video.FullName, FormatModel.Format),
                                IsProgressDownloadVisible   = Visibility.Visible,
                                IsPercentLabelVisible       = Visibility.Visible,
                                IsConvertingLabelVisible    = Visibility.Hidden,
                                IsOperationDoneLabelVisible = Visibility.Hidden,
                                ConvertingLabelText         = Consts.ConvertingPleaseWait,
                                CurrentProgress             = 0,
                            };

                            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                            {
                                this._mp3List.Add(Mp3Model);
                            }));

                            using (var outFile = File.OpenWrite(CurrentFile.TmpTrackPath))
                            {
                                using (var progressStream = new ProgressStream(outFile))
                                {
                                    var streamLength = (long)video.StreamLength();

                                    progressStream.BytesMoved += (sender, args) =>
                                    {
                                        Mp3Model.CurrentProgress = args.StreamLength * 100 / streamLength;
                                        Debug.WriteLine($"{Mp3Model.CurrentProgress}% of video downloaded");
                                    };

                                    video.Stream().CopyTo(progressStream);
                                }
                            }
                            BeforeConversion(Mp3Model);
                            ExtractAudioFromVideo(CurrentFile);
                            AfterConversion(Mp3Model, CurrentFile);
                        }
                    }
                }
            });
        }