コード例 #1
0
        private async Task <StreamContext> GetStreamContextFromWatchPageAsync(VideoId videoId)
        {
            var watchPage = await WatchPage.GetAsync(_httpClient, videoId);

            var playerConfig = watchPage.TryGetPlayerConfig();

            var playerResponse =
                playerConfig?.GetPlayerResponse() ??
                watchPage.TryGetPlayerResponse() ??
                throw VideoUnplayableException.Unplayable(videoId);

            var previewVideoId = playerResponse.TryGetPreviewVideoId();

            if (!string.IsNullOrWhiteSpace(previewVideoId))
            {
                throw VideoRequiresPurchaseException.Preview(videoId, previewVideoId);
            }

            var playerSourceUrl = watchPage.TryGetPlayerSourceUrl() ?? playerConfig?.GetPlayerSourceUrl();
            var playerSource    = !string.IsNullOrWhiteSpace(playerSourceUrl)
                ? await PlayerSource.GetAsync(_httpClient, playerSourceUrl)
                : null;

            var cipherOperations = playerSource?.GetCipherOperations().ToArray() ?? Array.Empty <ICipherOperation>();

            if (!playerResponse.IsVideoPlayable())
            {
                throw VideoUnplayableException.Unplayable(videoId, playerResponse.TryGetVideoPlayabilityError());
            }

            if (playerResponse.IsLive())
            {
                throw VideoUnplayableException.LiveStream(videoId);
            }

            var streamInfoProviders = new List <IStreamInfoProvider>();

            // Streams from player config
            if (playerConfig != null)
            {
                streamInfoProviders.AddRange(playerConfig.GetStreams());
            }

            // Streams from player response
            streamInfoProviders.AddRange(playerResponse.GetStreams());

            // Streams from DASH manifest
            var dashManifestUrl = playerResponse.TryGetDashManifestUrl();

            if (!string.IsNullOrWhiteSpace(dashManifestUrl))
            {
                var dashManifest = await GetDashManifestAsync(dashManifestUrl, cipherOperations);

                streamInfoProviders.AddRange(dashManifest.GetStreams());
            }

            return(new StreamContext(streamInfoProviders, cipherOperations));
        }
コード例 #2
0
        /// <summary>
        /// Gets the HTTP Live Stream (HLS) manifest URL for the specified video (if it's a live video stream).
        /// </summary>
        public async Task <string> GetHttpLiveStreamUrlAsync(VideoId videoId)
        {
            var videoInfoResponse = await VideoInfoResponse.GetAsync(_httpClient, videoId);

            var playerResponse = videoInfoResponse.GetPlayerResponse();

            if (!playerResponse.IsVideoPlayable())
            {
                throw VideoUnplayableException.Unplayable(videoId, playerResponse.TryGetVideoPlayabilityError());
            }

            return(playerResponse.TryGetHlsManifestUrl() ??
                   throw VideoUnplayableException.NotLiveStream(videoId));
        }