Exemplo n.º 1
0
        private async Task <List <FileDownloadingInfo> > InitDownloadingAsync(IList <Video> videos, string directory, DownloadQuality quality, string audioExtForced)
        {
            List <FileDownloadingInfo> result = new List <FileDownloadingInfo>();

            //_client.DownloadClosedCaptionTrackAsync();

            //ThreadPool.SetMaxThreads(Environment.ProcessorCount, Environment.ProcessorCount);

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            foreach (var vid in videos)
            {
                try
                {
                    StreamManifest infoSet = await _client.Videos.Streams.GetManifestAsync(vid.Id);

                    string vidTitle = RemoveProhibitedChars(vid.Title);

                    switch (quality)
                    {
                    case DownloadQuality.Default:
                    case DownloadQuality.MuxedBest:

                        IEnumerable <MuxedStreamInfo> muxedStreams = infoSet.GetMuxedStreams();
                        IVideoStreamInfo muxedHighestQuality       = muxedStreams.GetWithHighestVideoQuality();
                        string           ext = muxedHighestQuality.Container.Name;
                        //string ext = "mkv";
                        string path = directory + "/" + vidTitle + "." + ext;

                        var file = new FileDownloadingInfo()
                        {
                            Name = vidTitle, StreamInfo = muxedHighestQuality
                        };
                        InitStreamDownloading(file, path, vid);
                        result.Add(file);
                        break;

                    case DownloadQuality.SeparateBest:

                        IEnumerable <IVideoStreamInfo> videoStreams = infoSet.GetVideoStreams();
                        IEnumerable <IAudioStreamInfo> audioStreams = infoSet.GetAudioStreams();
                        IStreamInfo highestBitRate      = audioStreams.GetWithHighestBitrate();
                        IStreamInfo videoHighestQuality = videoStreams.GetWithHighestVideoQuality();

                        string extVideo = videoHighestQuality.Container.Name;

                        string pathVideo = directory + "/" + vidTitle + "." + extVideo;

                        string extAudio  = highestBitRate.Container.Name;
                        string pathAudio = directory + "/" + vidTitle + "." + extAudio;

                        if (audioExtForced == String.Empty)
                        {
                            if (pathAudio.Equals(pathVideo))
                            {
                                pathAudio += ".audio." + extAudio;
                            }
                        }
                        else
                        {
                            pathAudio += "." + audioExtForced;
                        }

                        FileDownloadingInfo audio = new FileDownloadingInfo()
                        {
                            Name = vidTitle + "(audio)", StreamInfo = highestBitRate
                        };
                        FileDownloadingInfo video = new FileDownloadingInfo()
                        {
                            Name       = vidTitle,
                            StreamInfo = videoHighestQuality
                        };

                        if (File.Exists(pathAudio))
                        {
                            _logger.Log("File " + pathAudio + "already exists!. Consider removing or renaming.");
                        }
                        else
                        {
                            InitStreamDownloading(audio, pathAudio, vid);
                            result.Add(audio);
                        }

                        if (File.Exists(pathVideo))
                        {
                            _logger.Log("File " + pathVideo + "already exists!. Consider removing or renaming.");
                        }
                        else
                        {
                            InitStreamDownloading(video, pathVideo, vid);
                            result.Add(video);
                        }

                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(quality), quality, null);
                    }
                }
                catch (Exception ex)
                {
                    _logger.Log(ex.InnerException?.Message);
                }
            }

            return(result);
        }