c# asp.net youtube video downloader script

by nolovelust 19. February 2010 15:37

Update on 9/04/2010: Since youtube changed their layout code below may not work. Reason for that is that Youtube now generates unique machine id for each computer that visits it.

____________________________________________________________

Below code enables you to download youtube videos in MP4 format. It is easy to use
Save the code as youtube.aspx and call it like youtube.aspc?youtube=http://uk.youtube.com/watch?v=DD-pfi6vtnk&fmt=13

fmt 13 = 3GP download
fmt 18 = MP4 download

 

protected void Page_Load(object sender, EventArgs e)
    {

	        string vid_format = Request.QueryString["fmt"];
            if (vid_format == null)
            { vid_format = "13"; }


        if (Request.QueryString["youtube"] != null)
        {
            if (Request.QueryString["youtube"].ToString().Trim() != "")
            {
                string url = Request.QueryString["youtube"].ToString().Trim();
                url = convert(url);
                if (url != "")
                {
                    youtubecatch2(url,vid_format);
                }
            }
        }
    }


    private void youtubecatch2(string url, string vid_format)
    {
        string buffer = getContent(url);
        if (buffer.IndexOf("Error:") < 0)
        {
            try
            {

                int start = 0, end = 0;
                string startTag = "/watch_fullscreen?";
                string endTag = ";";
                start = buffer.IndexOf(startTag, StringComparison.CurrentCultureIgnoreCase);
                end = buffer.IndexOf(endTag, start, StringComparison.CurrentCultureIgnoreCase);
                string str = buffer.Substring(start + startTag.Length, end - (start + startTag.Length));
                string vid = str.Substring(str.IndexOf("video_id"), str.IndexOf("&", str.IndexOf("video_id")) - str.IndexOf("video_id"));
                string l = str.Substring(str.IndexOf("&l"), str.IndexOf("&", str.IndexOf("&l") + 1) - str.IndexOf("&l"));
                string t = str.Substring(str.IndexOf("&t"), str.IndexOf("&", str.IndexOf("&t") + 1) - str.IndexOf("&t"));
                string title = str.Substring(str.IndexOf("&title=") + 7);
                //title = title.Substring(0, title.Length - 1);
                //litResult.Text = "Download " + title + "";

                //Response.Redirect("http://youtube.com/get_video?" + vid + l + t +"&fmt=" + vid_format );
                Response.Redirect(GetRedirectedUrl("http://youtube.com/get_video?" + vid + l + t + "&fmt=" + vid_format));
                
				//Response.Write(buffer);



            }
            catch (Exception expTwo)
            {
			//Response.Write (expTwo.Message.ToString());
			//Response.Redirect("/removed.asp?err=1" /* + expTwo.Message.ToString())*/); 
			}
            
        }
        else
        {
            Response.Write (buffer); 
        }
    }
    private string GetRedirectedUrl(string url)
    {
        string buffer;
        try
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "GET";
            //req.ContentLength = outputBuffer.Length;
            //req.ContentType = "application/x-www-form-urlencoded";
            //StreamWriter swOut = new StreamWriter(req.GetRequestStream());
            //swOut.Write(outputBuffer);
            //swOut.Close();
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            //StreamReader sr = new StreamReader(resp.GetResponseStream());
            //buffer = sr.ReadToEnd();
            //sr.Close();
            buffer = resp.ResponseUri.ToString();
        }
        catch (Exception exp)
        {
            buffer = "Error: " + exp.Message.ToString();
        }

        return (buffer);
    }
    

    private string getContent(string url)
    {
        string buffer;
        try
        {
            string outputBuffer = "where=46038";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentLength = outputBuffer.Length;
            req.ContentType = "application/x-www-form-urlencoded";
            StreamWriter swOut = new StreamWriter(req.GetRequestStream());
            swOut.Write(outputBuffer);
            swOut.Close();
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            StreamReader sr = new StreamReader(resp.GetResponseStream());
            buffer = sr.ReadToEnd();
            sr.Close();
        }
        catch (Exception exp)
        {
            buffer = "Error: " + exp.Message.ToString();
        }

        return (buffer);
    }
    private string convert(string url)
    {
        url = url.Replace("www.youtube.com", "youtube.com");
        if (url.IndexOf("http://youtube.com/v/") >= 0)
        {
            url.Replace("http://youtube.com/v/", "http://youtube.com/watch?v=");
        }
        if (url.IndexOf("http://youtube.com/watch?v=") < 0)
        {
            url = "";
        }
        return (url);
    }

 

Tags: , ,

ASP.NET | Open Source

Comments (4) -

andre
andre South Africa
4/8/2010 12:29:19 PM #

This would be great if the  youtubecatch2 method was commented better.

I cannot find "/watch_fullscreen?" in my buffer so the whole thing fails.

Any ideas?

Reply

johnalphen
johnalphen United States
4/9/2010 10:10:12 AM #

startInfo.Arguments = string.Format(“-i \”{0}\” -f image2 -ss 1 -vframes 1
-s 80×60 -an \ \”{1}\””, srcURL, destURL);

How it works , can any one reply me ???

Reply

nolovelust
nolovelust
4/9/2010 12:15:09 PM #

Code above is from one of my old projects and since that you tube changed many things. you should for example try youtube api to access videos programmatically.

Reply

Sam
Sam India
7/6/2011 8:11:46 AM #

Try this instead .........
my.opera.com/.../youtube-video-download-method-and-csharp-code
Best of luck

Reply

Add comment

  Country flag

biuquote
Loading

Adverts

Welcome

Tag cloud

Month List