Module: WebVTT

Defined in:
lib/parser.rb,
lib/segmenter.rb

Defined Under Namespace

Classes: Cue, File, InputError, MalformedFile, Segmenter

Class Method Summary collapse

Class Method Details

.convert_from_srt(srt_file, output = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/parser.rb', line 10

def self.convert_from_srt(srt_file, output=nil)
  if !::File.exists?(srt_file)
    raise InputError, "SRT file not found"
  end

  srt = ::File.read(srt_file)
  output ||= srt_file.gsub(".srt", ".webvtt")

  # convert timestamps and save the file
  srt.gsub!(/([0-9]{2}:[0-9]{2}:[0-9]{2})([,])([0-9]{3})/, '\1.\3')
  # normalize new line character
  srt.gsub!("\r\n", "\n")

  srt = "WEBVTT\n\n#{srt}"
  ::File.open(output, "w") {|f| f.write(srt)}

  return File.new(output)
end

.read(file) ⇒ Object



6
7
8
# File 'lib/parser.rb', line 6

def self.read(file)
  File.new(file)
end

.segment(input, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/segmenter.rb', line 5

def self.segment(input, options={})
  if input.is_a?(String)
    input = File.new(input)
  end

  if ! input.respond_to?(:to_webvtt)
    raise InputError, "Input must be a WebVTT instance or a path"
  end

  segmenter = Segmenter.new(input, options)
  subs = segmenter.split_to_files
  playlist = segmenter.generate_playlist(subs)

  return [playlist, subs]
end