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

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:

URL 2:

URL 3:

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();

        public ActionResult DownLoadYoutubeVideo(string url)
            string videoUrl = GetVideoMP4Url(url);

                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);
            catch (Exception ex)

            return View();

        private string GetVideoMP4Url(string url)
                WebRequest req = WebRequest.Create(url);
                StreamReader reader = new StreamReader(
                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.

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= You may need to sign in.
    HTTP Error 403 (Forbidden): The server refused to fulfill the request.

    • December 10, 2011 at 1:49 pm

      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

    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


  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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: