Last active
August 29, 2015 14:22
-
-
Save kubido/e06afa00817af9372874 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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