Home > ASP.NET MVC, Youtube Video > Download Youtube Video ASP.NET MVC C#.

Download Youtube Video ASP.NET MVC C#.

September 10, 2011 Leave a comment Go to comments

Here I am going to describe the way you can download youtube video.

I have done an extensive search on how to download youtube video. But I didn’t get anything workable. All places I got the video download link like
http://youtube.com/get_video?video_id=t7w5Jj4Xmt8&t=vjVQa1PpcFPy4lrvGrDifDcP24sT1vHKZ27RT6ip8Aw=&fmt=22

But this type of URL is not working anymore. So going to search the response string on youtube video page and got a large flash player html like.

My target is to retrieve the video download URL. I have done some decoding and got the URL’s like within the large HTML

URL 1:
http://o-o.preferred.btcl-dac1.v14.lscache2.c.youtube.com/videoplayback?sparams=id,expire,ip,ipbits,itag,ratebypass,oc:U0hQSlVSUV9FSkNOMF9NTFVD&fexp=907063,912600&itag=44&ip=0.0.0.0&signature=17D5E1B0012ADA4F3F9551C413C3D42C1050520D.D84C7B42BAA0AC6470E205932E8ED91F708137D5&sver=3&ratebypass=yes&expire=1315573200&key=yt1&ipbits=0&id=d43e63de330caaa0&quality=large&fallback_host=tc.v14.cache2.c.youtube.com&type=video/webm;

URL 2:
http://o-o.preferred.btcl-dac1.v19.lscache8.c.youtube.com/videoplayback?sparams=id,expire,ip,ipbits,itag,algorithm,burst,factor,oc:U0hQSlVSUV9FSkNOMF9NTFVD&fexp=907063,912600&algorithm=throttle-factor&itag=35&ip=0.0.0.0&burst=40&sver=3&signature=30D267633E9ABF59E1F48CC9A15C1AED16837314.15F1E316C903C19906D545F3DD35C27AAF7A7FAD&expire=1315573200&key=yt1&ipbits=0&factor=1.25&id=d43e63de330caaa0&quality=large&fallback_host=tc.v19.cache8.c.youtube.com&type=video/x-flv

URL 3:
http://o-o.preferred.btcl-dac1.v19.lscache7.c.youtube.com/videoplayback?sparams=id,expire,ip,ipbits,itag,ratebypass,oc:U0hQSlVSUV9FSkNOMF9NTFVD&fexp=907063,912600&itag=18&ip=0.0.0.0&signature=416E0CD141D4E1A0758404CCD19E07BAD7A7F5D4.4E71474194702D05B1A8C80B69FC5770C9C3B423&sver=3&ratebypass=yes&expire=1315573200&key=yt1&ipbits=0&id=d43e63de330caaa0&quality=medium&fallback_host=tc.v19.cache7.c.youtube.com&type=video/mp4

First, I have tried with the URL 1 it returns 403 forbidden error.
Then URL 2 also returns 403 forbidden errors.
But the third one worked for me

Here is the ASP.NET MVC Code to download youtube video

Controller: VideoController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.IO;

namespace Video.Youtube.Controllers
{
    public class VideoController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult DownLoadYoutubeVideo()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DownLoadYoutubeVideo(string url)
        {
            string videoUrl = GetVideoMP4Url(url);

            try
            {
                WebClient wc = new WebClient();
                string fileName = @"E:\video\" + Guid.NewGuid().ToString() + ".mp4";
                wc.DownloadFile(videoUrl, fileName);

                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment; filename="
                    + new FileInfo(fileName).Name);
                Response.WriteFile(fileName);
                Response.End();
            }
            catch (Exception ex)
            {
            }

