Provides a method to get the download link of a YouTube video.
        private static JObject LoadJson(string url)
        {
            string str = HttpHelper.DownloadString(url);

            if (DownloadUrlResolver.IsVideoUnavailable(str))
            {
                throw new VideoNotAvailableException();
            }
            return(JObject.Parse(new Regex("ytplayer\\.config\\s*=\\s*(\\{.+?\\});", RegexOptions.Multiline).Match(str).Result("$1")));
        }
 public static IEnumerable <VideoInfo> GetDownloadUrls(string videoUrl, bool decryptSignature = true)
 {
     if (videoUrl == null)
     {
         throw new ArgumentNullException("videoUrl");
     }
     if (!DownloadUrlResolver.TryNormalizeYoutubeUrl(videoUrl, out videoUrl))
     {
         throw new ArgumentException("URL is not a valid youtube URL!");
     }
     try
     {
         JObject json                       = DownloadUrlResolver.LoadJson(videoUrl);
         string  videoTitle                 = DownloadUrlResolver.GetVideoTitle(json);
         IEnumerable <VideoInfo> list       = (IEnumerable <VideoInfo>)Enumerable.ToList <VideoInfo>(DownloadUrlResolver.GetVideoInfos(DownloadUrlResolver.ExtractDownloadUrls(json), videoTitle));
         string html5PlayerVersion          = DownloadUrlResolver.GetHtml5PlayerVersion(json);
         IEnumerator <VideoInfo> enumerator = list.GetEnumerator();
         try
         {
             while (((IEnumerator)enumerator).MoveNext())
             {
                 VideoInfo current = enumerator.Current;
                 current.HtmlPlayerVersion = html5PlayerVersion;
                 if (decryptSignature && current.RequiresDecryption)
                 {
                     DownloadUrlResolver.DecryptDownloadUrl(current);
                 }
             }
         }
         finally
         {
             if (enumerator != null)
             {
                 ((IDisposable)enumerator).Dispose();
             }
         }
         return(list);
     }
     catch (Exception ex)
     {
         if (ex is WebException || ex is VideoNotAvailableException)
         {
             throw;
         }
         else
         {
             DownloadUrlResolver.ThrowYoutubeParseException(ex, videoUrl);
         }
     }
     return(null);
 }
        public static async Task <Uri> GetVideoUriAsync(string videoId)
        {
            var videoUrl = "http://www.youtube.com/watch?v=" + videoId;

            var videoInfos = await GetDownloadUrls(videoUrl, false);

            // TODO - parameterize video resolution
            VideoInfo video = videoInfos
                              .First(info => info.VideoType == VideoType.Mp4 && info.Resolution == 360);

            // If the video has an encrypted signature, decipher it
            if (video.RequiresDecryption)
            {
                await DownloadUrlResolver.DecryptDownloadUrl(video);
            }

            return(new Uri(video.DownloadUrl));
        }
        public static void DecryptDownloadUrl(VideoInfo videoInfo)
        {
            IDictionary <string, string> queryString = HttpHelper.ParseQueryString(videoInfo.DownloadUrl);

            if (!queryString.ContainsKey("signature"))
            {
                return;
            }
            string signature = queryString["signature"];
            string decipheredSignature;

            try
            {
                decipheredSignature = DownloadUrlResolver.GetDecipheredSignature(videoInfo.HtmlPlayerVersion, signature);
            }
            catch (Exception ex)
            {
                throw new YoutubeParseException("Could not decipher signature", ex);
            }
            videoInfo.DownloadUrl        = HttpHelper.ReplaceQueryStringParameter(videoInfo.DownloadUrl, "signature", decipheredSignature);
            videoInfo.RequiresDecryption = false;
        }
示例#5
0
        static void Main(string[] args)
        {
            try
            {
                //var link = new List<string>();
                //link.Add("https://www.youtube.com/watch?v=GlGuLcQhrWg");
                string link = @"https://www.youtube.com/watch?v=YzC-FYg66xA&list=PL0kIvpOlieSNWR3YPSjh9P2p43SFnNBlB&index=1";
                //foreach (var item in link)
                //{

                IEnumerable <VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls("https://www.youtube.com/watch?v=YzC-FYg66xA&list=PL0kIvpOlieSNWR3YPSjh9P2p43SFnNBlB&index=1");
                VideoInfo video           = videoInfos.First(info => info.VideoType == VideoType.Mp4 && info.Resolution == 720);
                var       videoDownloader = new VideoDownloader(video, Path.Combine(@"C:\Users\lalomarquez\Desktop\", video.Title + video.VideoExtension));

                videoDownloader.DownloadProgressChanged += (sender, argss) => Console.WriteLine(argss.ProgressPercentage);
                videoDownloader.Execute();
                //}
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
 private static IEnumerable <DownloadUrlResolver.ExtractionInfo> ExtractDownloadUrls(JObject json)
 {
     string[] strArray = (string[])Enumerable.ToArray <string>(Enumerable.Concat <string>(((string)DownloadUrlResolver.GetStreamMap(json)).Split((char[])new char[1]
     {
         ','
     }), ((string)DownloadUrlResolver.GetAdaptiveStreamMap(json)).Split((char[])new char[1]
     {
         ','
     })));
     for (int index = 0; index < strArray.Length; ++index)
     {
         IDictionary <string, string> queryString = HttpHelper.ParseQueryString(strArray[index]);
         bool   flag = false;
         string url;
         if (queryString.ContainsKey("s") || queryString.ContainsKey("sig"))
         {
             flag = queryString.ContainsKey("s");
             string str = queryString.ContainsKey("s") ? queryString["s"] : queryString["sig"];
             url = string.Concat(string.Format("{0}&{1}={2}", queryString["url"], "signature", str), queryString.ContainsKey("fallback_host") ? string.Concat("&fallback_host=", queryString["fallback_host"]) : string.Empty);
         }
         else
         {
             url = queryString["url"];
         }
         string str1 = HttpHelper.UrlDecode(HttpHelper.UrlDecode(url));
         if (!HttpHelper.ParseQueryString(str1).ContainsKey("ratebypass"))
         {
             str1 = string.Concat(str1, string.Format("&{0}={1}", "ratebypass", "yes"));
         }
         DownloadUrlResolver.ExtractionInfo extractionInfo = new DownloadUrlResolver.ExtractionInfo();
         extractionInfo.RequiresDecryption = flag;
         Uri uri = new Uri(str1);
         extractionInfo.Uri = uri;
         yield return(extractionInfo);
     }
     strArray = (string[])null;
 }
 public static Task <IEnumerable <VideoInfo> > GetDownloadUrlsAsync(string videoUrl, bool decryptSignature = true)
 {
     return(Task.Run <IEnumerable <VideoInfo> >(() => DownloadUrlResolver.GetDownloadUrls(videoUrl, decryptSignature)));
 }