public List<YouTubeVideoQuality> GetYouTubeVideoUrls(string VideoUrl)
        {
            var list = new List<YouTubeVideoQuality>();

            var id = GetVideoIDFromUrl(VideoUrl);
            var infoUrl = string.Format("http://www.youtube.com/get_video_info?&video_id={0}&el=detailpage&ps=default&eurl=&gl=US&hl=en", id);
            var infoText = new WebClient().DownloadString(infoUrl);
            var infoValues = HttpUtility.ParseQueryString(infoText);
            if (infoValues["errorcode"] == "150")
            {
                //Age restriction error, cant download
                return null;
            }
            var title = infoValues["title"];
            var videoDuration = infoValues["length_seconds"];
            var videos = infoValues["url_encoded_fmt_stream_map"].Split(',');
            foreach (var item in videos)
            {
                try
                {
                    var data = HttpUtility.ParseQueryString(item);
                    var server = Uri.UnescapeDataString(data["fallback_host"]);
                    var signature = data["sig"] ?? data["signature"] ?? data["s"];   // Hans: Added "s" for encrypted signatures
                    var url = Uri.UnescapeDataString(data["url"]) + "&fallback_host=" + server;

                    if (!string.IsNullOrEmpty(signature) && data["s"] == null)
                    {
                        url += "&signature=" + signature;
                    }

                    // If the download-URL contains encrypted signature
                    if (data["s"] != null)
                    {
                        string html = helper.DownloadWebPage(VideoUrl);
                        string Player_Version = Regex.Match(html, @"""\\/\\/s.ytimg.com\\/yts\\/jsbin\\/html5player-(.+?)\.js""").Groups[1].ToString();

                        // Decrypt the signature
                        string decryptedSignature = GetDecipheredSignature(Player_Version, signature.ToString());

                        // The new download-url with decrypted signature
                        url += "&signature=" + decryptedSignature;
                    }

                    var size = getSize(url);
                    var videoItem = new YouTubeVideoQuality();
                    videoItem.DownloadUrl = url;
                    videoItem.VideoSize = size;
                    videoItem.VideoTitle = title;
                    var tagInfo = new ITagInfo(Uri.UnescapeDataString(data["itag"]));
                    videoItem.Dimension = tagInfo.VideoDimensions;
                    videoItem.Extention = tagInfo.VideoExtentions;
                    videoItem.Length = long.Parse(videoDuration);
                    list.Add(videoItem);
                }
                catch { }
            }

            return list;
        }
Beispiel #2
0
        public List <YouTubeVideoQuality> GetYouTubeVideoUrls(string VideoUrl)
        {
            var list = new List <YouTubeVideoQuality>();

            var id         = GetVideoIDFromUrl(VideoUrl);
            var infoUrl    = string.Format("http://www.youtube.com/get_video_info?&video_id={0}&el=detailpage&ps=default&eurl=&gl=US&hl=en", id);
            var infoText   = new WebClient().DownloadString(infoUrl);
            var infoValues = HttpUtility.ParseQueryString(infoText);

            if (infoValues["errorcode"] == "150")
            {
                //Age restriction error, cant download
                return(null);
            }
            var title         = infoValues["title"];
            var videoDuration = infoValues["length_seconds"];
            var videos        = infoValues["url_encoded_fmt_stream_map"].Split(',');

            foreach (var item in videos)
            {
                try
                {
                    var data      = HttpUtility.ParseQueryString(item);
                    var server    = Uri.UnescapeDataString(data["fallback_host"]);
                    var signature = data["sig"] ?? data["signature"] ?? data["s"];   // Hans: Added "s" for encrypted signatures
                    var url       = Uri.UnescapeDataString(data["url"]) + "&fallback_host=" + server;

                    if (!string.IsNullOrEmpty(signature) && data["s"] == null)
                    {
                        url += "&signature=" + signature;
                    }

                    // If the download-URL contains encrypted signature
                    if (data["s"] != null)
                    {
                        string html           = helper.DownloadWebPage(VideoUrl);
                        string Player_Version = Regex.Match(html, @"""\\/\\/s.ytimg.com\\/yts\\/jsbin\\/html5player-(.+?)\.js""").Groups[1].ToString();

                        // Decrypt the signature
                        string decryptedSignature = GetDecipheredSignature(Player_Version, signature.ToString());

                        // The new download-url with decrypted signature
                        url += "&signature=" + decryptedSignature;
                    }

                    var size      = getSize(url);
                    var videoItem = new YouTubeVideoQuality();
                    videoItem.DownloadUrl = url;
                    videoItem.VideoSize   = size;
                    videoItem.VideoTitle  = title;
                    var tagInfo = new ITagInfo(Uri.UnescapeDataString(data["itag"]));
                    videoItem.Dimension = tagInfo.VideoDimensions;
                    videoItem.Extention = tagInfo.VideoExtentions;
                    videoItem.Length    = long.Parse(videoDuration);
                    list.Add(videoItem);
                }
                catch { }
            }

            return(list);
        }