Created
January 5, 2013 03:33
-
-
Save ssig33/4459560 to your computer and use it in GitHub Desktop.
Convert Youtube Caption to SRT.
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
| #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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
ruby convert #{Youtube Caption's XML} > foobar.srt