class Captive::VTT
Constants
Public Class Methods
integer?(val)
click to toggle source
# File lib/captive/formats/vtt.rb, line 101 def self.integer?(val) val.to_i.to_s == val end
metadata?(text)
click to toggle source
VTT
Metadata tag matcher
# File lib/captive/formats/vtt.rb, line 92 def self.metadata?(text) !!text.match(VTT_METADATA) end
parse(blob:)
click to toggle source
Parse VTT
blob and return array of cues
# File lib/captive/formats/vtt.rb, line 14 def self.parse(blob:) cue_list = [] lines = blob.split("\n") state = :new_cue cue = nil raise InvalidSubtitle, 'Invalid VTT Signature' unless validate_header(lines.shift) lines.each_with_index do |line, index| line.strip! case state when :new_cue next if line.empty? if metadata?(line) state = :metadata next end # If its not metadata, and its not an empty line, it should be a timestamp or an identifier unless time?(line) # If this line is an identifier the next line should be a timecode next if time?(lines[index + 1]) raise InvalidSubtitle, "Invalid Time Format at line #{index + 1}" unless time?(line) end elements = line.split start_time = elements[0] end_time = elements[2] cue = Cue.new(start_time: start_time, end_time: end_time) state = :text when :text if line.empty? ## end of previous cue cue_list << cue cue = nil state = :new_cue else cue.add_text(line) end when :metadata next unless line.empty? # Line is empty which means metadata block is over state = :new_cue end end # Check to make sure we add the last cue if for some reason the file lacks an empty line at the end cue_list << cue unless cue.nil? # Return the cue_list cue_list end
time?(text)
click to toggle source
VTT
Timecode matcher
# File lib/captive/formats/vtt.rb, line 97 def self.time?(text) !!text.match(/^(\d{2}:)?\d{2}:\d{2}.\d{3}.*(\d{2}:)?\d{2}:\d{2}.\d{3}/) end
validate_header(line)
click to toggle source
VTT
Header tag matcher
# File lib/captive/formats/vtt.rb, line 86 def self.validate_header(line) # Make sure BOM does not interfere with header detection !!line.force_encoding('UTF-8').delete("\xEF\xBB\xBF").strip.match(/^#{VTT_HEADER}/) end
Public Instance Methods
to_s()
click to toggle source
Dump contents to String
# File lib/captive/formats/vtt.rb, line 71 def to_s string = VTT_HEADER.dup string << "\n\n" cues.each do |cue| string << milliseconds_to_timecode(cue.start_time) string << ' --> ' string << milliseconds_to_timecode(cue.end_time) string << "\n" string << cue.text string << "\n\n" end string end