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

fix(WebVTT): Add support to voice tag styles #4845

Merged
merged 5 commits into from
Jan 19, 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
116 changes: 105 additions & 11 deletions lib/text/vtt_text_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ shaka.text.VttTextParser = class {
for (const [key, value] of Object.entries(textColor)) {
const cue = new shaka.text.Cue(0, 0, '');
cue.color = value;
styles.set(key, cue);
styles.set('.' + key, cue);
}

const bgColor = shaka.text.Cue.defaultTextBackgroundColor;
for (const [key, value] of Object.entries(bgColor)) {
const cue = new shaka.text.Cue(0, 0, '');
cue.backgroundColor = value;
styles.set(key, cue);
styles.set('.' + key, cue);
}
}

Expand Down Expand Up @@ -414,6 +414,7 @@ shaka.text.VttTextParser = class {
}
payload = VttTextParser.replaceColorPayload_(payload);
payload = VttTextParser.replaceKaraokeStylePayload_(payload);
payload = VttTextParser.replaceVoiceStylePayload_(payload);
const xmlPayload = '<span>' + payload + '</span>';
const element = shaka.util.XmlUtils.parseXmlString(xmlPayload, 'span');
if (element) {
Expand All @@ -439,6 +440,79 @@ shaka.text.VttTextParser = class {
}
}

/**
* Converts voice style tag to be valid for xml parsing
* For example,
* input: <v Shaka>Test
* output: <v.voice-Shaka>Test</v.voice-Shaka>
*
* @param {string} payload
* @return {string} processed payload
* @private
*/
static replaceVoiceStylePayload_(payload) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if this method is correct?
I tried running it on this simple content:

'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000\n' +
'<v A>Test\n\n' +
'00:00:40.000 --> 00:00:50.000\n' +
'<v B>Test'

And I got this output (broken into multiple lines for readability):

'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000\n' +
'<v.voice-A>Test\n\n' +
'00:00:40.000 --> 00:00:50.000\n' +
'<v.voice-B>Test\n' +
'</v.voice-A></v.voice-B>'

Note that both of the end tags are at the end of the file. Also, the end tags are in the wrong order; this ends up being like <A><B></A></B>, which is incorrectly nested.

Incidentally, if one of the tags already has an end tag, the other isn't given one:

'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000\n' +
'<v A>Test</v>\n\n' +
'00:00:40.000 --> 00:00:50.000\n' +
'<v B>Test'

Results in:

'WEBVTT\n\n' +
'00:00:20.000 --> 00:00:40.000\n' +
'<v.voice-A>Test</v.voice-A>\n\n' +
'00:00:40.000 --> 00:00:50.000\n' +
'<v.voice-B>Test\n'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are wrong! The payload parameter is <v A>Test</v> or <v B>Test. I retested with this input and it works well!

Copy link
Contributor

@theodab theodab Jan 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, I had misread the method. I was mistakenly thinking it was parseCueStyles as in parse the styles of all cues, not parse all of the styles of one cue.
I guess if the expectation is that there will be at most one voice tag per invocation of this method, this should be fine.

const voiceTag = 'v';
const names = [];
let nameStart = -1;
let newPayload = '';
let hasVoiceEndTag = false;
for (let i = 0; i < payload.length; i++) {
// This condition is used to manage tags that have end tags.
if (payload[i] === '/') {
theodab marked this conversation as resolved.
Show resolved Hide resolved
const end = payload.indexOf('>', i);
if (end === -1) {
return payload;
}
const tagEnd = payload.substring(i + 1, end);
if (!tagEnd || tagEnd != voiceTag) {
newPayload += payload[i];
continue;
}
hasVoiceEndTag = true;
let tagStart = null;
if (names.length) {
tagStart = names[names.length -1];
}
if (!tagStart) {
newPayload += payload[i];
} else if (tagStart === tagEnd) {
newPayload += '/' + tagEnd + '>';
i += tagEnd.length + 1;
} else {
if (!tagStart.startsWith(voiceTag)) {
newPayload += payload[i];
continue;
}
newPayload += '/' + tagStart + '>';
i += tagEnd.length + 1;
}
} else {
// Here we only want the tag name, not any other payload.
if (payload[i] === '<') {
nameStart = i + 1;
if (payload[nameStart] != voiceTag) {
nameStart = -1;
}
} else if (payload[i] === '>') {
if (nameStart > 0) {
names.push(payload.substr(nameStart, i - nameStart));
nameStart = -1;
}
}
newPayload += payload[i];
}
}
for (const name of names) {
const newName = name.replace(' ', '.voice-');
newPayload = newPayload.replace(`<${name}>`, `<${newName}>`);
newPayload = newPayload.replace(`</${name}>`, `</${newName}>`);
if (!hasVoiceEndTag) {
newPayload += `</${newName}>`;
}
}
return newPayload;
}

