DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Convert Smil To Srt

08.18.2008
| 840 views |
  • submit to reddit
        
lines = IO.readlines("captions2.txt")

File.open("peepcode-009-rails.srt", "w") do |f|
  n = 0
  lines.each do |line|
    if (line != "\n")
      if line =~ /\[..:..:..\...\]/
        n = n + 1
        matches = /\[(..):(..):(..)\.(..)\]/.match(line)
        hour, min, sec, usec = matches[1], matches[2], matches[3], matches[4]
        time = Time.local(2000, "jan", 1, hour.to_i, min.to_i, sec.to_i, usec.to_i)
        time2 = time + 3 # sec
        f.puts "\n"
        f.puts n
        f.puts "#{time.strftime("%H:%M:%S")},#{usec} --> #{time2.strftime("%H:%M:%S")},#{usec}" # 00:00:06,088 --> 00:00:09,282
      else
        f.puts line
      end
    end
  end
end

puts "done"