Saturday, August 08, 2009

Youtube HD viewing in Silverlight

 
 

Sent to you by Sparkie via Google Reader:

 
 

via Vertigo: Blog by dannyp on 8/6/09

After reading Corey Schuman's blog entry about viewing YouTube videos in Silverlight I decided to try it myself.

Here is a screenshot of Asteroid Impact being played using the Silverlight 3 Video Player from the MSDN code gallery using a YouTube stream.

 Capture

 

How do you get the URL to the video stream?

Note: I initially tried getting the URL on the client but due to YouTube's client access policy it was not possible. Instead I created a WCF web service that supplies the URL to the client.

  1. Make a HttpWebRequest to "http://www.youtube.com/get_video_info?video_id=" + videoID
  2. Read the response and parse it. The response will come back as a string and looks like a query string.
    1. The pieces of the response that you will need to parse out are the values for "token" and "fmt_map"
    2. The value for "fmt_map" looks something like "18/640000/9/0/115". A value of 18 or 22 before the first slash means that a h.264 stream is available, there are other types as well but for simplicity's sake I won't delve into them. A value of 6 means the video is only available in flash video which is unfortunately not supported by Silverlight.
  3. Make a second HttpWebRequest to "http://www.youtube.com/get_video.php?video_id=" + videoID + "&t=" + token + "&fmt=" + fmt (fmt should be set to 18 or 22 depending on what fmt_map indiciated is available)
  4. In the response, the value of the "Location" header will be the URL to video stream.

You can then set the source of a media element or media player and view the video.


 
 

Things you can do from here:

 
 

No comments: