示例#1
0
        static IEnumerable <IStreamInfo> GetOptimalStreams(StreamManifest streamManifest, Container container)
        {
            if (streamManifest.GetAudioOnlyStreams().Any() && streamManifest.GetVideoOnlyStreams().Any())
            {
                // Include audio stream
                // Priority: transcoding -> bitrate
                yield return(streamManifest
                             .GetAudioOnlyStreams()
                             .OrderByDescending(s => s.Container == container)
                             .ThenByDescending(s => s.Bitrate)
                             .First());

                // Include video stream
                if (!container.IsAudioOnly)
                {
                    // Priority: video quality -> transcoding
                    yield return(streamManifest
                                 .GetVideoOnlyStreams()
                                 .OrderByDescending(s => s.VideoQuality)
                                 .ThenByDescending(s => s.Container == container)
                                 .First());
                }
            }
            // Use single muxed stream if adaptive streams are not available
            else
            {
                // Priority: video quality -> transcoding
                yield return(streamManifest
                             .GetMuxedStreams()
                             .OrderByDescending(s => s.VideoQuality)
                             .ThenByDescending(s => s.Container == container)
                             .First());
            }
        }
        private async Task <bool> DownloadTrailerAsync(Movie movie)
        {
            try
            {
                YoutubeClient  youtube        = new YoutubeClient();
                StreamManifest streamManifest = await youtube.Videos.Streams.GetManifestAsync(movie.TrailerURL);

                // Get highest quality muxed stream
                IVideoStreamInfo streamInfo = streamManifest.GetMuxedStreams().GetWithHighestVideoQuality();

                if (streamInfo != null)
                {
                    // Download the stream to file
                    await youtube.Videos.Streams.DownloadAsync(streamInfo, Path.Combine(movie.FilePath, $"{movie.Title} ({movie.Year})-trailer.{streamInfo.Container}"));

                    return(true);
                }

                return(false);
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error downloading trailer for {movie.Title}\n{ex.Message}");
                await _hubContext.Clients.All.SendAsync("downloadAllTrailers", movie);

                return(false);
            }
        }
示例#3
0
        private Utils.VideoSettings GetVideoSettings(VideoId videoId)
        {
            StreamManifest manifest = client.Videos.Streams.GetManifestAsync(videoId).Result;

            Utils.VideoSettings videoSettings = new Utils.VideoSettings()
            {
                StreamManifest      = manifest,
                MuxedStreamInfo     = manifest.GetMuxedStreams().ToList(),
                VideoOnlyStreamInfo = manifest.GetVideoOnlyStreams().ToList(),
                AudioOnlyStreamInfo = manifest.GetAudioOnlyStreams().ToList()
            };

            return(videoSettings);
        }
        private static IEnumerable <IStreamInfo> GetBestMediaStreamInfos(
            StreamManifest streamManifest,
            ConversionFormat format)
        {
            // Use single muxed stream if adaptive streams are not available
            if (!streamManifest.GetAudioOnlyStreams().Any() || !streamManifest.GetVideoOnlyStreams().Any())
            {
                // Priority: video quality -> transcoding
                yield return(streamManifest
                             .GetMuxedStreams()
                             .OrderByDescending(s => s.VideoQuality)
                             .ThenByDescending(s => !IsTranscodingRequired(s.Container, format))
                             .First());

                yield break;
            }

            // Include audio stream
            // Priority: transcoding -> bitrate
            yield return(streamManifest
                         .GetAudioOnlyStreams()
                         .OrderByDescending(s => !IsTranscodingRequired(s.Container, format))
                         .ThenByDescending(s => s.Bitrate)
                         .First());

            // Include video stream
            if (!format.IsAudioOnly)
            {
                // Priority: video quality -> transcoding
                yield return(streamManifest
                             .GetVideoOnlyStreams()
                             .OrderByDescending(s => s.VideoQuality)
                             .ThenByDescending(s => !IsTranscodingRequired(s.Container, format))
                             .First());
            }
        }
        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);
        }