Created
April 20, 2011 17:09
-
-
Save brettbuddin/931985 to your computer and use it in GitHub Desktop.
Grabs the H.264 version of a video from YouTube.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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