Class: SubRipReader

Inherits:
Object
  • Object
show all
Includes:
Patterns
Defined in:
lib/subtitle-library/reader.rb

Constant Summary

Constants included from Patterns

Patterns::MICRO_DVD_LINE, Patterns::SUBVIEWER_LINE, Patterns::SUBVIEWER_METADATA, Patterns::SUB_RIP_LINE, Patterns::SUB_RIP_TIMING

Instance Method Summary collapse

Constructor Details

#initialize(subs_path) ⇒ SubRipReader

Returns a new instance of SubRipReader.



55
56
57
58
# File 'lib/subtitle-library/reader.rb', line 55

def initialize(subs_path)
  @subs_path = subs_path
  @fps = 23.976
end

Instance Method Details

#check_timing(start_time, end_time, last_end_time, error_log, check_syntax, actual_lines) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/subtitle-library/reader.rb', line 130

def check_timing(start_time, end_time, last_end_time, error_log, check_syntax, actual_lines)
  if start_time.year + start_time.month + start_time.day +
    end_time.year + end_time.month + end_time.day != 6 or
      start_time >= end_time or start_time < last_end_time
        if check_syntax
          error_log += "Invalid timing at line #{actual_lines}.\n"
        else
          puts "Invalid timing at line #{actual_lines}.\n"
        end
        return [false, error_log]
  end
  [true, error_log]
end

#parse_timing(line) ⇒ Object



161
162
163
164
165
166
167
168
169
170
# File 'lib/subtitle-library/reader.rb', line 161

def parse_timing(line)
  match = SUB_RIP_TIMING.match line
  time_args = [1, 1, 1] + match.to_s.split(/,|:/).collect(&:to_i)
  time_args[6] *= 1000
  start_time = Time.mktime *time_args
  match = SUB_RIP_TIMING.match match.post_match
  time_args = [1, 1, 1] + match.to_s.split(/,|:/).collect(&:to_i)
  time_args[6] *= 1000
  [start_time, Time.mktime(*time_args)]
end

#read_subs(check_syntax) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/subtitle-library/reader.rb', line 60

def read_subs(check_syntax)
  cues = []
  actual_lines = 0
  error_log = ""
  is_eof = false
  last_end_time = Time.new 1, 1, 1
  File.open(@subs_path, 'r') do |subs|
    while true
      is_eof, actual_lines, strip_line = read_until_timing(subs, actual_lines)
      break if is_eof
      if SUB_RIP_LINE =~ strip_line
        start_time, end_time = parse_timing strip_line
        valid_timing, error_log = check_timing start_time, end_time, last_end_time, error_log, check_syntax, actual_lines
        unless valid_timing
          is_eof, actual_lines, strip_line = read_until_index subs, actual_lines, strip_line, false
          break if is_eof
          next
        end
        last_end_time = end_time
        line = subs.gets
        break unless line
        actual_lines += 1
        text = line.strip
        strip_line = line.strip
        while strip_line != ''
          line = subs.gets
          unless line
            is_eof = true
            break
          end
          actual_lines += 1
          strip_line = line.strip
          text += "\n" + strip_line
        end
        if is_eof
          cues << Cue.new(start_time, end_time, text.rstrip) unless check_syntax
          break
        end
        line = subs.gets
        unless line
          cues << Cue.new(start_time, end_time, text.rstrip) unless check_syntax
          break
        end
        actual_lines += 1
        is_eof, actual_lines, strip_line, text = read_until_index subs, actual_lines, line, true, text
        cues << Cue.new(start_time, end_time, text.strip) unless check_syntax
        break if is_eof
      elsif check_syntax
        error_log += "Syntax error at line #{actual_lines}.\n"
        line = subs.gets
        break unless line
        actual_lines += 1
        is_eof, actual_lines, strip_line = read_until_index subs, actual_lines, line, false
        break if is_eof
      else
        line = subs.gets
        break unless line
        actual_lines += 1
        is_eof, actual_lines, strip_line = read_until_index subs, actual_lines, line, false
        break if is_eof
      end
    end
  end
  if check_syntax
    error_log == '' ? 'No errors were found.' : error_log.rstrip
  else
    [cues, @fps]
  end
end

#read_until_index(subs, actual_lines, line, append, text = nil) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/subtitle-library/reader.rb', line 172

def read_until_index(subs, actual_lines, line, append, text = nil)
  strip_line = line.strip
  while not /\A\d+$/ =~ strip_line
    text += "\n" + strip_line if append
    line = subs.gets
    unless line
      is_eof = true
      break
    end
    actual_lines += 1
    strip_line = line.strip
  end
  [is_eof, actual_lines, strip_line] + (append ? [text] : [])
end

#read_until_timing(subs, actual_lines) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/subtitle-library/reader.rb', line 144

def read_until_timing(subs, actual_lines)
  line = subs.gets
  return true unless line
  actual_lines += 1
  strip_line = line.strip
  while strip_line == '' or /\A\d+$/ =~ strip_line
    line = subs.gets
    unless line
      is_eof = true
      break
    end
    actual_lines += 1
    strip_line = line.strip
  end
  [is_eof, actual_lines, strip_line]
end