/// <summary> /// Downloads metadata for the YouTube video with the specified ID. /// </summary> protected virtual async Task <YouTubeMetadata> DownloadMetadata(string id, CancellationToken cancellationToken) { IDictionary <string, string> rawMetadata; Status.Update(null, "Downloading metadata...", WorkStatusType.DownloadingFile); // make http client var client = HttpHelper.CreateClient(); // send http request using (var response = await client.GetAsync(GetYouTubeVideoInfoUri(id), cancellationToken)) { // decode metadata into rawMetadata var content = await response.Content.ReadAsStringAsync(); rawMetadata = YouTubeUtils.ExtractUrlEncodedParamMap(content); } // extract metadata var metadata = new YouTubeMetadata { FormatList = rawMetadata.ContainsKey("fmt_list") ? rawMetadata["fmt_list"] : null, Status = rawMetadata["status"], }; // extract player response var rawPlayerResponse = rawMetadata["player_response"] ?? throw new GrabParseException("Failed to fetch player_response from metadata."); var playerResponse = JToken.Parse(rawPlayerResponse); metadata.PlayerResponse = ExtractPlayerResponseMetadata(playerResponse); // extract muxed streams if (metadata.PlayerResponse.MuxedStreams != null) { metadata.MuxedStreams = metadata.PlayerResponse.MuxedStreams; } if (metadata.MuxedStreams == null) { var urlEncodedFmtStreamMap = rawMetadata.GetOrDefault("url_encoded_fmt_stream_map") ?? throw new GrabParseException("Failed to fetch url_encoded_fmt_stream_map from metadata."); var fmtStreamMap = YouTubeUtils.ExtractUrlEncodedParamList(urlEncodedFmtStreamMap); metadata.MuxedStreams = ExtractMuxedStreamsMetadata(fmtStreamMap); } // extract adaptive streams if (metadata.PlayerResponse.AdaptiveStreams != null) { metadata.AdaptiveStreams = metadata.PlayerResponse.AdaptiveStreams; } if (metadata.AdaptiveStreams == null) { var urlEncodedAdaptiveFormats = rawMetadata["adaptive_fmts"] ?? throw new GrabParseException("Failed to fetch adaptive_fmts from metadata."); var adaptiveFmts = YouTubeUtils.ExtractUrlEncodedParamList(urlEncodedAdaptiveFormats); metadata.AdaptiveStreams = ExtractAdaptiveFormatsMetadata(adaptiveFmts); } return(metadata); }
public void LoadPlaylist(string url) { _youtubeManager.GetPlaylistItems( YouTubeUtils.GetPlaylistIdFromUrl(url) , _maxNumberOfItemsInPlaylist , OnListDataLoaded ); }
void Start() { _playback.OnVideoComplete += OnVideoComplete; if (_videoURL != null && _videoURL != "") { PlayVideo(YouTubeUtils.GetVideoIdFromUrl(_videoURL)); } }
/// <summary> /// Accepts <paramref name="cipher"/> as a URI-encoded string in the following form: /// <para>sp=sig&s=wggj6zg7m-...&url=https%3A%2F%2Fr5---sn-5hnekn7k.googlevideo.com%2Fvideoplayback...</para> /// Extracts its useful encoded parameters and puts them into the specified <paramref name="streamInfo"/>. /// </summary> protected virtual void UpdateStreamCipherInfo(YouTubeStreamInfo streamInfo, string cipher) { if (string.IsNullOrEmpty(cipher)) { throw new ArgumentNullException(nameof(cipher)); } var map = YouTubeUtils.ExtractUrlEncodedParamMap(cipher); streamInfo.Url = map.GetOrDefault("url") ?? throw new GrabParseException("Failed to extract URL from cipher."); streamInfo.Signature = map.GetOrDefault("s") ?? throw new GrabParseException("Failed to extract signature from cipher."); }