Note this is a very old post, I suspect this doesn’t work anymore!

A random script I hacked together (through searching and experimentation) for pulling down videos from YouTube with ruby, its only guaranteed to last along as the current YouTube html formatting lasts.

Will work under windows, with some minor changes (and installation of curl and ffmpeg). Usage should be obvious, it takes the YouTube url (with watch? in it) as an argument (try quoting “”)

#!/usr/bin/ruby
require 'rubygems'
require 'hpricot'
require 'open-uri'

USER_AGENT = %{Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3}

def parse_youtube(url)
  youtube = "http://www.youtube.com/"
  url =~ /watch\?v=(.*)/
  doc = Hpricot(open("#{youtube}watch\?v=#{$1.split("&")[0]}"))
  doc.search("/html/body/").each do |foo|
  matches = URI.unescape(foo.inner_html).scan(/\|(.*?http.+?),.*?\|/)
  matches.each do |match|
    match.each do |item|
      return URI.unescape(item) if item =~ /videoplayback/
    end
    end
  end
  nil
end

if __FILE__ == $0 and ARGV[0]
  url = parse_youtube(ARGV[0])
  `curl -o \"foo.flv\" -L -A \"#{USER_AGENT}\" \"#{url}\"`
end