Skip to content

Instantly share code, notes, and snippets.

@brettbuddin
Created April 20, 2011 17:09
Show Gist options
  • Save brettbuddin/931985 to your computer and use it in GitHub Desktop.
Save brettbuddin/931985 to your computer and use it in GitHub Desktop.
Grabs the H.264 version of a video from YouTube.
require 'curb'
require 'cgi'
def youtube_url(video_id)
c = Curl::Easy.new("http://www.youtube.com/watch?v=#{video_id}")
c.cookies = 'PREF: f2=40000000;'
c.perform
content = c.body_str
matches = content.match(/"fmt_url_map":\s"([^"]*)"/)
if matches
matches[1].split(',').each do |line|
parts = line.split('|')
id = parts[0].to_i
if id == 18
link = parts[1]
link.gsub!(/\\u0026/, '&')
link.gsub!(/\\\//, '/')
return CGI::unescape(link)
end
end
end
nil
end
puts youtube_url("NJOIR0slR_c")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment