Download YouTube video through .net code

27 06 2008

Using following methods you can download youtube videos directly from your browser without using any software.

1. Retrieve the id video from the link (the code after ‘v=’)
example: => NegsNQy1ZoU
2. In your favourite browser go to{ID video}
3. Link in the address bar will change. Replace ’swf/l.swf’ with ‘get_video’ and press Enter
4. Save your file

Just experienc it in your browser.

Do It Progrmatically:

Now we will do it programaticaly using Just follow followin steps

1. Create a page named as DownloadUTube.aspx

<form id=”form1″ runat=”server”>
<td>YouTube URL: <asp:TextBox ID=”txtUrl” runat=”server”></asp:TextBox></td>
<td><asp:Button ID=”btnSubmit” runat=”server” Text=”Submit” OnClick=”btnSubmit_Click” /></td>
<td align=”center” colspan=”2″>Example: </td>
<td colspan=”2″><asp:Literal ID=”litResult” runat=”server”></asp:Literal></td>

You have successfully completed 1st step now proceed to 2nd one.

Step 2:

Now in your cSharp file you have to go as follow.

// First of all we will change our utube link to our desired format.

string URL = this.txtUrl.Text;
string VideoId = URL.Substring(URL.IndexOf(‘=’)+1);
URL = @”; + VideoId;

// Now we will send request to server which will create new link.
WebRequest MyRequest = HttpWebRequest.Create(URL);
WebResponse MyResponse = MyRequest.GetResponse();

// we get newly create link into string RealURL
string RealURL = MyResponse.ResponseUri.ToString();

// Creates an HttpWebRequest with the specified URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(RealURL);

// Sends the HttpWebRequest and waits for the response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

// Gets the stream associated with the response.
Stream receiveStream = myHttpWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding(“utf-8″);

// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, encode);

// Now we will write whole stream to specified. It is the path within your website.
StreamWriter writer = new StreamWriter(Server.MapPath(@”Contents\Videos\YourVideoName.flv”), true);

Just go on specified path and u will see the video.

Final saying, if you have any problem about this. write to me. I will try my best to solve it.

Muhammad Tahir Rauf




7 responses

27 06 2008

Good one.

27 06 2008
ipod chargers

The youtube video tip is useful! Thanks!

17 07 2008
Bansh Patel

Hi Tahir,

Thanks a lot buddy. Realy thats helfull for me.
Thanks again.

Bansh Patel
System Analyst

4 09 2008
Pawan Kumar Singh

Hey Bro you have created a good program………..also there is another way to minimize the code i.e.

string videoID = txtPath.Text.Substring(txtPath.Text.IndexOf(‘=’) + 1);
WebRequest wReq = HttpWebRequest.Create(@”” + videoID);
WebResponse wRes = wReq.GetResponse();
string originalVdo = wRes.ResponseUri.OriginalString.Replace(“swf/l.swf”, “get_video”);
WebClient wc = new WebClient();
wc.DownloadFile(originalVdo, @”c:\fileName.flv”);

ur work is dam good…………….

7 10 2008

a good way I use to download videos from youtube is in this site

you can download youtube and metacafe videos, and also convert files format

It is very quick and easy, worked for me

1 02 2009
ishrat jahan


Leave a Reply

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

You are commenting using your 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: