Forked from lsloan/ Convert video subtitles from YouTube XML format to SubRip (.srt)
Created
July 16, 2015 09:46
-
-
Save viliam-durina/4f1d63129922823dee2a to your computer and use it in GitHub Desktop.
This file contains 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
# Convert XML Youtube subtitles to SubRip (srt) format | |
# To download the subtitle in XML, put de code of the Youtube video | |
# at the end of the next url: | |
# http://video.google.com/timedtext?hl=en&lang=en&v= | |
require 'rubygems' | |
require 'hpricot' | |
youtube_xml = ARGV[0] | |
xml = Hpricot.XML(open(youtube_xml)) | |
srt = File.open(ARGV[1] || youtube_xml.gsub('.xml', '.srt'), 'w+') | |
i = 1 | |
(xml/'text').each do |sub| | |
ini = Time.at(sub['start'].to_f) | |
fi = ini + sub['dur'].to_f | |
srt.write( | |
%{#{i} | |
#{'%02d:%02d:%02d,%03d' % [ini.hour-1, ini.min, ini.sec, ini.usec/1000]} --> #{'%02d:%02d:%02d,%03d' % [fi.hour-1, fi.min, fi.sec, fi.usec/1000]} | |
#{sub.inner_text} | |
}) | |
i += 1 | |
end | |
srt.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment