Module: Formats
- Includes:
- PlatformEndLine
- Included in:
- SubtitleIt::Subtitle
- Defined in:
- lib/subtitle_it/formats/ass.rb,
lib/subtitle_it/formats/mpl.rb,
lib/subtitle_it/formats/rsb.rb,
lib/subtitle_it/formats/srt.rb,
lib/subtitle_it/formats/sub.rb,
lib/subtitle_it/formats/xml.rb,
lib/subtitle_it/formats/yml.rb
Overview
SubtitleIt MPSub, MicroDVD or VOBSub format TODO: tricky.. detect which format we got.
10251115You always say that.|The same thing every time. 11181177“l’m throug h, never again,|too dangerous.”
MicroDVD: 1125.000 24472513You should come to the Drama Club, too. 25132594Yeah. The Drama Club is worried|that you haven’t been coming. 26032675I 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
Instance Method Summary collapse
- #parse_ass ⇒ Object
- #parse_mpl ⇒ Object
- #parse_rsb ⇒ Object
- #parse_srt ⇒ Object
- #parse_sub ⇒ Object
- #parse_time(n) ⇒ Object
- #parse_xml ⇒ Object
- #parse_yml ⇒ Object
-
#ratio ⇒ Object
between our formats, what changes can be reduced to a value.
-
#to_ass ⇒ Object
not mine!.
- #to_mpl ⇒ Object
- #to_rsb ⇒ Object
- #to_srt ⇒ Object
- #to_sub ⇒ Object
- #to_xml ⇒ Object
- #to_yml ⇒ Object
- #xml_lines ⇒ Object
Methods included from PlatformEndLine
Instance Method Details
#parse_ass ⇒ Object
6 7 8 |
# File 'lib/subtitle_it/formats/ass.rb', line 6 def parse_ass end |
#parse_mpl ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/subtitle_it/formats/mpl.rb', line 10 def parse_mpl @raw.to_a.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 |
#parse_rsb ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/subtitle_it/formats/rsb.rb', line 13 def parse_rsb inn = @raw.to_a @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 { |t| t.strip } text = line.split('==')[1] #.strip final << Subline.new(time_on, time_off, text ? text.strip : nil) end end |
#parse_srt ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/subtitle_it/formats/srt.rb', line 13 def parse_srt endl = endline( @raw ) @raw.split( endl*2 ).inject([]) do |final,line| line = line.split(endl) line.delete_at(0) time_on,time_off = line[0].split('-->').map { |t| t.strip } line.delete_at(0) text = line.join("|") final << Subline.new(time_on, time_off, text) end end |
#parse_sub ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/subtitle_it/formats/sub.rb', line 25 def parse_sub # FIXME: 1.8 and 1.9 way of working @raw.send(@raw.respond_to?(:lines) ? :lines : :to_a).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 |
#parse_time(n) ⇒ Object
47 48 49 |
# File 'lib/subtitle_it/formats/sub.rb', line 47 def parse_time(n) n.to_i / 1000 * @fps * ratio end |
#parse_xml ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/subtitle_it/formats/xml.rb', line 26 def parse_xml final = [] doc = Hpricot.XML(@raw) (doc/'tt'/'p').each do |line| time_on, time_off = ['begin', 'dur'].map { |str| line[str.to_sym] } text = line.innerHTML final << Subline.new(time_on,time_off,text) end return final end |
#parse_yml ⇒ Object
6 7 8 9 10 11 12 13 |
# 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 |
#ratio ⇒ Object
between our formats, what changes can be reduced to a value
21 22 23 |
# File 'lib/subtitle_it/formats/sub.rb', line 21 def ratio 1 end |
#to_ass ⇒ Object
not mine!
11 12 |
# File 'lib/subtitle_it/formats/ass.rb', line 11 def to_ass end |
#to_mpl ⇒ Object
20 21 22 23 24 25 26 27 28 |
# 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 return line_ary.join( endl ) + endl end |
#to_rsb ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# 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 return 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 |
#to_srt ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/subtitle_it/formats/srt.rb', line 25 def to_srt endl = endline( @raw ) 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.gsub("|", endl) + endl end out.join( endl ) end |
#to_sub ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/subtitle_it/formats/sub.rb', line 38 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 return line_ary.join( endl ) + endl end |
#to_xml ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/subtitle_it/formats/xml.rb', line 47 def to_xml endl = endline( @raw ) out = <<XML <?xml version="1.0" encoding="UTF-8"?> <tt xml:lang="en" xmlns="http://www.w3.org/2006/10/ttaf1" xmlns:tts="http://www.w3.org/2006/10/ttaf1#styling"> <head> <styling>#{@style + endl if @style} </styling> </head> <body> <div xml:lang="en"> #{xml_lines} </div> </body> </tt> XML out.chomp end |
#to_yml ⇒ Object
15 16 17 |
# File 'lib/subtitle_it/formats/yml.rb', line 15 def to_yml YAML.dump(self) end |
#xml_lines ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/subtitle_it/formats/xml.rb', line 37 def xml_lines endl = endline( @raw ) line_ary = [] @lines.each do |l| toff = l.time_off - l.time_on line_ary << " <p begin=\"#{l.time_on}\" dur=\"#{toff}\">#{l.text}</p>" end return line_ary.join( endl ) end |