Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow VTT files with erroneous linebreaks #2394

Merged
merged 6 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 || '';
joeyparrish marked this conversation as resolved.
Show resolved Hide resolved

/** @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([],
joeyparrish marked this conversation as resolved.
Show resolved Hide resolved
'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 @@ -1199,6 +1190,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: ''},
joeyparrish marked this conversation as resolved.
Show resolved Hide resolved
{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