Rorcraft Blog

Download youtube videos with ruby

As you might know, we’ve released Ankoder.net for while now. It lets anyone to download videos into their iPods and various other formats. Scraping the flv url from Youtube HTML isnt exactly easy.

This is how we do it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  def parse_youtube(url)
     youtube = "http://www.youtube.com/"
     # url =~ /(?:http\:\/\/.*youtube.com\/(?:watch\?v=|v\/))?(.*)$/
     url =~ /watch\?v=(.*)/ 
     video_id = $1
     video_id = video_id.split("&")[0]
     flv_url = nil
     open("#{youtube}watch\?v=#{video_id}") do |f|
       f.each_line do |line|
         if line =~ /watch_fullscreen\?(.*?)video_id=([\w-]+)&(.*?)&t=([\w-]+)&/
           # p line
           flv_url = "#{youtube}get_video?video_id=#{$2}&t=#{$4};auto"
           break
         end
       end
     end
     flv_url
  end

USER_AGENT = %{Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.11) 
Gecko/20071231 Firefox/2.0.0.11 Flock/1.0.5}

IO.popen("curl -o \"#{file_name}\" -L -A \"#{USER_AGENT}\" \"#{parse_youtube(url)}\"  2>&1")
blog comments powered by Disqus

Contact

We love to hear about your web projects.
Email:
Sydney: +61 421 591 943
Hong Kong:+852 6901 2682

Categories