+1.916.577.1977 | Downloads | Buy | Register | Login
 Search  
Tuesday, July 08, 2008
Search Blogs
 

Available Blogs
 

Previous Blogs
 

Technorati
 
More blogs about coversant.

About Coversant
 

YouTube Getter
 
Location: BlogsStarin' at the Wall    
Posted by: JD Conley 4/5/2007 6:13 PM

Usually everything I post here is serious business. Well, not today! Today, we're going to look at how to get the raw video stream for YouTube videos. Why, you ask? Well, curiosity, mostly. :) Some people like sports, I like problem solving. There are a number of browser plug-ins and web sites that do this already, but hey, it was still fun.

YouTube offers a bit of an API for developers to mess with. It's mostly for grabbing sets of videos and preview images and such. They also let you embed their player in your pages. That's awfully nice of them, but what if I don't like their player? What if I want a sexier player? Well, if you've got something that can play/convert Flash Video (FLV's), here's the full C# code to grab the FLV URI (pieces dissected below):

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
using System.Collections.Specialized;

namespace JDFun
{
    public static class YouTubeGetter
    {
        public static Uri GetFlvUri(string viewUri)
        {
            return GetFlvUri(new Uri(viewUri));
        }

        public static Uri GetWatchUri(Uri flvUri)
        {
            NameValueCollection qry = HttpUtility.ParseQueryString(flvUri.Query);

            string videoID = qry["video_id"];
            string watchUri = string.Concat("http://www.youtube.com/watch?v=", HttpUtility.UrlEncode(videoID));

            return new Uri(watchUri);
        }

        public static Uri GetImageUri(Uri flvUri)
        {
            NameValueCollection qry = HttpUtility.ParseQueryString(flvUri.Query);

            string imageUri = qry["iurl"];

            return new Uri(imageUri);
        }

        public static Uri GetFlvUri(Uri viewUri)
        {
            // so either i've got the embed link or the watch link

            //watch link: http://www.youtube.com/watch?v=up-RX_YN7yA
            //embed link: http://www.youtube.com/v/up-RX_YN7yA

            string toQuery = null;

            NameValueCollection queryString = HttpUtility.ParseQueryString(viewUri.Query);

            string videoId = queryString["v"];

            if (null != videoId)
            {
                toQuery = string.Concat("http://www.youtube.com/v/", videoId);
            }
            else
            {
                toQuery = viewUri.ToString();
            }

            if (null == toQuery)
                throw new InvalidOperationException("Not a valid YouTube Uri.");

            Uri queryUri = new Uri(toQuery);
            //ok we have the uri to query, now go there and get redirected.
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(queryUri);
            req.AllowAutoRedirect = true;

            // make them think we're coming from a direct link
            req.Referer = string.Empty;

            // firefox rules!
            req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3";
            using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
            {
                string absoluteRedirectLocation = resp.ResponseUri.AbsoluteUri;

                //replace the swf with the get_video request
                if (!absoluteRedirectLocation.Contains("/p.swf?"))
                    throw new InvalidOperationException("Unrecognized Uri. YouTube changed something.");

                absoluteRedirectLocation = absoluteRedirectLocation.Replace("/p.swf?", "/get_video?");

                //return the absolute URI for this request
                return new Uri(absoluteRedirectLocation);
            }
        }
    }
}

Ok, wow, that's a lot bigger than I remember. Here's the basic idea.

  1. Get the video ID from the given URI. The URI could be the "watch" URI, or it could be the URI from the embed code they give you.
  2. Load the video directly based on the video ID, at the embed URI.
  3. Make a HTTP Request and get redirected to another URI, (which happens to contain the Image URI, and a special token "t").
  4. Know the magic URI where they keep the video streams (I cheated) and use it.

Now wasn't that fun? :)

Permalink |  Trackback

Comments (9)  
Re: YouTube Getter    By Daniel on 7/8/2007 10:00 PM
This script does not work anymore.

viagra online    By TrackBack on 11/29/2007 4:13 PM
Erectile dysfunction meds at genericameds.com
# viagra online

exercises for fast weight loss    By TrackBack on 11/29/2007 4:13 PM
Thank you. weight-loss-pages.com
# exercises for fast weight loss

hypnosis    By TrackBack on 11/29/2007 4:13 PM
The most efficient way to track down this info is to apply all means and resources at your disposal.
# hypnosis

effective at home exercises for weight loss    By TrackBack on 11/29/2007 4:13 PM
Thank you. weight-loss-pages.com
# effective at home exercises for weight loss

Re: YouTube Getter    By Matilda on 11/29/2007 4:13 PM
Guide - How to save/download video from youtube
Guide - How to watch youtube video on MP4 Player

Why you should download/save videos from youtube :
You found an interesting video from youtube and you want to save/download it to your computer and later share it with your friend or keep it as a collection. However, by default Youtube doesn't allow users to save/download the videos. It only allow user to send a hyperlink to the friends and invite them to go to youtube website to view the videos.

why not save/download the video before it's too late ?

Steps to save/download the video from youtube

For Internet Explorer (PC) users

1) Click your favourite video clip, you can see the URL in your browser becomes something like this : http://www.youtube.com/watch?v=qCcCEqD117U

