Class: Subtool::Import::Srt

Inherits:
Object
  • Object
show all
Defined in:
lib/subtool/import/srt.rb

Constant Summary collapse

HOUR =
60 * 60 * 1000
MIN =
60 * 1000
SEC =
1000

Class Method Summary collapse

Class Method Details

.file(filename) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/subtool/import/srt.rb', line 26

def self.file(filename)
  fh = File.open(filename)
  subtitles = self.filehandler(fh)
  fh.close

  subtitles
end

.filehandler(fh) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/subtool/import/srt.rb', line 9

def self.filehandler(fh)
  subtitles = []
  until fh.eof? do

    number = Integer(fh.readline.strip)
    position = self.parse_position(fh.readline.strip)
    body = ""
    while (! fh.eof?) && ("" != (content = fh.readline.strip))
      body = body + content + "\n"
    end

    subtitles.push(Subtool::Subtitle.new(number, position[:start], position[:end], body.strip))
  end

  subtitles
end

.parse_position(position) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/subtool/import/srt.rb', line 34

def self.parse_position(position)
  matches = position.match(/^(\d\d):(\d\d):(\d\d),(\d\d\d) --> (\d\d):(\d\d):(\d\d),(\d\d\d)$/)
  throw Exception.new("Can't parse #{position} as a valid time range") unless matches

  matches = matches[1..8].map { |s| s.to_i }
  {
      :start => self.to_millisecs(matches[0], matches[1], matches[2], matches[3]),
      :end => self.to_millisecs(matches[4], matches[5], matches[6], matches[7]),
  }
end