Ejemplo n.º 1
0
        /// <summary>
        /// Return direct url to video from Youtube
        /// </summary>
        /// <param name="url">Link (string) from Youtube, like "https://www.youtube.com/watch?v=ХХХХХХХХХ" or embed version</param>
        /// <returns>Direct link to video stream (videofile)</returns>
        public static string GetVideoDirectURL(string url)
        {
            IList <VideoQuality> list = null;

            list = YouTubeDownloader.GetYouTubeVideoUrls(url);
            string DirectURL = list.First().DownloadUrl;

            return(DirectURL);
        }
Ejemplo n.º 2
0
        public static string GetYouTubeVideoTitle(string videoUrl)
        {
            var list = new List <VideoQuality>();

            var id     = YouTubeDownloader.GetVideoIdFromUrl(videoUrl);
            var longId = YouTubeDownloader.GetLongVideoIdFromUrl(videoUrl);

            string stsValue       = null;
            bool   ageGateContent = false;
            string status;
            string title;
            string videoDuration;
            string infoUrl;
            string infoText;

            WebClient webClient = new WebClient();

            webClient.Proxy = Helper.InitialProxy();

            infoUrl  = string.Format(CultureInfo.InvariantCulture, "https://www.youtube.com/watch?v={0}&gl=US&hl=en&has_verified=1&bpctr=9999999999", longId);
            infoText = webClient.DownloadString(infoUrl);

            ageGateContent = infoText.Contains("player-age-gate-content");
            if (ageGateContent)
            {
                infoUrl  = string.Format(CultureInfo.InvariantCulture, "https://www.youtube.com/embed/{0}", longId);
                infoText = webClient.DownloadString(infoUrl);
                stsValue = Regex.Match(infoText, @"""sts"":([0-9]+?),").Groups[1].Value;
            }
            if (ageGateContent && !string.IsNullOrEmpty(stsValue))
            {
                infoUrl = string.Format(CultureInfo.InvariantCulture, "https://www.youtube.com/get_video_info?video_id={0}&eurl=https://youtube.googleapis.com/v/{1}&sts={2}", longId, id, stsValue);
            }
            else
            {
                infoUrl = string.Format(CultureInfo.InvariantCulture, "http://www.youtube.com/get_video_info?&video_id={0}&el=detailpage&ps=default&eurl=&gl=US&hl=en", longId);
            }
            infoText = webClient.DownloadString(infoUrl);
            webClient.Dispose();
            var infoValues = HttpUtility.ParseQueryString(infoText);

#if DEBUG
            Dictionary <String, String> testInfoValues = new Dictionary <string, string>();
            foreach (String key in infoValues)
            {
                testInfoValues.Add(key, infoValues[key]);
            }
#endif

            String offerButtonText;
            offerButtonText = infoValues["ypc_offer_button_text"];
            if (offerButtonText == null)
            {
                offerButtonText = String.Empty;
            }
            else
            {
                offerButtonText = ", Pay " + offerButtonText;
            }

            status = infoValues["status"];
            if (status != null && status == "fail")
            {
                var  errorcode    = infoValues["errorcode"];
                var  errordetail  = infoValues["errordetail"];
                var  reason       = infoValues["reason"];
                bool boolDetail   = false;
                int  intErrorCode = int.Parse(errorcode, CultureInfo.InvariantCulture);
                if (errordetail != null)
                {
                    int detail = int.Parse(errordetail, CultureInfo.InvariantCulture);
                    boolDetail = detail == 0 ? false : true;
                }
                throw new DownloaderException(reason, intErrorCode, boolDetail);
            }
            title         = infoValues["title"];
            videoDuration = infoValues["length_seconds"];

            return(title);
        }
Ejemplo n.º 3
0
        //private static readonly string[] elValue = {
        //    "",
        //    "detailpage",
        //    "embedded",
        //    "vevo",
        //};

        public static IList <VideoQuality> GetYouTubeVideoUrls(string videoUrl)
        {
            var list = new List <VideoQuality>();

            var id     = YouTubeDownloader.GetVideoIdFromUrl(videoUrl);
            var longId = YouTubeDownloader.GetLongVideoIdFromUrl(videoUrl);

            string stsValue       = null;
            bool   ageGateContent = false;
            string status;
            string title;
            string videoDuration;

            string[] videos;
            string   infoUrl;
            string   infoText;

            WebClient webClient = new WebClient();

            webClient.Proxy = Helper.InitialProxy();

            infoUrl  = string.Format(CultureInfo.InvariantCulture, "https://www.youtube.com/watch?v={0}&gl=US&hl=en&has_verified=1&bpctr=9999999999", longId);
            infoText = webClient.DownloadString(infoUrl);

            ageGateContent = infoText.Contains("player-age-gate-content");
            if (ageGateContent)
            {
                infoUrl  = string.Format(CultureInfo.InvariantCulture, "https://www.youtube.com/embed/{0}", longId);
                infoText = webClient.DownloadString(infoUrl);
                stsValue = Regex.Match(infoText, @"""sts"":([0-9]+?),").Groups[1].Value;
            }
            if (ageGateContent && !string.IsNullOrEmpty(stsValue))
            {
                infoUrl = string.Format(CultureInfo.InvariantCulture, "https://www.youtube.com/get_video_info?video_id={0}&eurl=https://youtube.googleapis.com/v/{1}&sts={2}", longId, id, stsValue);
            }
            else
            {
                infoUrl = string.Format(CultureInfo.InvariantCulture, "https://www.youtube.com/get_video_info?&video_id={0}&el=detailpage&ps=default&eurl=&gl=US&hl=en", longId);
            }

            //       infoUrl = string.Format(CultureInfo.InvariantCulture, "https://www.youtube.com/get_video_info?&video_id={0}", longId);

            //System.Net.WebClient wc = new System.Net.WebClient();
            //string Text = wc.DownloadString("http://www.climahom.eu/EULA.txt");
            //string Text2 = wc.DownloadString(infoUrl);

            infoText = webClient.DownloadString(infoUrl);
            webClient.Dispose();
            var infoValues = HttpUtility.ParseQueryString(infoText);

#if DEBUG
            Dictionary <String, String> testInfoValues = new Dictionary <string, string>();
            foreach (String key in infoValues)
            {
                testInfoValues.Add(key, infoValues[key]);
            }
#endif

            String offerButtonText;
            offerButtonText = infoValues["ypc_offer_button_text"];
            if (offerButtonText == null)
            {
                offerButtonText = String.Empty;
            }
            else
            {
                offerButtonText = ", Pay " + offerButtonText;
            }

            status = infoValues["status"];
            if (status != null && status == "fail")
            {
                var  errorcode    = infoValues["errorcode"];
                var  errordetail  = infoValues["errordetail"];
                var  reason       = infoValues["reason"];
                bool boolDetail   = false;
                int  intErrorCode = int.Parse(errorcode, CultureInfo.InvariantCulture);
                if (errordetail != null)
                {
                    int detail = int.Parse(errordetail, CultureInfo.InvariantCulture);
                    boolDetail = detail == 0 ? false : true;
                }
                throw new DownloaderException(reason, intErrorCode, boolDetail);
            }
            title         = infoValues["title"];
            videoDuration = infoValues["length_seconds"];
            videos        = infoValues["url_encoded_fmt_stream_map"].Split(',');

            string html  = Helper.DownloadWebPage(videoUrl);
            var    regex = new Regex(@"player-(.+?).js");
            Match  match = regex.Match(html);
            string playerVersion;
            if (match.Length > 3)
            {
                playerVersion = regex.Match(html).Result("$1");
            }
            else
            {
                playerVersion = @"vfl6PEqvQ\\/ www - player.css\";
            }

            foreach (var item in videos)
            {
                if (String.IsNullOrEmpty(item))
                {
                    throw new ArgumentException("Cannot download \"" + title + "\"" + offerButtonText);
                }
                try
                {
                    var    data          = HttpUtility.ParseQueryString(item);
                    var    fallback_host = data["fallback_host"];
                    String server        = String.Empty;
                    if (!String.IsNullOrEmpty(fallback_host))
                    {
                        server = Uri.UnescapeDataString(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 decryptedSignature;
                        try
                        {
                            // Decrypt the signature
                            decryptedSignature = Decipherer.DecipherWithVersion(signature.ToString(), playerVersion);
                        }
                        catch (Exception e)
                        {
                            //string ex = e.Message;
                            throw new DownloaderException("Signature decipher problem", e);
                        }
                        // The new download-url with decrypted signature
                        url += "&signature=" + decryptedSignature;
                    }

                    var size      = GetSize(url);
                    var videoItem = new VideoQuality();
                    videoItem.DownloadUrl = url;
                    videoItem.VideoSize   = size;
                    videoItem.VideoTitle  = title;
                    var tagInfo = new ITagInfo(Uri.UnescapeDataString(data["itag"]));
                    videoItem.Dimension = tagInfo.VideoDimensions;
                    videoItem.Extension = tagInfo.VideoExtensions;
                    try
                    {
                        videoItem.Length = long.Parse(videoDuration, CultureInfo.InvariantCulture);
                    }
                    catch { Debug.WriteLine("Can't get info about videoItem.Length (videoItem.Length). Assembly:" + Assembly.GetExecutingAssembly().GetName().ToString() + " Class YouTubeDownloader,  Method GetYouTubeVideoUrls");; }

                    list.Add(videoItem);
                }
                catch (Exception e)
                {
                    string ex = e.Message;
                    throw;
                }
            }
            return(list);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Return direct urls to images video preview from Youtube
 /// </summary>
 /// <param name="url">Link (string) from Youtube, like "https://www.youtube.com/watch?v=ХХХХХХХХХ" or embed version</param>
 /// <returns>Direct link to image</returns>
 public static string[] GetPrevImages(string url)
 {
     string[] Urls = new string[3];
     for (int i = 0; i < 3; i++)
     {
         Urls[i] = string.Format(CultureInfo.InvariantCulture, "http://i3.ytimg.com/vi/{0}/hq{1}.jpg", YouTubeDownloader.GetVideoIdFromUrl(url), (i + 1).ToString());
     }
     return(Urls);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Return direct url to image video from Youtube
 /// </summary>
 /// <param name="url">Link (string) from Youtube, like "https://www.youtube.com/watch?v=ХХХХХХХХХ" or embed version</param>
 /// <returns>Direct link to image</returns>
 public static string GetImage(string url)
 {
     return(string.Format(CultureInfo.InvariantCulture, "http://i3.ytimg.com/vi/{0}/hqdefault.jpg", YouTubeDownloader.GetVideoIdFromUrl(url)));
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Return duration of video from Youtube
        /// </summary>
        /// <param name="url">Link (string) from Youtumbe, like "https://www.youtube.com/watch?v=ХХХХХХХХХ" or embed version</param>
        /// <returns>Video duration as timespan</returns>
        public static TimeSpan GetDuraion(string url)
        {
            TimeSpan t = YouTubeDownloader.GetYouTubeVideoDuration(url);

            return(t);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Return Title of video from Youtube
        /// </summary>
        /// <param name="url">Link (string) from Youtube, like "https://www.youtube.com/watch?v=ХХХХХХХХХ" or embed version</param>
        /// <returns>Video Title</returns>
        public static string GetTitle(string url)
        {
            string t = YouTubeDownloader.GetYouTubeVideoTitle(url);

            return(t);
        }