@@ -123,7 +123,7 @@ public extension Subtitles.Coder.VTT {
123123 . removingBOM ( )
124124 . lines
125125 . enumerated ( )
126- . map { ( offset: $0. offset, element: $0. element. trimmingCharacters ( in : . whitespaces ) ) }
126+ . map { ( offset: $0. offset, element: $0. element) }
127127
128128 guard lines [ 0 ] . element. contains ( " WEBVTT " ) else {
129129 throw SubTitlesError . invalidFile
@@ -135,7 +135,7 @@ public extension Subtitles.Coder.VTT {
135135 var inSection = false
136136 var currentLines = [ ( index: Int, line: String) ] ( )
137137 lines. forEach { item in
138- let line = item. element. trimmingCharacters ( in : . whitespaces )
138+ let line = item. element
139139 if line. isEmpty {
140140 if inSection == true {
141141 // End of section
@@ -229,10 +229,20 @@ public extension Subtitles.Coder.VTT {
229229 // Assume its a cue identifier
230230 identifier = l1. line
231231
232+ // Next line should be the times
232233 index += 1
233- let l2 = section [ index]
234- times = try parseTime ( index: l2. index, timeLine: l2. line)
235- index += 1
234+
235+ if index < section. count {
236+ let l2 = section [ index]
237+ times = try parseTime ( index: l2. index, timeLine: l2. line)
238+ index += 1
239+ }
240+ }
241+
242+ // Check if we've found a time value. If not, then skip it
243+ guard let times = times else {
244+ // WebVTT format error? Cannot find time definition
245+ continue
236246 }
237247
238248 // next is the text
@@ -264,8 +274,8 @@ public extension Subtitles.Coder.VTT {
264274
265275 let entry = Subtitles . Cue (
266276 identifier: identifier,
267- startTime: times! . 0 ,
268- endTime: times! . 1 ,
277+ startTime: times. 0 ,
278+ endTime: times. 1 ,
269279 text: text,
270280 speaker: speaker
271281 )
0 commit comments