private static YoutubeModel LoadModel(string videoUrl) { var videoId = videoUrl.Replace("https://youtube.com/watch?v=", ""); var url = $"https://www.youtube.com/get_video_info?video_id={videoId}&eurl=https://youtube.googleapis.com/v/{videoId}"; return(YoutubeModel.FromJson(HttpHelper.UrlDecode(HttpHelper.ParseQueryString(HttpHelper.DownloadString(url))["player_response"]))); }
private static YoutubeModel LoadModel(string videoUrl) { var videoId = videoUrl.Replace("https://youtube.com/watch?v=", ""); var url = $"https://www.youtube.com/watch?v={videoId}&gl=US&hl=en&has_verified=1&bpctr=9999999999"; var pageSource = HttpHelper.DownloadString(url); var player_response = string.Empty; if (Regex.IsMatch(pageSource, @"[""\']status[""\']\s*:\s*[""\']LOGIN_REQUIRED")) { url = $"https://www.youtube.com/get_video_info?video_id={videoId}&eurl=https://youtube.googleapis.com/v/{videoId}"; pageSource = HttpHelper.DownloadString(url); player_response = HttpHelper.ParseQueryString(pageSource)["player_response"]; player_response = HttpHelper.UrlDecode(player_response); return(YoutubeModel.FromJson(player_response)); } var dataRegex = new Regex(@"ytplayer\.config\s*=\s*(\{.+?\});", RegexOptions.Multiline); var dataMatch = dataRegex.Match(pageSource); if (dataMatch.Success) { string extractedJson = dataMatch.Result("$1"); if (!extractedJson.Contains("raw_player_response:ytInitialPlayerResponse")) //https://www.youtube.com/watch?v=9Y7TRMISkGE { player_response = JObject.Parse(extractedJson)["args"]["player_response"].ToString(); return(YoutubeModel.FromJson(player_response)); } } dataRegex = new Regex(@"ytInitialPlayerResponse\s*=\s*({.+?})\s*;\s*(?:var\s+meta|</script|\n)", RegexOptions.Multiline); dataMatch = dataRegex.Match(pageSource); if (dataMatch.Success) { player_response = dataMatch.Result("$1"); return(YoutubeModel.FromJson(player_response)); } dataRegex = new Regex(@"ytInitialPlayerResponse\s*=\s*({.+?})\s*;", RegexOptions.Multiline); dataMatch = dataRegex.Match(pageSource); if (dataMatch.Success) { player_response = dataMatch.Result("$1"); return(YoutubeModel.FromJson(player_response)); } ThrowYoutubeParseException(new VideoNotAvailableException("Unable to extract video data"), videoUrl); return(null); }
public static string ToJson(this YoutubeModel self) => JsonConvert.SerializeObject(self, Converter.Settings);
private static string GetVideoTitle(YoutubeModel model) { return(model.VideoDetails.Title.Replace("+", " ")); }
private static IEnumerable <VideoInfo> GetVideoInfos(YoutubeModel model) { var streamingFormats = GetStreamMap(model); if (streamingFormats == null) { streamingFormats = new List <Format>(); } var adaptiveStream = GetAdaptiveStreamMap(model); if (adaptiveStream != null) { streamingFormats.AddRange(adaptiveStream); } foreach (var fmt in streamingFormats) { if (!fmt.Itag.HasValue) { continue; } var itag = (int)fmt.Itag.Value; VideoInfo videoInfo = VideoInfo.Defaults.SingleOrDefault(info => info.FormatCode == itag); videoInfo = videoInfo ?? new VideoInfo(itag); if (!string.IsNullOrEmpty(fmt.Url)) { videoInfo.DownloadUrl = HttpHelper.UrlDecode(HttpHelper.UrlDecode(fmt.Url)); } else if (!string.IsNullOrEmpty(fmt.Cipher) || !string.IsNullOrEmpty(fmt.SignatureCipher)) { IDictionary <string, string> cipher = null; if (!string.IsNullOrEmpty(fmt.Cipher)) { cipher = HttpHelper.ParseQueryString(fmt.Cipher); } if (!string.IsNullOrEmpty(fmt.SignatureCipher)) { cipher = HttpHelper.ParseQueryString(fmt.SignatureCipher); } if (!cipher.ContainsKey("url")) { continue; } if (!cipher.ContainsKey("s")) { continue; } var url = cipher["url"]; var sig = cipher["s"]; url = HttpHelper.UrlDecode(url); url = HttpHelper.UrlDecode(url); sig = HttpHelper.UrlDecode(sig); sig = HttpHelper.UrlDecode(sig); url = url.Replace("&s=", "&sig="); videoInfo.DownloadUrl = HttpHelper.ReplaceQueryStringParameter(url, SignatureQuery, sig); videoInfo.RequiresDecryption = true; } else { continue; } if (!HttpHelper.ParseQueryString(videoInfo.DownloadUrl).ContainsKey(RateBypassFlag)) { videoInfo.DownloadUrl = string.Concat(videoInfo.DownloadUrl, string.Format("&{0}={1}", "ratebypass", "yes")); } if (fmt.AudioSampleRate.HasValue) { videoInfo.AudioBitrate = (int)fmt.AudioSampleRate.Value; } if (fmt.ContentLength.HasValue) { videoInfo.FileSize = (int)fmt.ContentLength.Value; } if (!string.IsNullOrEmpty(fmt.QualityLabel)) { videoInfo.FormatNote = fmt.QualityLabel; } else { videoInfo.FormatNote = fmt.Quality; } if (fmt.Fps.HasValue) { videoInfo.FPS = (int)fmt.Fps.Value; } if (fmt.Height.HasValue) { videoInfo.Height = (int)fmt.Height.Value; } if (fmt.Width.HasValue) { videoInfo.Width = (int)fmt.Width.Value; } // bitrate for itag 43 is always 2147483647 if (itag != 43) { if (fmt.AverageBitrate.HasValue) { videoInfo.AverageBitrate = fmt.AverageBitrate.Value / 1000f; } else if (fmt.Bitrate.HasValue) { videoInfo.AverageBitrate = fmt.Bitrate.Value / 1000f; } } if (fmt.Height.HasValue) { videoInfo.Height = (int)fmt.Height.Value; } if (fmt.Width.HasValue) { videoInfo.Width = (int)fmt.Width.Value; } yield return(videoInfo); } }
private static List <Format> GetStreamMap(YoutubeModel model) { return(model.StreamingData?.Formats.ToList()); }
private static List <Format> GetAdaptiveStreamMap(YoutubeModel model) { return(model.StreamingData.AdaptiveFormats.ToList()); }