            return View();
        }

        private string GetVideoMP4Url(string url)
        {
            try
            {
                WebRequest req = WebRequest.Create(url);
                StreamReader reader = new StreamReader(
                    req.GetResponse().GetResponseStream());
                string content = reader.ReadToEnd();

                int start = content.IndexOf("vorbi");
                int end = content.IndexOf("mp4", start) + 3;
                content = content.Substring(start, end - start);

                int start1 = content.LastIndexOf("http");
                int end1 = content.LastIndexOf("mp4") + 3;
                content = content.Substring(start1, end1 - start1);
                content = Server.UrlDecode(content);
                content = Server.UrlDecode(content);

                return new Uri(content).AbsoluteUri;
            }
            catch (Exception ex)
            {
            }

            return null;            
        }

    }
}

And here is the razor view:

@{
    ViewBag.Title = "DownLoadYoutubeVideo";
    Layout = "~/Views/Shared/_Layout.cshtml";	
}
@using (Html.BeginForm("DownLoadYoutubeVideo", "Video"))
{
    @Html.Label("Enter Youtube URL:")
    @Html.TextBox("Url", null, new { style = "width:500px;" })
    
}

You can convert this downloaded mp4 video to any format using the great FFMPEG command line tools. Which, you can also be used from ASP.NET MVC.

I will describe the video conversion process later.

Advertisements
Categories: ASP.NET MVC, Youtube Video
  1. Anon
    November 29, 2011 at 3:48 am

    Hey.. nice post.. looks like Google changed their code already. I could download a .flv file but I am unable to play the file using .flv players! It appears that Google is splitting up the large video into smaller chunks and requesting the chunks on demand (as and when the end of the previous chunk is nearing).

  2. Andrew
    December 10, 2011 at 5:15 am

    Nice code, working fine in localhost 🙂
    but when i uploaded on my website, it give error “HTTP Error 403 (Forbidden): The server refused to fulfill the request.” 😦

    Please help me

    Full error is as:

    Access to the webpage was denied

    You are not authorized to access the webpage at http://o-o.preferred.ord12s02.v4.lscache6.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Cratebypass%2Ccp&fexp=919200&itag=18&ip=69.0.0.0&signature=BE40FC87910D4E156A8A21E8557B967FF5A86D88.3ECE516ED3286042C7B8A5D69DF1BCFD44329F31&sver=3&ratebypass=yes&source=youtube&expire=1323519219&key=yt1&ipbits=8&cp=U0hRSFVMV19GU0NOMV9NRkFDOlhDTUY0SFFSZDkx&id=b999123b4521012d&quality=medium&fallback_host=tc.v4.cache6.c.youtube.com&type=video/mp4 You may need to sign in.
    HTTP Error 403 (Forbidden): The server refused to fulfill the request.

    • December 10, 2011 at 1:49 pm

      @andrew:
      It should also work in the server. Please try with other file as test.
      I am using this in a server.

      • Andrew
        December 13, 2011 at 8:11 am

        @mrsarker – sorry, i hv tried again it with flv file.. its not working…all code run fine on localhost… do you hv any alternate solution ?

  3. Andrew
    December 13, 2011 at 8:42 am

    @mrsarker – i hv converted Windows form application into ASP.NET Web Application , then i uploaded on my website … but it no work…same thing work ,when i tried on my localhost

  4. December 15, 2011 at 9:30 am

    Did you check only for one video????
    Try with other video. It is working nice for me.
    I have hosted in a server and working fine there.
    Sorry, I have no other alternatives.

    • Andrew
      December 17, 2011 at 4:42 pm

      yes, i checked many video, but still same problem.. i will try it with another alternative,… i will contact u , if got it…
      thanx for your reply

  5. May 30, 2013 at 12:29 pm

    Hi
    please help me , i m working for post comment on you tube in mvc but i am not able to do this

  6. sudheer lakki
    July 12, 2013 at 10:43 am

    Hey Good it is working fine in localhost,If i move my code to server file is not downloading.
    I need to download the file to C://Users//{username}//Downloads folder in C# MVC can you give me a suggestion please.

    When i move my code to live it is not downloading

    Thanks,
    Lakki

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: