From 6dd80c85d68096a72308391e7d3d0f7ba8833a9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?A=CC=81lvaro=20Velad=20Galva=CC=81n?= Date: Fri, 18 Aug 2023 14:54:53 +0200 Subject: [PATCH 1/2] feat: Support stylized payload in SRT format Info about the format in https://docs.fileformat.com/video/srt/ --- lib/text/srt_text_parser.js | 10 ++++- lib/text/vtt_text_parser.js | 8 ++++ test/text/srt_text_parser_unit.js | 73 +++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/lib/text/srt_text_parser.js b/lib/text/srt_text_parser.js index b2a3d73125..89a70a4444 100644 --- a/lib/text/srt_text_parser.js +++ b/lib/text/srt_text_parser.js @@ -110,7 +110,15 @@ shaka.text.SrtTextParser = class { // convert time codes lines[0] = lines[0].replace(/,/g, '.'); - return lines.join('\n') + '\n\n'; + const webvttCue = lines.join('\n') + .replace(/{b}/g, '') + .replace(/{\/b}/g, '') + .replace(/{i}/g, '') + .replace(/{\/i}/g, '') + .replace(/{u}/g, '') + .replace(/{\/u}/g, ''); + + return webvttCue + '\n\n'; } }; diff --git a/lib/text/vtt_text_parser.js b/lib/text/vtt_text_parser.js index 6271c95ad0..863be3853c 100644 --- a/lib/text/vtt_text_parser.js +++ b/lib/text/vtt_text_parser.js @@ -722,6 +722,14 @@ shaka.text.VttTextParser = class { case 'u': nestedCue.textDecoration.push(underline); break; + case 'font': { + const color = + /** @type {!Element} */(element).getAttribute('color'); + if (color) { + nestedCue.color = color; + } + break; + } case 'div': { const time = /** @type {!Element} */(element).getAttribute('time'); if (!time) { diff --git a/test/text/srt_text_parser_unit.js b/test/text/srt_text_parser_unit.js index fba00e8b5f..8a33edeff5 100644 --- a/test/text/srt_text_parser_unit.js +++ b/test/text/srt_text_parser_unit.js @@ -5,6 +5,8 @@ */ describe('SrtTextParser', () => { + const Cue = shaka.text.Cue; + it('supports no cues', () => { verifyHelper([], '', @@ -59,6 +61,77 @@ describe('SrtTextParser', () => { {periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0}); }); + it('supports payload stylized', () => { + verifyHelper( + [ + { + startTime: 10, + endTime: 20, + payload: '', + nestedCues: [ + { + startTime: 10, + endTime: 20, + payload: 'Test', + fontWeight: Cue.fontWeight.BOLD, + }, + ], + }, + { + startTime: 20, + endTime: 30, + payload: '', + nestedCues: [ + { + startTime: 20, + endTime: 30, + payload: 'Test2', + fontStyle: Cue.fontStyle.ITALIC, + }, + ], + }, + { + startTime: 30, + endTime: 40, + payload: '', + nestedCues: [ + { + startTime: 30, + endTime: 40, + payload: 'Test3', + textDecoration: [Cue.textDecoration.UNDERLINE], + }, + ], + }, + { + startTime: 40, + endTime: 50, + payload: '', + nestedCues: [ + { + startTime: 40, + endTime: 50, + payload: 'Test4', + color: 'red', + }, + ], + }, + ], + '1\n' + + '00:00:10,000 --> 00:00:20,000\n' + + '{b}Test{/b}\n\n' + + '2\n' + + '00:00:20,000 --> 00:00:30,000\n' + + '{i}Test2{/i}\n\n' + + '3\n' + + '00:00:30,000 --> 00:00:40,000\n' + + '{u}Test3{/u}\n\n'+ + '4\n' + + '00:00:40,000 --> 00:00:50,000\n' + + 'Test4', + {periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0}); + }); + /** * @param {!Array} cues * @param {string} text From 64f01baed429c14fee002642bf1be229128f25e9 Mon Sep 17 00:00:00 2001 From: Alvaro Velad Galvan Date: Sat, 19 Aug 2023 10:39:23 +0200 Subject: [PATCH 2/2] Improve tests --- test/text/srt_text_parser_unit.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/text/srt_text_parser_unit.js b/test/text/srt_text_parser_unit.js index 8a33edeff5..beee706cef 100644 --- a/test/text/srt_text_parser_unit.js +++ b/test/text/srt_text_parser_unit.js @@ -75,6 +75,11 @@ describe('SrtTextParser', () => { payload: 'Test', fontWeight: Cue.fontWeight.BOLD, }, + { + startTime: 10, + endTime: 20, + payload: ' Unstyled', + }, ], }, { @@ -119,7 +124,7 @@ describe('SrtTextParser', () => { ], '1\n' + '00:00:10,000 --> 00:00:20,000\n' + - '{b}Test{/b}\n\n' + + '{b}Test{/b} Unstyled\n\n' + '2\n' + '00:00:20,000 --> 00:00:30,000\n' + '{i}Test2{/i}\n\n' +