Skip to content

Instantly share code, notes, and snippets.

@kubido
Last active August 29, 2015 14:22
Show Gist options
  • Save kubido/e06afa00817af9372874 to your computer and use it in GitHub Desktop.
Save kubido/e06afa00817af9372874 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'nokogiri'
youtube_xml = ARGV[0]
xml = File.open(ARGV[0])
srt = File.open(ARGV[1] || youtube_xml.gsub('.xml', '.srt'), 'w+')
doc = Nokogiri::XML(xml)
doc.children.children.each_with_index do |text, idx|
ini = Time.at(text.attributes['start'].value.to_f)
fi = ini + text.attributes['dur'].value.to_f
tz = 7 #your timezone, mine is +7
srt.write("#{idx+1}\n")
srt.write("#{'%02d:%02d:%02d,%03d' % [ini.hour-tz, ini.min, ini.sec, ini.usec/1000]} --> #{'%02d:%02d:%02d,%03d' % [fi.hour-tz, fi.min, fi.sec, fi.usec/1000]}")
srt.write("\n#{text.children.text}\n\n")
end
srt.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment