class Subber::Parser::Vtt

Constants

BYTE_ORDER_MARK_STRING
COUNTER_REGEX
CUE_DELIMITER_REGEX
INVALID_CUE_START_STRINGS
SUBTITLE_REGEX
TIMECODE_REGEX
TIME_RANGE_REGEX
WINDOW_LINE_BREAK_REGEX

Public Class Methods

parse(file_content) click to toggle source

@param file_content [String] @return [Array<Subber::Subtitle>]

# File lib/subber/parser/vtt.rb, line 17
def parse(file_content)
  file_content = remove_window_line_break(file_content)
  cues = extract_cues(file_content)

  cues.map.with_index do |cue, index|
    convert_cue_to_subtitle(cue, index)
  end
end

Private Class Methods

convert_cue_to_subtitle(cue, index) click to toggle source

@param cue [String] @param index [Integer] @return [Array<Subber::Subtitle>]

# File lib/subber/parser/vtt.rb, line 42
def convert_cue_to_subtitle(cue, index)
  matches = cue.match(SUBTITLE_REGEX).to_a
  raise(Subber::Errors::InvalidVttFormat, cue) if matches.empty?

  _cue, counter, time_range_string, content = matches

  counter = (index + 1).to_s if counter.empty?

  counter = extract_counter(counter)
  from, to = extract_time_range(time_range_string)

  Subber::Subtitle.new(
    counter: counter,
    start_time: convert_time_to_ms(from),
    end_time: convert_time_to_ms(to),
    content: content.strip
  )
rescue Subber::Errors::InvalidCounter
  raise(Subber::Errors::InvalidCounter, cue)
rescue Subber::Errors::InvalidTimeRange
  raise(Subber::Errors::InvalidTimeRange, cue)
rescue Subber::Errors::InvalidTimestamp
  raise(Subber::Errors::InvalidTimestamp, cue)
end
convert_time_to_ms(time_string) click to toggle source

@param time_string [String] @return [Integer] @raise [Subber::Errors::InvalidTimestamp]

# File lib/subber/parser/vtt.rb, line 103
def convert_time_to_ms(time_string)
  matches = time_string.match(TIMECODE_REGEX).to_a
  raise(Subber::Errors::InvalidTimestamp, time_string) if matches.empty?

  int_matches = matches.map(&:to_i)
  _time_string, hours, minutes, seconds, milliseconds = int_matches

  ((hours * 60 + minutes) * 60 + seconds) * 1000 + milliseconds
end
extract_counter(text) click to toggle source

@param counter_string [String] @return [Integer] @raise [Subber::Errors::InvalidCounter]

# File lib/subber/parser/vtt.rb, line 78
def extract_counter(text)
  raise(Subber::Errors::InvalidCounter) if text.match(COUNTER_REGEX).nil?
  text.sub!(BYTE_ORDER_MARK_STRING, '')
  text.to_i
end
extract_cues(file_content) click to toggle source

@param file_content [String] @return [Array<String>]

# File lib/subber/parser/vtt.rb, line 31
def extract_cues(file_content)
  cues = file_content.split(CUE_DELIMITER_REGEX)
  cues.reject do |cue|
    cue.start_with?(*INVALID_CUE_START_STRINGS)
  end
end
extract_time_range(text) click to toggle source

@param text [String] @return [

[String] from
[String] to

] @raise [Subber::Errors::InvalidCounter]

# File lib/subber/parser/vtt.rb, line 91
def extract_time_range(text)
  matches = text.match(TIME_RANGE_REGEX).to_a
  raise(Subber::Errors::InvalidTimeRange) if matches.empty?
  _text, from, to = matches

  [from, to]
end
remove_window_line_break(file_content) click to toggle source

@param file_content [String] @return [String]

# File lib/subber/parser/vtt.rb, line 70
def remove_window_line_break(file_content)
  file_content.gsub(WINDOW_LINE_BREAK_REGEX, '')
end