lunes, 30 de mayo de 2011

Download Youtube Videos With C#

All right since youtube changed around its layout and user terms
it seems to be that so many unattended, yet good apps to download
videos from youtube just stoped working. This is because now youtube
makes the final user completely responsable for the content he/she uploads
and visits.

Basically what we do is get the "movie_player" element in the video page
then scrap a little this element, to find the "fmt_url_map", which is kind
of complex as its provides up to 4 video formats, in this code snippet
I show how to get the mp4 file, but you can hop between the options
to have the other formats, flv, mp3 or whatever...

in order to do this you will need the Html agility pack found here:
http://htmlagilitypack.codeplex.com/
download it and add it as a reference in your project.

then you need this two methods:

  /*this methods get's the download link for youtube videos in mp4 format.*/
  public string url(string url)
        {
            string html = getYoutubeHtml(url);
            HtmlAgilityPack.HtmlDocument hDoc = new HtmlDocument();
            hDoc.LoadHtml(html);
            HtmlNode node = hDoc.GetElementbyId("movie_player");
            string flashvars = node.Attributes[5].Value;
            string _url = Uri.UnescapeDataString(flashvars);
            string[] w = _url.Split('&');
            string link = "";
            bool foundUrlMap = false;
            for (int i = 0; i < w.Length; i++)
            {
                if (w[i].Contains("fmt_url_map="))
                {
                    foundUrlMap = true;
                    link += w[i].Split('|')[1];
                }
                if (foundUrlMap)
                {
                    /*add the parameters to the url*/
                    link += "&" + w[i];
                    if (w[i].Contains("id="))
                    {
                        link = link.Split(',')[0]; /*change the array index for different formats*/
                        break;
                    }
                }
            }
            link = link.Split('|')[1] + "&title=out";
            return link;
        }
  
  /*this method downloads the html code from the youtube page.*/
        private string getYoutubeHtml(string url)
        {
            string html = "";
            WebRequest request = WebRequest.Create(url);
            WebResponse response = request.GetResponse();
            TextReader reader = new StreamReader(response.GetResponseStream());
            string line = "";
            while ((line = reader.ReadLine()) != null)
            {
                html += line;
            }
            return html;
        }

then you can use a webclient to get the video to your coumputer simple use this code lines:
 WebClient wc = new WebClient();
    location = YoutubeSaveDialog.FileName;
    wc.DownloadFile(download_url, location);

Thanks again youtube for making our lifes a little more complicated :) (just kidding this was easy!)

HAPPY CODING!

3 comentarios:

  1. Do you have an updated version of this? Does not seem to work correctly. Get indexoutofbounds at "link = link.Split('|')[1] + "&title=out";"

    ResponderEliminar
  2. yep, youtube changed the game a few months ago, let me see if I can get a new post on this!

    ResponderEliminar