Module: SubtitleIt::Formats

Included in:
SubtitleIt
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

Instance Method Summary collapse

Instance Method Details

#parse_assObject



7
8
9
# File 'lib/subtitle_it/formats/ass.rb', line 7

def parse_ass

end

#parse_mplObject



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
	text_on, text_off, text = line_data
	text_on, text_off = [text_on.to_i, text_off.to_i].map { |t| t.to_i * 1000 }	    
    i << Subline.new(text_on, text_off, text.chomp)
  end
end

#parse_rsbObject



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|
    text_on,text_off = line.split('=>').map { |t| t.strip }
    text = line.split('==')[1].strip
    final << Subline.new(text_on, text_off, text)
  end
end

#parse_srtObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/subtitle_it/formats/srt.rb', line 13

def parse_srt 
  @raw.split(/\n\n/).inject([]) do |final,line|
    line = line.split(/\n/)
    line.delete_at(0)
    text_on,text_off = line[0].split('-->').map { |t| t.strip }
    line.delete_at(0)        
    text = line.join("|")
    final << Subline.new(text_on, text_off, text)
  end
end

#parse_subObject



27
28
29
30
31
32
33
34
35
# File 'lib/subtitle_it/formats/sub.rb', line 27

def parse_sub
  @raw.to_a.inject([]) do |i,l|
	line_data = l.scan(/^\{([0-9]{1,})\}\{([0-9]{1,})\}(.+)$/)
	line_data = line_data.at 0
	text_on, text_off, text = line_data
	text_on, text_off = [text_on.to_i, text_off.to_i].map { |t| (t.to_i/@fps*1000).to_i }	    
    i << Subline.new(text_on, text_off, text.chomp)
  end
end

#parse_xmlObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/subtitle_it/formats/xml.rb', line 26

def parse_xml
  final = []
  doc = Hpricot.XML(@raw)
 # p (doc/'tt'/'p').first[:dur]#inspect
  (doc/'tt'/'p').each do |line|
    text_on = line[:begin]
    text_off = line[:dur]
    text = line.innerHTML
    final << Subline.new(text_on,text_off,text)
  end
  final
end

#parse_ymlObject



7
8
9
10
11
12
13
14
# File 'lib/subtitle_it/formats/yml.rb', line 7

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

#ratioObject

between our formats, what changes can be reduced to a value



21
22
23
24
# File 'lib/subtitle_it/formats/sub.rb', line 21

def ratio
 1
  
end

#to_assObject

not mine!



12
13
# File 'lib/subtitle_it/formats/ass.rb', line 12

def to_ass
end

#to_mplObject



20
21
22
23
24
25
26
# File 'lib/subtitle_it/formats/mpl.rb', line 20

def to_mpl
  @lines.inject([]) do |i,l|
    start = l.text_on.to_i / 100
    stop = l.text_off.to_i / 100
    i << "[%d][%d]%s" % [start, stop, l.text]
  end.join("\n")
end

#to_rsbObject



25
26
27
28
29
30
# File 'lib/subtitle_it/formats/rsb.rb', line 25

def to_rsb
  out = "- title: #{@title}\n- authors: #{@authors}\n- version: #{@version}\n"
  out << @lines.inject([]) do |i,l|
    i << "%s => %s == %s" % [l.text_on.to_s, l.text_off.to_s, l.text]
  end.join("\n")      
end

#to_srtObject



24
25
26
27
28
29
30
31
32
# File 'lib/subtitle_it/formats/srt.rb', line 24

def to_srt
  out = []
  @lines.each_with_index do |l,i|
    out << "#{i}"
    out << "%s --> %s" % [l.text_on.to_s, l.text_off.to_s]
    out << l.text.gsub("|","\n")
  end
  out.join("\n")
end

#to_subObject



37
38
39
40
41
42
43
# File 'lib/subtitle_it/formats/sub.rb', line 37

def to_sub
  @lines.inject([]) do |i,l|
    start = l.text_on.to_i / 1000 * @fps * ratio
    stop = l.text_off.to_i / 1000 * @fps * ratio
    i << "{%d}{%d}%s" % [start, stop, l.text]
  end.join("\r\n")
end

#to_xmlObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/subtitle_it/formats/xml.rb', line 46

def to_xml
  out = <<XML
<?xml version="1.0" encoding="UTF-8"?>
<tt xml:lang="en" xmlns="http://www.w3.org/2006/04/ttaf1"  xmlns:tts="http://www.w3.org/2006/04/ttaf1#styling">
  <head>
<styling>#{@style + "\n" if @style}
</styling>
  </head>
  <body>
<div xml:lang="en">
#{xml_lines}                
</div>
  </body>
</tt>
XML
out.chomp
end

#to_ymlObject



16
17
18
# File 'lib/subtitle_it/formats/yml.rb', line 16

def to_yml
  YAML.dump(self)
end

#xml_linesObject



39
40
41
42
43
44
# File 'lib/subtitle_it/formats/xml.rb', line 39

def xml_lines
  @lines.inject([]) do |i,l| 
    toff = l.text_off - l.text_on
    i << "      <p begin=\"#{l.text_on}\" dur=\"#{toff}\">#{l.text}</p>"
  end.join("\n")
end