Class: WebVTT::File
- Inherits:
-
Object
- Object
- WebVTT::File
- Defined in:
- lib/parser.rb
Instance Attribute Summary collapse
-
#cues ⇒ Object
Returns the value of attribute cues.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #actual_total_length ⇒ Object
-
#initialize(webvtt_file) ⇒ File
constructor
A new instance of File.
- #parse ⇒ Object
- #save(output = nil) ⇒ Object
- #to_webvtt ⇒ Object
- #total_length ⇒ Object
Constructor Details
#initialize(webvtt_file) ⇒ File
Returns a new instance of File.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/parser.rb', line 33 def initialize(webvtt_file) if !::File.exists?(webvtt_file) raise InputError, "WebVTT file not found" end @path = webvtt_file @filename = ::File.basename(@path) @content = ::File.read(webvtt_file).gsub("\r\n", "\n") # normalizing new line character parse end |
Instance Attribute Details
#cues ⇒ Object
Returns the value of attribute cues.
31 32 33 |
# File 'lib/parser.rb', line 31 def cues @cues end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
30 31 32 |
# File 'lib/parser.rb', line 30 def filename @filename end |
#header ⇒ Object (readonly)
Returns the value of attribute header.
30 31 32 |
# File 'lib/parser.rb', line 30 def header @header end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
30 31 32 |
# File 'lib/parser.rb', line 30 def path @path end |
Instance Method Details
#actual_total_length ⇒ Object
52 53 54 |
# File 'lib/parser.rb', line 52 def actual_total_length @cues.last.end_in_sec - @cues.first.start_in_sec end |
#parse ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/parser.rb', line 63 def parse # remove bom first @content.gsub!("\uFEFF", '') cues = @content.split("\n\n") @header = cues.shift header_lines = @header.split("\n").map(&:strip) if (header_lines[0] =~ /^WEBVTT/).nil? raise MalformedFile, "Not a valid WebVTT file" end @cues = [] cues.each do |cue| cue_parsed = Cue.new(cue) if !cue_parsed.text.nil? @cues << cue_parsed end end @cues end |
#save(output = nil) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/parser.rb', line 56 def save(output=nil) output ||= @path.gsub(".srt", ".webvtt") File.open(output, "w") {|f| f.write(to_webvtt)} return output end |
#to_webvtt ⇒ Object
44 45 46 |
# File 'lib/parser.rb', line 44 def to_webvtt [@header, @cues.map(&:to_webvtt)].flatten.join("\n\n") end |
#total_length ⇒ Object
48 49 50 |
# File 'lib/parser.rb', line 48 def total_length @cues.last.end_in_sec end |