views:

851

answers:

7

Does anybody know the VB or C# code to programmatically download a video from YouTube?

I have gone through all related questions on this site and the only code I have found (which uses the WebClient Class webclient.DownloadFileAsync) returns the text source of the video, but not the Flash video (.flv) file that needs to be downloaded?

Do you know any SDK or library which can help? I have also downloaded the GoogleAPI (YouTube DLL) but have not found a way of doing it.

+1  A: 

Downloading those videos is not really legal, so there will be no official API there.

You still can grab html and find .flv though.

S.Mark
+4  A: 

Try the following.

http://code.google.com/apis/youtube/code.html
http://apiblog.youtube.com/2009/03/latest-net-sdk-released-linq-new-social.html

Brij
Am I missing something? It doesn't look like you can download videos with the API
Stranger
A: 

Open source, so you can have a look at the code and see how they did it.

http://www.codeproject.com/KB/IP/MyDownloader.aspx

Here is a more succinct example - note, not the actual class:

http://pastebin.com/f396257b6

Dominic Bou-Samra
+2  A: 

You can use web scraping and extract the information needed to download the video. It only requires about 5 lines of code.

If the YouTube URL is:

    http://www.youtube.com/watch?v=DYW50F42ss8

the HTML for the URL may look like the following near "t":

    "rv.5.view_count": "470136", "t": "vjVQa1PpcFPDxa83Hr1_9pftRUWdsMyJ10a2o8QZvIs%3D", "rv.6.id": "3f72CTDe4-0"

A regular expression that matches uniquely if the HTML is processed line by line is:

    \",\s*\"t\":\s*\"([^\"]+)

The download URL is then

    http://www.youtube.com/get_video.php?video_id=ID1&t=ID2

where ID1 is the ID from the input, DYW50F42ss8 in the example. ID2 is what comes after "t" with %3D replaced by "=", vjVQa1PpcFPDxa83Hr1_9pftRUWdsMyJ10a2o8QZvIs= in the example.

Thus the URL to download the Flash video in this example is:

    http://www.youtube.com/get_video.php?video_id=DYW50F42ss8&t=vjVQa1PpcFPDxa83Hr1_9pftRUWdsMyJ10a2o8QZvIs=

Note that the structure/syntax of the YouTube HTML changes from time to time and thus your software must also be modified when it happens. I have observed such a change 3 times in 13 months (2008-10, 2009-03 and 2009-08).

Peter Mortensen
Thanks Peter, this is the only solution that works. Of course if the HTML is changed, this may not work anymore, but apparently there is no other solution.
JMSoft
@JMSoft: unfortunately another breakage happend on 2010-07-22. The solution here no longer works. I am working on finding a solution and will update my answer when I find one. In the meantime, see: http://webapps.stackexchange.com/questions/4651/what-happened-to-youtube-download-urls
Peter Mortensen
+1  A: 

Here's a quick function to do what you want. I made use of Peter Mortensens regex.

It's not threaded or anything like that.

http://pastebin.com/m46f0d312

alexn
A: 

You can take a look at an extensible YouTube web-scraping .NET library and code here:

http://ivolo.mit.edu/post/Download-YouTube-Videos-using-C.aspx

Ilya
+1  A: 

I wrote this C# function and it worked for me:

public static void Download(string videoID, string newFilePath)
{
    WebClient wc = new WebClient();
    string file = wc.DownloadString(string.Format("http://www.youtube.com/watch?v={0}", videoID));
    string t = new Regex("(?<=&t=)[^&]*").Match(file).Value;
    wc.DownloadFile(string.Format("http://www.youtube.com/get_video?t={0}=&amp;video_id={1}",t,videoID), newFilePath);

}

Thanks Peter.