2) highlight the URL, "copy" & "paste" to Youtube Robot url and click "+" button
or move mouse to the download link, right click the mouse use "menu"->"download by youtube robot"
or drag the url to youtuberobot's floating window

4) When the download link shows, the flv video file will auto download to your local computer

5) You can double click to view the downloaded video file

6) Youtube Robot intergrate with FLV Converter,you can select output format first then click convert button to convet the downloaded video file to your format.

7) You can plup-in IPod, PSP to usb port,after convert finished Youtuberobot will Auto transfer video to iPod, PSP,iPhone, Pocket PC, or Zune


Product page: http://www.youtuberobot.com
Direct download link: http://www.youtuberobot.com/download/utuberobot.exe
Company web-site: http://www.youtuberobot.com
E-mail: support@youtuberobot.com

Re: YouTube Getter    By Matilda on 11/29/2007 4:13 PM
Guide - How to save/download video from youtube
Guide - How to watch youtube video on MP4 Player

Why you should download/save videos from youtube :
You found an interesting video from youtube and you want to save/download it to your computer and later share it with your friend or keep it as a collection. However, by default Youtube doesn't allow users to save/download the videos. It only allow user to send a hyperlink to the friends and invite them to go to youtube website to view the videos.

why not save/download the video before it's too late ?

Steps to save/download the video from youtube

For Internet Explorer (PC) users

1) Click your favourite video clip, you can see the URL in your browser becomes something like this : http://www.youtube.com/watch?v=qCcCEqD117U

2) highlight the URL, "copy" & "paste" to Youtube Robot url and click "+" button
or move mouse to the download link, right click the mouse use "menu"->"download by youtube robot"
or drag the url to youtuberobot's floating window

4) When the download link shows, the flv video file will auto download to your local computer

5) You can double click to view the downloaded video file

6) Youtube Robot intergrate with FLV Converter,you can select output format first then click convert button to convet the downloaded video file to your format.

7) You can plup-in IPod, PSP to usb port,after convert finished Youtuberobot will Auto transfer video to iPod, PSP,iPhone, Pocket PC, or Zune


Product page: http://www.youtuberobot.com
Direct download link: http://www.youtuberobot.com/download/utuberobot.exe
Company web-site: http://www.youtuberobot.com
E-mail: support@youtuberobot.com

comprare generico cialis Senza ricetta    By TrackBack on 11/29/2007 4:13 PM
d0f671bcf46b94325c99f3841c828bd7 La disfunzione erettile Farmacia in linea Italia http://it.genericameds.com
# comprare generico cialis Senza ricetta

Re: YouTube Getter    By rs-sky on 11/29/2007 4:13 PM
rs-sky is an professional store for runescape gold,items,money,accounts,powerleveling,questqoint,runes and some other goods with fast delivery and world class service.
runescape gold

runescape money


©2008 Coversant, Inc. | Privacy Policy | About Coversant | Contact Info