Thứ Tư, 20 tháng 4, 2011

Download video from youtube use c# and .net 4.0

Download video from youtube use c# and .net 4.0

download code

Idea:


  1. download html string by url
  2. analytics html string, split string and get title, video url, thumbnail url
Action:


  1. Create new project
  2. Design website UI:
  3. Write code for download info button:
  4. Create sub methods:

  5. press F5 to test:
  6. Link download:
  7. Finish
Code in project:

protected void btnDownload_Click(object sender, EventArgs e)
        {
            string URL = convertUrl(txtUrl.Text.Trim());
            string title_video = "";
            imgThumbnail.ImageUrl  = "http://i1.ytimg.com/vi/" + URL.Replace("http://youtube.com/watch?v=", "") + "/default.jpg";
            imgThumbnail0.ImageUrl = "http://i1.ytimg.com/vi/" + URL.Replace("http://youtube.com/watch?v=", "") + "/0.jpg";
            imgThumbnail1.ImageUrl = "http://i1.ytimg.com/vi/" + URL.Replace("http://youtube.com/watch?v=", "") + "/1.jpg";
            imgThumbnail2.ImageUrl = "http://i1.ytimg.com/vi/" + URL.Replace("http://youtube.com/watch?v=", "") + "/2.jpg";
            hlDownload.NavigateUrl = GetDownloadLink(URL,ref title_video);
            lblTitle.Text = title_video;
        }

        private string convertUrl(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.Substring(0, url.IndexOf("&")));
        }

        public string GetDownloadLink(string inputurl,ref string title_video)
        {
            string outputurl = "";

            string type = "";
            int size = 0;
            string source;


            var request = (HttpWebRequest)WebRequest.Create(inputurl.ToString());


            var response = (HttpWebResponse)request.GetResponse();

            source = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8).ReadToEnd();

            //get title
            title_video = source.Substring(source.IndexOf("<title>") + 7, source.IndexOf("</title>") - (source.IndexOf("<title>") + 7)).Replace("\n", "").Replace("youtube", "");


            if (source.IndexOf("video_id") > -1)
            {
                if (source.Contains("&fmt_url_map="))
                {
                    source = System.Text.RegularExpressions.Regex.Split(source, "&fmt_url_map=")[1];
                }
                if (source.Contains("\"fmt_url_map\": \""))
                {

                    source = System.Text.RegularExpressions.Regex.Split(source, "\"fmt_url_map\": \"")[1];
                }
            }
            source = HttpUtility.UrlDecode(source).Replace("%2C", ",").Split(new string[] { "http:\\/\\/" }, StringSplitOptions.RemoveEmptyEntries).ElementAt(1);
            source = source.Insert(0, "http://").Replace("|", "");
            source = source.Remove(source.LastIndexOf(","), source.Length - source.LastIndexOf(","));
            if (Convert.ToString(source[source.Length - 1]) == "C")
            {
                source = source.Remove(source.Length - 1, 1);
            }
            if (source.Contains("rv.2.rating"))
            {
                int index = source.IndexOf("\",");
                source = source.Remove(index, source.Length - index);
            }

            source = source.Replace(@"\/", "/");
            source = source.Replace("\\u0026", "&");
            var request2 = (HttpWebRequest)WebRequest.Create(source);
            var response2 = (HttpWebResponse)request2.GetResponse();
            if (response2.ContentType == "video/x-flv")
            {
                type = ".flv";
                outputurl = source;
                size = (int)response2.ContentLength;
            }
            else if (response2.ContentType == "video/mp4")
            {
                type = ".mp4";
                outputurl = source;
                size = (int)response2.ContentLength;
            }
            else
            {
                type = "";
                outputurl = "";
            }

            return outputurl;
        }

Wish you success!

Không có nhận xét nào:

Đăng nhận xét