Wednesday, January 24, 2007

GetVideoURL and GetVideoName from Youtube

Update: 1/15/2009 YouTube made breaking changes to their site, the code below no longer works.

The function GetVideoURL returns the direct URL to the movie file in a Youtube page by passing the page's HTML source.

Private Function GetVideoURL(ByVal htmlSource As String) As String
'Youtube's url to retrieve the video
Dim targetUrl As String = ""
'locate the src url passed to the flash player
Dim regexpFindID = New Regex("/player2.swf?((.\n)*?)"",", RegexOptions.IgnoreCase)
Dim matchID As Match
matchID = regexpFindID.Match(htmlSource)
If matchID.Success Then
'extract the videoID query string
Dim vidID As String
vidID = matchID.Value.Substring(13, matchID.Value.Length - 15)
Return targetUrl + vidID
Return Nothing
End If
End Function

Private Function GetVideoName(ByVal htmlSource As String) As String
'locate the title based on the div tag
Dim regexpFindName = New Regex("video_title"">((.\n)*?)", RegexOptions.IgnoreCase)
Dim matchName As Match
matchName = regexpFindName.Match(htmlSource)
If matchName.Success Then
'extract the title
Dim vidName As String
vidName = matchName.Value.Substring(13, matchName.Value.Length - 18)
'remove common invalid file system chars & html syntax
Dim arrInvalids As String() = {"\", "/", ":", "?", "<", ">", "", " ", "<", ">", "&", """}
Dim invalid As String
For Each invalid In arrInvalids
If vidName.Contains(invalid) Then
vidName = vidName.Replace(invalid, "")
End If
Return vidName
Return Nothing
End If
End Function

Xido is just an IE control using these functions along with threaded asynchronous calls using the Net.WebClient object to download the files (the download limit is by design to prevent abuse).
Many people are now focusing on these kinds of applications, both running as clients or web applications and are including video transcoding as well as other features.

By contrast Xido is only a small application built out of boredom using these old functions which I had once written out of my frustration while using Sites like KeepVid forced me to copy/paste URLs and manually rename every single file. Xido was only meant to alleviate this problem and should be considered defunct until an interesting challenge comes up using embedded Flash video.

Go build your own rip site like :

Someone even built a webservice that does this same thing (I still don't know why).

No comments: