Youtube Video Downloader in Ruby

#Written by Cheng Meng
#Created on 2007-7-27

requireopen-uri‘

url_video = ""
youtube = "http://www.youtube.com/"
ARGV[0] =~ /(?:#{youtube}(?:watch\\?v=|v\\/))?(.*)$/
video_id = $1
open("#{youtube}watch\\?v=#{video_id}") do |f|
  f.each_line do |line|
    if line =~ /SWFObject\\("\\/player2.swf\\?([^"]+)"/
      url_video << "#{youtube}get_video?#{$1}"
      break
    end
  end
end
#puts "video:#{url_video}"
video = File.new("video_#{video_id}.flv","wb")
open(url_video) do |f|
  f.each_byte { |c| video.putc c }
end

This program is based on the idea explained in Peteris Krumins’s article Downloading Youtube videos with gawk.But I don’t know any gawk(巨拗口,in Chinese),and I just learn a little Ruby.So I decided to hack my own.

usage:save the above code as .rb file.This program accepts one argument to specify the url of the youtube video page.Three forms of urls are all permitted:the watch page,such as http://www.youtube.com/watch?v=En0A8KGMgq8,or http://www.youtube.com/v/En0A8KGMgq8,which usually contained in embedded code;even just the ID,En0A8KGMgq8,is OK.Then the script parses the argument,finds the actual url of video,and saves video to a “video_ID.flv” file.

Note:Linux users may need to change the mode option “wb” to “w”(I’m not sure about this,for I have not a linux system in my laptop).Only DOS/Windows needs this “b” to prevent Ruby from converting ‘\a’ to ‘\d\a’.I waste a lot of time in this problem.

Update:according to Corsair,the task of downloading should be given to more professional tools,such as wget.Just remove the last 3 lines and add “puts url_video”.Use pipe to connect my script to wget,like this:

wget -O output_filename `video_downloader.rb url`

video_downloader.rb is the modified script.

阅读(974 次)

share this post These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Slashdot

10 Comments »

  1. steven said,

    七月 27, 2007 @ 9:36 下午

    嗯,liskell的确没有新东西,但是,依我看它将是极nb的,它不仅仅是lisp,它是pure和lazy的lisp。
    fpl一直有lisp和haskell各自为代表的两种风格,前者的meta programming使得语言中几乎没有不可表达的死角,可以实现方言化的编程,扩充能力极强。后者很重要的特征是pure和lazy,有很多独一无二的表达方法是得自于这些特征,像无穷数据结构之类。二者如果结合带来的表达力恐怕是不太好估量的。
    我嘛,因为对语言有点洁癖,一直想找个理想完美的语言。最后感觉到这些特征就是最终的要求,但是我编程水平还是太差劲,加上老妈不给买本本,今年春天干脆放弃了自己做的打算。
    这个语言简直就是俺的梦中情人。。。得了,也好,要是我做,它的前途还真是颇为渺茫呢,哈哈
    另外,我怀疑这样的语言不应该现在才问世,说不定早就有了,我或许孤陋了。。

  2. VenomCK said,

    七月 27, 2007 @ 11:45 下午

    youtube now is deadly lagging in China(or especiall in my home?), i’ve to wait for loading by constantly click PLAY and PAUSE, so youtube only remains me fragil videos…

    BTW, .flv is a little bit unconvinient for the particular player, do u know why youtube chose .flv as standard?

  3. VenomCK said,

    七月 27, 2007 @ 11:46 下午

    damn convenience
    tomorrow’s iBT hope somewhat high grade than ur:)

  4. Cheng Meng said,

    七月 28, 2007 @ 12:08 上午

    我这儿还可以啊,稍微缓冲一下就好了。用我这个脚本,直接下很慢,但是可以把地址抠出来用flashget之类的下就快了。
    想在脚本里再加一行,把重定向后的地址放到剪贴板里,这样就可以直接在flashget里面下了,或者用firefox下也行
    flv比较小,其实就是flash
    good luck to your IBT,TODAY

  5. Cheng Meng said,

    七月 28, 2007 @ 12:17 上午

    @steven
    听说早一点的时候,各种functional language满天飞,都是比较学术的,没有工业化,主要出现在论文里。lazy的lisp肯定也有人做过,一些乱七八糟的小语言。好像sicp里就讲过了怎么实现laziness….

  6. steven said,

    七月 28, 2007 @ 2:45 下午

    嗯,laziness它爸有篇论文 cons不应求其参数的值,可见laziness也是从lisp里来的;据说Scheme的一个版本都差点被作成lazy的,因为考虑到活性语义模拟惰性比反过来更容易所以才选择了前者,真是很遗憾。

  7. Youtube Video URL Extractor in Python said,

    七月 28, 2007 @ 4:05 下午

    […] Cheng Meng 的那个 Youtube downloader,Ruby 写的,由于对 Ruby 的无知,一点也看不懂,所以就参考 DownloadYoutube […]

  8. Cheng Meng said,

    七月 28, 2007 @ 5:06 下午

    @steven
    活性语义模拟求值?再问一个,是不是就是call by name和call by value啊?确实在scheme里摸拟laziness很容易,把一个需要lazy evaluation的expression用lambda wrap起来就可以了。当时看到这个,觉得真是鬼斧神工

  9. steven said,

    七月 31, 2007 @ 11:07 上午

    说得怪痒痒的,装上了Scheme,看SICP中。
    活性语义和call by value应该没啥区别。这方面术语乱七八糟,这个活性什么的是从一学究的书上传染来的,由于挺吓人就被我记住了。。^_^

  10. jessie said,

    十月 2, 2007 @ 12:37 上午

    opp

RSS feed for comments on this post · TrackBack URI

Leave a Comment