Skip to content

Instantly share code, notes, and snippets.

@lfzawacki
Created December 21, 2012 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lfzawacki/4350000 to your computer and use it in GitHub Desktop.
Save lfzawacki/4350000 to your computer and use it in GitHub Desktop.
Ever needed to get all thumbnails for a youtube video?
#!/usr/bin/ruby
require 'open-uri'
def classy_exit
puts "I need a youtube video URL"
exit 0
end
def get_video_id url
# papa loves you
reg = /((?:https?\:\/\/)?(?:www\.)?youtube(?:-nocookie)?.com(?:\/(?:v|embed)\/([a-zA-Z0-9_-]+))|(?:watch\?.*v=([a-zA-Z0-9_-]+))).*/
data = reg.match url
data[2] || data[3] if data
end
classy_exit if ARGV[0].nil?
id = get_video_id(ARGV[0])
classy_exit if id.nil?
Dir.mkdir(id)
(0..5).each do |i|
filename = "#{i}.jpg"
begin
content = open("http://img.youtube.com/vi/#{id}/#{filename}").read
File.open(File.join(id, filename), "wb") { |f| f << content }
puts "Created #{filename}"
rescue OpenURI::HTTPError
# some videos wont have all thumbs
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment