Skip to content

Commit

Permalink
feat: Allow VTT files with erroneous linebreaks (#2394)
Browse files Browse the repository at this point in the history
Bad linebreaks will now cause cues to be skipped (with a warning),
rather than throwing an error.

Closes #2358
Co-authored-by: Álvaro Velad Galván <[email protected]>
  • Loading branch information
bloomtom authored Feb 9, 2023
1 parent dc9bd27 commit 9b1c614
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 31 deletions.
8 changes: 3 additions & 5 deletions lib/text/vtt_text_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,9 @@ shaka.text.VttTextParser = class {
let end = VttTextParser.parseTime_(parser);

if (start == null || expect == null || end == null) {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.TEXT,
shaka.util.Error.Code.INVALID_TEXT_CUE,
'Could not parse cue time range in WebVTT');
shaka.log.alwaysWarn(
'Failed to parse VTT time code. Cue skipped:', id, text);
return null;
}

start += timeOffset;
Expand Down
2 changes: 1 addition & 1 deletion lib/util/text_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ shaka.util.TextParser = class {
* @const
* @private {string}
*/
this.data_ = data;
this.data_ = data || '';

/** @private {number} */
this.position_ = 0;
Expand Down
69 changes: 44 additions & 25 deletions test/text/vtt_text_parser_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ describe('VttTextParser', () => {
const Cue = shaka.text.Cue;
const CueRegion = shaka.text.CueRegion;
const originalLogWarning = shaka.log.warning;
const anyString = jasmine.any(String);

/** @type {!jasmine.Spy} */
let logWarningSpy;
Expand Down Expand Up @@ -161,38 +160,30 @@ describe('VttTextParser', () => {
});

it('rejects invalid time values', () => {
errorHelper(shaka.util.Error.Code.INVALID_TEXT_CUE,
verifyHelper([],
'WEBVTT\n\n00.020 --> 0:00.040\nTest',
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0},
anyString);
errorHelper(shaka.util.Error.Code.INVALID_TEXT_CUE,
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0});
verifyHelper([],
'WEBVTT\n\n0:00.020 --> 0:00.040\nTest',
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0},
anyString);
errorHelper(shaka.util.Error.Code.INVALID_TEXT_CUE,
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0});
verifyHelper([],
'WEBVTT\n\n00:00.20 --> 0:00.040\nTest',
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0},
anyString);
errorHelper(shaka.util.Error.Code.INVALID_TEXT_CUE,
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0});
verifyHelper([],
'WEBVTT\n\n00:100.20 --> 0:00.040\nTest',
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0},
anyString);
errorHelper(shaka.util.Error.Code.INVALID_TEXT_CUE,
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0});
verifyHelper([],
'WEBVTT\n\n00:00.020 --> 0:00.040\nTest',
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0},
anyString);
errorHelper(shaka.util.Error.Code.INVALID_TEXT_CUE,
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0});
verifyHelper([],
'WEBVTT\n\n00:00:00:00.020 --> 0:00.040\nTest',
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0},
anyString);
errorHelper(shaka.util.Error.Code.INVALID_TEXT_CUE,
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0});
verifyHelper([],
'WEBVTT\n\n00:61.020 --> 0:00.040\nTest',
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0},
anyString);
errorHelper(shaka.util.Error.Code.INVALID_TEXT_CUE,
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0});
verifyHelper([],
'WEBVTT\n\n61:00.020 --> 0:00.040\nTest',
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0},
anyString);
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0});
});

it('supports vertical setting', () => {
Expand Down Expand Up @@ -1223,6 +1214,34 @@ describe('VttTextParser', () => {
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0});
});

it('supports an extra newline inside the cue body', () => {
verifyHelper(
[
{startTime: 20, endTime: 40, payload: 'Test'},
{startTime: 40, endTime: 50, payload: 'Test2'},
],
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000\n' +
'Test\n\nExtra line\n\n' +
'00:00:40.000 --> 00:00:50.000\n' +
'Test2',
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0});
});

it('supports an extra newline before the cue body', () => {
verifyHelper(
[
{startTime: 20, endTime: 40, payload: ''},
{startTime: 40, endTime: 50, payload: 'Test2'},
],
'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000\n' +
'\nTest\n\n' +
'00:00:40.000 --> 00:00:50.000\n' +
'Test2',
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0});
});

/**
* @param {!Array} cues
* @param {string} text
Expand Down

0 comments on commit 9b1c614

Please sign in to comment.