Class: MicroDVDReader

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) ⇒ MicroDVDReader

Returns a new instance of MicroDVDReader.



192
193
194
195
# File 'lib/subtitle-library/reader.rb', line 192

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

Instance Method Details

#add_new_line(line, cues, last_end_frame, error_log, check_syntax, actual_lines) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/subtitle-library/reader.rb', line 245

def add_new_line(line, cues, last_end_frame, error_log, check_syntax, actual_lines)
  match = /\d+/.match line
  start_frame = match.to_s.to_i
  match = /\d+/.match match.post_match
  end_frame = match.to_s.to_i
  if start_frame <= end_frame and start_frame >= last_end_frame
    unless check_syntax
      text = MICRO_DVD_LINE.match(line).post_match
      cues << Cue.new(start_frame, end_frame, text.gsub('|', "\n"))
    end
    last_end_frame = end_frame
  elsif check_syntax
    error_log += "Syntax error at line #{actual_lines}.\n"
  end
  [last_end_frame, error_log]
end

#find_out_fps(subs) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/subtitle-library/reader.rb', line 223

def find_out_fps(subs)
  line = subs.gets
  actual_lines = 1
  while line
    strip_line = line.strip
    if strip_line != ''
      if MICRO_DVD_LINE =~ strip_line
        first_line = MICRO_DVD_LINE.match(strip_line).post_match.strip
        if /\A\d*\.?\d*$/ =~ first_line
          @fps = first_line.to_f
          line = subs.gets
          actual_lines += 1
        end
      end
      break
    end
    line = subs.gets
    actual_lines += 1
  end
  [line, actual_lines]
end

#read_subs(check_syntax) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/subtitle-library/reader.rb', line 197

def read_subs(check_syntax)
  cues = []
  error_log = ''
  last_end_frame = 0
  File.open(@subs_path, 'r') do |subs|
    line, actual_lines = find_out_fps subs
    while line
      strip_line = line.strip
      if strip_line != ''
        if MICRO_DVD_LINE =~ strip_line
          last_end_frame, error_log = add_new_line strip_line, cues, last_end_frame, error_log, check_syntax, actual_lines
        elsif check_syntax
          error_log += "Syntax error at line #{actual_lines}.\n"
        end
      end
      line = subs.gets
      actual_lines += 1
    end
  end
  if check_syntax
    error_log == '' ? 'No errors were found.' : error_log.rstrip
  else
    [cues, @fps]
  end
end