Skip to content

Instantly share code, notes, and snippets.

@Ruin0x11
Created May 12, 2016 03:16
Show Gist options
  • Save Ruin0x11/13b72b8d52fb913997bffe8be5a083a9 to your computer and use it in GitHub Desktop.
Save Ruin0x11/13b72b8d52fb913997bffe8be5a083a9 to your computer and use it in GitHub Desktop.
download niconico video + comments
#!/usr/bin/env ruby
require 'niconico'
require 'streamio-ffmpeg'
require 'optparse'
require 'yaml'
# also requires danmaku2ass
VIDEO_URL = /https?:\/\/(?:www\.|secure\.)?nicovideo\.jp\/watch\/((?:[a-z])+?[0-9]+)/
count = 1000
ARGV.options do |opts|
opts.on("-c", "--count=val", Integer) { |val| count = val }
opts.parse!
end
url = ARGV.pop
raise "No URL given" unless url
if url =~ VIDEO_URL
id = $1
end
raise "Could not parse ID" unless id
puts "Logging in..."
user_id = YAML.load_file(File.join(ENV['HOME'], "nv.yml"))
nv = Niconico.new(user_id["mail"], user_id["password"])
nv.login
v = nv.video(id)
name = "#{id}-#{v.title}"
filename = "#{name}.#{v.type}"
puts "Downloading #{id}..."
File.open("#{filename}", 'w') { |file| file.write(v.get_video) }
puts "Downloading comments..."
File.open("#{name}.xml", 'w') { |file| file.write(v.get_comment(count)) }
ff = FFMPEG::Movie.new("./#{filename}")
video_width = ff.width
video_height = ff.height
# I can't bring myself to rewrite this all over again...
`danmaku2ass \"#{name}.xml\" -s #{video_width}x#{video_height} -o \"#{name}.ass\"`
File.unlink("#{name}.xml")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment