Example #1
0
        private static IEnumerable <VideoInfo> GetVideoInfos(Model model)
        {
            var streamingFormats = GetStreamMap(model);

            streamingFormats.AddRange(GetAdaptiveStreamMap(model));

            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;
                    videoInfo.FileSizeHumanReadable = ContentLengthtoHumanReadable(Convert.ToString(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);
            }
        }
Example #2
0
 internal VideoInfo(VideoInfo info)
     : this(info.FormatCode, info.VideoType, info.Resolution, info.Is3D, info.AudioType, info.AudioBitrate, info.AdaptiveType)
 {
 }