Forked from lsloan/ Convert video subtitles from YouTube XML format to SubRip (.srt)
Created
November 2, 2015 16:03
-
-
Save Ema-Mu/be96264202014d9979cf to your computer and use it in GitHub Desktop.
Convert XML Youtube subtitles to SubRip (srt)
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