/**
* Converts karaoke style tag to be valid for xml parsing
* For example,
Expand Down Expand Up @@ -495,28 +569,36 @@ shaka.text.VttTextParser = class {
let nameStart = -1;
let newPayload = '';
for (let i = 0; i < payload.length; i++) {
if (payload[i] === '/') {
if (payload[i] === '/' && i > 0 && payload[i - 1] === '<') {
const end = payload.indexOf('>', i);
if (end <= i) {
return payload;
}
const tagEnd = payload.substring(i + 1, end);
if (!tagEnd || tagEnd !== 'c') {
newPayload += payload[i];
continue;
}
const tagStart = names.pop();
if (!tagEnd || !tagStart) {
return payload;
if (!tagStart) {
newPayload += payload[i];
} else if (tagStart === tagEnd) {
newPayload += '/' + tagEnd + '>';
i += tagEnd.length + 1;
} else {
if (!tagStart.startsWith('c.') || tagEnd !== 'c') {
return payload;
if (!tagStart.startsWith('c.')) {
newPayload += payload[i];
continue;
}
newPayload += '/' + tagStart + '>';
i += tagEnd.length + 1;
newPayload += '/' + tagStart + '>';
}
} else {
if (payload[i] === '<') {
nameStart = i + 1;
if (payload[nameStart] != 'c') {
nameStart = -1;
}
} else if (payload[i] === '>') {
if (nameStart > 0) {
names.push(payload.substr(nameStart, i - nameStart));
Expand Down Expand Up @@ -585,12 +667,24 @@ shaka.text.VttTextParser = class {
const bold = shaka.text.Cue.fontWeight.BOLD;
const italic = shaka.text.Cue.fontStyle.ITALIC;
const underline = shaka.text.Cue.textDecoration.UNDERLINE;
const tags = element.nodeName.split(/[ .]+/);
const tags = element.nodeName.split(/(?=[ .])+/g);
for (const tag of tags) {
if (styles.has(tag)) {
VttTextParser.mergeStyle_(nestedCue, styles.get(tag));
let styleTag = tag;
// White blanks at start indicate that the style is a voice
if (styleTag.startsWith('.voice-')) {
const voice = styleTag.split('-').pop();
styleTag = `v[voice="${voice}"]`;
}
if (styles.has(styleTag)) {
VttTextParser.mergeStyle_(nestedCue, styles.get(styleTag));
}
switch (tag) {
case 'br': {
const lineBreakCue = rootCue.clone();
lineBreakCue.lineBreak = true;
cues.push(lineBreakCue);
break;
}
case 'b':
nestedCue.fontWeight = bold;
break;
Expand Down
100 changes: 94 additions & 6 deletions test/text/vtt_text_parser_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -997,25 +997,32 @@ describe('VttTextParser', () => {
{
startTime: 80,
endTime: 90,
payload: '<b><c.lime>Parse fail 1</b></c>',
payload: '<b><c.lime>Parse fail 1</b></c.lime>',
nestedCues: [],
},
{
startTime: 90,
endTime: 100,
payload: '<c.lime><b>Parse fail 2</c></b>',
payload: '<c.lime><b>Parse fail 2</c.lime></b>',
nestedCues: [],
},
{
startTime: 100,
endTime: 110,
payload: '<c.lime>forward slash 1/2 in text</c>',
nestedCues: [],
payload: '',
nestedCues: [
{
startTime: 100,
endTime: 110,
payload: 'forward slash 1/2 in text',
color: '#0F0',
},
],
},
{
startTime: 110,
endTime: 120,
payload: '<c.lime>less or more < > in text</c>',
payload: '<c.lime>less or more < > in text</c.lime>',
nestedCues: [],
},
],
Expand Down Expand Up @@ -1067,6 +1074,51 @@ describe('VttTextParser', () => {
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0});
});

it('supports voice style blocks', () => {
verifyHelper(
[
{
startTime: 20,
endTime: 40,
payload: '',
nestedCues: [
{
startTime: 20,
endTime: 40,
payload: 'Test',
color: 'cyan',
},
],
},
{
startTime: 40,
endTime: 50,
payload: '',
nestedCues: [
{
startTime: 40,
endTime: 50,
payload: 'Test',
color: 'cyan',
},
{
startTime: 40,
endTime: 50,
payload: '2',
fontStyle: Cue.fontStyle.ITALIC,
},
],
},
],
'WEBVTT\n\n' +
'STYLE\n::cue(v[voice="Shaka"]) { color: cyan; }\n\n' +
'00:00:20.000 --> 00:00:40.000\n' +
'<v Shaka>Test\n\n' +
'00:00:40.000 --> 00:00:50.000\n' +
'<v Shaka>Test</v><i>2</i>',
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0});
});

it('supports default color overriding', () => {
verifyHelper(
[
Expand All @@ -1087,12 +1139,48 @@ describe('VttTextParser', () => {
],
'WEBVTT\n\n' +
'STYLE\n' +
'::cue(bg_blue) { font-size: 10px; background-color: #FF0 }\n\n' +
'::cue(.bg_blue) { font-size: 10px; background-color: #FF0 }\n\n' +
'00:00:10.000 --> 00:00:20.000\n' +
'<c.red.bg_blue>Example 1</c>\n\n',
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0});
});

// https://github.com/shaka-project/shaka-player/issues/4479
it('keep styles when there are line breaks', () => {
verifyHelper(
[
{
startTime: 10, endTime: 20,
payload: '',
nestedCues: [
{
startTime: 10,
endTime: 20,
payload: '1',
color: '#F0F',
},
{
startTime: 10,
endTime: 20,
payload: '',
lineBreak: true,
},
{
startTime: 10,
endTime: 20,
payload: '2',
color: '#F0F',
fontStyle: Cue.fontStyle.ITALIC,
},
],
},
],
'WEBVTT\n\n' +
'00:00:10.000 --> 00:00:20.000\n' +
'<c.magenta>1</c><br/><c.magenta><i>2</i></c>\n\n',
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0});
});

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