Download YouTube Videos using C#

As far as I know, YouTube has blocked the get_video web service. If they release another public web service to download flv's, I or anyone else can extend the library described here by adding an IResolver provider. But for now, it seems this library will not work for downloading YouTube videos. Apologies.

Wouldn’t it be great to be able to download and watch your favorite YouTube videos  anytime you want, even when offline? In fact, wouldn’t it be even better if you can write a program that downloads multiple at once, and converts all of them to a specific audio or video format?

This entire process turns out to be fairly easy, and I want to provide a very brief explanation on how you can do this. I also attached a C# library that will do this for you, and that you can extend as YouTube web services may change in the future.

1. Resolve YouTube FLV download url

YouTube provides a convenient web service to download any public video if you have the two variables that identify the video server-side. These variables, “t” and “video_id”, can be extracted from the actual page source of the YouTube page in the properties that set up the flash video player. Once, we have parsed these variables, we can just download the FLV video from the following url:

http://youtube.com/get_video?video_id={0}&t={1}
where {0} is “video_id” and {1} is “t”

Understanding that the url, server variables, or page source format may change any time, and probably will, I used an interface to abstract out the actual url resolving:

public interface IResolver
{
     string ResolveFlvDownloadUrl(string pageSource, out string videoTitle);
}

If you write your own IResolver parser, you can still use the rest of my tool by extending DownloadManager and overriding

protected override IResolver CreateResolver()
{
     return new YtSourceDepthParser();
}

where YtSourceDepthParser will be your parser.

2. Download YouTube FLV

Just use an HTTPWebRequest to the resolved url to download the FLV video, and save it as a file.  In my tool, DownloadManager will do this for you, and provides events for when the download starts, progresses (bytes left, percent completed, time remaining), and finishes.

3. Watch your video


Enjoy your video in a player that can read FLV flash format videos like VLC.

Stay tuned for further articles where I will describe how to use C# to convert FLV to almost any video, and audio format (avi, mpeg, mp4, mp3, wave, etc.)

Disclaimer: This is a tutorial and a tool using public YouTube web services. The information, code, and programs relayed in this article is purely for technical purposes and is provided without any guarantee or intent.

 

YoutubeDownloader Source.zip (5,39 kb)

Ivolo.Tools.Youtube_win32.dll (12,50 kb)