module Formats
ASS - http://en.wikipedia.org/wiki/SubStation_Alpha http://www.malakith.net/aegiwiki/ASS
TODO: ass!
SubtitleIt
MPL2 - extension .txt
[1025]You always say that.|The same thing every time.
Where N is sec * 10
SubtitleIt
RSB - Ruby Subtitle
00:32 => 00:33 Nice police work! | Thank you! 00:53 => N == Howdy ho! MM:SS => N == TEXT | NEWLINE
Where N is the seconds to last.
SubtitleIt
SRT - Subrip format
N 00:55:21,600 –> 00:55:27,197 lt's not even 20 years. You've sold the casinos and made fortunes for all of us.
Where N is the sub index number
SubtitleIt
MPSub, MicroDVD or VOBSub format TODO: tricky.. detect which format we got.
{1025}{1115}You always say that.|The same thing every time. {1118}{1177}“l'm throug h, never again,|too dangerous.”
MicroDVD: {1}{1}25.000 {2447}{2513}You should come to the Drama Club, too. {2513}{2594}Yeah. The Drama Club is worried|that you haven't been coming. {2603}{2675}I see. Sorry, I'll drop by next time.
Where N is ms / framerate / 1000 (ms -> s)
parts of the code from 'simplesubtitler' from Marcin (tiraeth) Chwedziak
SubtitleIt
YML Dump
Public Instance Methods
# File lib/subtitle_it/formats/srt.rb, line 14 def endl endline(@raw) end
# File lib/subtitle_it/formats/ass.rb, line 6 def parse_ass end
# File lib/subtitle_it/formats/mpl.rb, line 10 def parse_mpl @raw.lines.inject([]) do |i, l| line_data = l.scan(/^\[([0-9]{1,})\]\[([0-9]{1,})\](.+)$/) line_data = line_data.at 0 time_on, time_off, text = line_data time_on, time_off = [time_on.to_i, time_off.to_i].map { |t| t.to_i * 1000 } i << Subline.new(time_on, time_off, text.chomp) end end
# File lib/subtitle_it/formats/rsb.rb, line 13 def parse_rsb inn = @raw.lines @title = inn.delete_at(0).split(':')[1] @authors = inn.delete_at(0).split(':')[1] @version = inn.delete_at(0).split(':')[1] inn.inject([]) do |final, line| time_on, time_off = line.split('=>').map(&:strip) text = line.split('==')[1] # .strip final << Subline.new(time_on, time_off, text ? text.strip : nil) end end
# File lib/subtitle_it/formats/srt.rb, line 18 def parse_srt @raw.split(endl * 2).inject([]) do |final, line| line = line.split(endl) line.delete_at(0) time_on, time_off = line[0].split('-->').map(&:strip) line.delete_at(0) text = line.join('|') final << Subline.new(time_on, time_off, text) end end
# File lib/subtitle_it/formats/sub.rb, line 25 def parse_sub @raw.lines.reduce([]) do |i, l| line_data = l.scan(/^\{([0-9]{1,})\}\{([0-9]{1,})\}(.+)$/) line_data = line_data.at 0 time_on, time_off, text = line_data time_on, time_off = [time_on.to_i, time_off.to_i].map do |t| (t.to_i / @fps * 1000 / ratio).to_i end i << Subline.new(time_on, time_off, text ? text.chomp : nil) end end
# File lib/subtitle_it/formats/sub.rb, line 46 def parse_time(n) n.to_i / 1000 * @fps * ratio end
# File lib/subtitle_it/formats/yml.rb, line 6 def parse_yml @yaml = YAML.load(@raw) header = @yaml.delete('header') @title = header['title'] @author = header['authors'] @version = header['version'] @yaml['lines'].map { |l| Subline.new(l[0], l[1], l[2]) } end
between our formats, what changes can be reduced to a value
# File lib/subtitle_it/formats/sub.rb, line 21 def ratio 1 end
not mine!
# File lib/subtitle_it/formats/ass.rb, line 10 def to_ass end
# File lib/subtitle_it/formats/mpl.rb, line 20 def to_mpl endl = endline(@raw) line_ary = [] @lines.each do |l| start, stop = [l.time_on, l.time_off].map { |val| val.to_i / 100 } line_ary << '[%d][%d]%s' % [start, stop, l.text] end line_ary.join(endl) + endl end
# File lib/subtitle_it/formats/rsb.rb, line 25 def to_rsb endl = endline(@raw) out = ["- title: #{@title}", "- authors: #{@authors}", "- version: #{@version}"] @lines.each do |l| out << '%s => %s == %s' % [l.time_on.to_s, l.time_off.to_s, l.text] end out.join(endl) + endl # out = "- title: #{@title}\n- authors: #{@authors}\n- version: #{@version}\n" # out << @lines.inject([]) do |i,l| # i << "%s => %s == %s" % [l.time_on.to_s, l.time_off.to_s, l.text] # end.join("\n") end
# File lib/subtitle_it/formats/srt.rb, line 29 def to_srt out = [] @lines.each_with_index do |l, i| out << "#{i + 1}" out << '%s --> %s' % [l.time_on.to_s(','), l.time_off.to_s(',')] out << (l.text ? l.text.gsub('|', endl) : ' ') + endl end out.join(endl) end
# File lib/subtitle_it/formats/sub.rb, line 37 def to_sub endl = endline(@raw) line_ary = [] @lines.each do |l| line_ary << '{%d}{%d}%s' % [parse_time(l.time_on), parse_time(l.time_off), l.text] end line_ary.join(endl) + endl end
# File lib/subtitle_it/formats/yml.rb, line 15 def to_yml YAML.dump(self) end