Skip to content

Instantly share code, notes, and snippets.

@ssig33
Created January 5, 2013 03:33
Show Gist options
  • Select an option

  • Save ssig33/4459560 to your computer and use it in GitHub Desktop.

Select an option

Save ssig33/4459560 to your computer and use it in GitHub Desktop.
Convert Youtube Caption to SRT.
#coding:utf-8
require 'xmlsimple'
require 'time'
def generate_time f
hour = (f/60/60).to_i
f += -(hour*60*60)
min = (f/60).to_i
f += -(min*60)
sec = f.to_i
float = ((f-sec)*1000).to_i
"#{pad hour}:#{pad min}:#{pad sec},#{float}"
end
def pad i
i = i.to_i
if i < 10
"0#{i}"
else
i.to_s
end
end
xml = XmlSimple.xml_in(open(ARGV[0]).read)
srt = ''
xml['text'].each_with_index{|x,i|
start = x['start'].to_f
from = generate_time x['start'].to_f
to = generate_time x['start'].to_f + x['dur'].to_f
srt += <<EOS
#{i+1}
#{from} --> #{to}
#{x['content']}
EOS
}
puts srt
@ssig33

ssig33 commented Jan 5, 2013

Copy link
Copy Markdown
Author

Usage:
ruby convert #{Youtube Caption's XML} > foobar.srt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment