class Captions::VTT

Constants

ALIGNMENT_VALUES

Alignment Data

AUTO_KEYWORD

Auto Keyword used in Alignment

VTT_HEADER

Header used for all VTT files

VTT_METADATA

VTT file comments/style section

Public Instance Methods

dump(file) click to toggle source

Export CueList to VTT file

# File lib/captions/formats/vtt.rb, line 54
def dump(file)
  base_dump(file) do |file|
    file.write(VTT_HEADER)
    @cue_list.each do |cue|
      file.write("\n\n")
      file.write(msec_to_timecode(cue.start_time))
      file.write(" --> ")
      file.write(msec_to_timecode(cue.end_time))
      file.write("\n")
      file.write(cue.text)
    end
  end
end
get_alignment(value) click to toggle source
# File lib/captions/formats/vtt.rb, line 102
def get_alignment(value)
  raise InvalidSubtitle, "Invalid VTT Alignment Property" unless ALIGNMENT_VALUES[value]
  return ALIGNMENT_VALUES[value]
end
get_line(value) click to toggle source
# File lib/captions/formats/vtt.rb, line 107
def get_line(value)
  raise InvalidSubtitle, "VTT Line property should be a valid number" if !is_integer?(value) and value != AUTO_KEYWORD
  return value.to_i
end
get_position(value) click to toggle source
# File lib/captions/formats/vtt.rb, line 112
def get_position(value)
  raise InvalidSubtitle, "VTT Position should be a valid number" if !is_integer?(value)
  raise InvalidSubtitle, "VTT Position should be a number between 0 to 100" if (value.to_i < 0) or (value.to_i > 100)
  return value.to_i
end
is_integer?(val) click to toggle source
# File lib/captions/formats/vtt.rb, line 118
def is_integer?(val)
  val.to_i.to_s == val
end
is_meta_data?(text) click to toggle source

Check whether its a meta-data or not

# File lib/captions/formats/vtt.rb, line 74
def is_meta_data?(text)
  !!text.match(VTT_METADATA)
end
is_text?(text) click to toggle source

Check whether if its subtilte text or not

# File lib/captions/formats/vtt.rb, line 84
def is_text?(text)
  !text.empty? and text.is_a?(String) and text != VTT_HEADER
end
is_time?(text) click to toggle source

Timecode format used in VTT file

# File lib/captions/formats/vtt.rb, line 79
def is_time?(text)
  !!text.match(/^(\d{2}:)?\d{2}:\d{2}.\d{3}.*(\d{2}:)?\d{2}:\d{2}.\d{3}/)
end
parse() click to toggle source

Parse VTT file and update CueList

# File lib/captions/formats/vtt.rb, line 24
def parse
  base_parser do
    count = 1
    cue_count = 0
    meta_data_section = false
    cue = nil
    raise InvalidSubtitle, "Invalid VTT Signature" unless validate_header(@file.gets)
    while(line = @file.gets) do
      line = line.strip
      if line.empty?
        meta_data_section = false
      elsif is_meta_data?(line)
        meta_data_section = true
      elsif is_time?(line)
        @cue_list.append(cue) if cue
        cue_count += 1
        cue = Cue.new
        cue.number = cue_count
        line = line.split
        cue.set_time(line[0], line[2])
        set_properties(cue, line[3..-1])
      elsif !meta_data_section and is_text?(line)
        cue.add_text(line)
      end
    end
    @cue_list.append(cue) if cue
  end
end
set_properties(cue, properties) click to toggle source
# File lib/captions/formats/vtt.rb, line 88
def set_properties(cue, properties)
  properties.each do |prop|
    prop, value = prop.split(":")
    value.gsub!("%","")
    case prop
    when "align"
      cue.alignment = get_alignment(value)
    when "line"
      value = value.split(",")[0]
      cue.position = get_line(value)
    end
  end
end
validate_header(line) click to toggle source

Check whether its a VTT_HEADER or not

# File lib/captions/formats/vtt.rb, line 69
def validate_header(line)
  !!line.strip.match(/^#{VTT_HEADER}/)
end