Downloading a Video from YouTube using C#

I'm not sure how useful this is to the average person, but if you really need that video of that Gopher looking dramatic there's an easy way to download it. It turns out that within a YouTube page, you're given everything that you would need in order to pull down the files. Specifically there is a line in there that looks like this: var swfArgs = {...}. That line of javascript holds everything we need. There are three items from that we need to use: length_seconds, t, and video_id. So we simply parse the data out and add them to an url that looks like this: http://www.youtube.com?video_id=VIDEO_ID&l=LENGTH_SECONDS&t=T. Using this url we can then pull down our video in an FLV file. So how can we automate this process?

public static void GetMovie(string FileLocation, string Url)
        {
            try
            {
                string Content = FileManager.GetFileContents(new Uri(Url));
                Regex TempRegex = new Regex("var swfArgs = {(.*)}");
                Match Match = TempRegex.Match(Content);
                Content = Match.Value;
                TempRegex = new Regex("\"video_id\": \"(.*?\")");
                string VideoLocation = "http://www.youtube.com/get_video?video_id=";
                Match = TempRegex.Match(Content);
                VideoLocation += Match.Groups[1].Value.Replace("\"", "");
                VideoLocation += "&l=";
                TempRegex = new Regex("\"length_seconds\": \"(.*?\")");
                Match = TempRegex.Match(Content);
                VideoLocation += Match.Groups[1].Value.Replace("\"", "");
                VideoLocation += "&t=";
                TempRegex = new Regex("\"t\": \"(.*?\")");
                Match = TempRegex.Match(Content);
                VideoLocation += Match.Groups[1].Value.Replace("\"", "");
                Stream TempStream;
                FileManager.GetFileContents(new Uri(VideoLocation), out TempStream);
                BinaryReader TempReader = new BinaryReader(TempStream);
                List<byte> Bytes = new List<byte>();
                while (true)
                {
                    byte[] Buffer = new byte[1024];
                    int Length = TempReader.Read(Buffer, 0, 1024);
                    for (int x = 0; x < Length; ++x)
                    {
                        Bytes.Add(Buffer[x]);
                    }
                    if (Length == 0)
                        break;
                }
                FileManager.SaveFile(Bytes.ToArray(), FileLocation);
            }
            catch { }
        }

You will notice that the code is using something called FileManager. That is a bit of code that can be found in my utility library. Basically it just downloads the files and gives it to us in a format that we can use. Past that though, it's just a bit of file parsing using Regex. That's all there is to it. So try it out, leave feedback, and happy coding.

kick it on DotNetKicks.com   Shout it
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListEmail

Posted by: James Craig
Posted on: 9/15/2009 at 1:29 PM
Tags: , , ,
Categories: C#
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading