Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
20 changes: 1 addition & 19 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -4178,25 +4178,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
if (mimeType === 'application/x-subtitle-lrc') {
const LrcTextParser = shaka.text.LrcTextParser;
if (LrcTextParser) {
const cues = LrcTextParser.getCues(data);
let webvttString = 'WEBVTT\n\n';
for (const cue of cues) {
const webvttTimeString = (time) => {
const hours = Math.floor(time / 3600);
const minutes = Math.floor(time / 60 % 60);
const seconds = Math.floor(time % 60);
const milliseconds = Math.floor(time * 1000 % 1000);
return (hours < 10 ? '0' : '') + hours + ':' +
(minutes < 10 ? '0' : '') + minutes + ':' +
(seconds < 10 ? '0' : '') + seconds + '.' +
(milliseconds < 100 ? (milliseconds < 10 ? '00' : '0') : '') +
milliseconds;
};
webvttString += webvttTimeString(cue.startTime) + ' --> ' +
webvttTimeString(cue.endTime) + '\n';
webvttString += cue.payload + '\n\n';
}
return webvttString;
return LrcTextParser.convertToWebVTT(data, this.video_.duration);
} else {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
Expand Down
69 changes: 51 additions & 18 deletions lib/text/lrc_text_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ goog.require('shaka.util.StringUtils');


/**
* LRC file format: https://en.wikipedia.org/wiki/LRC_(file_format)
*
* @implements {shaka.extern.TextParser}
* @export
*/
Expand All @@ -31,15 +33,16 @@ shaka.text.LrcTextParser = class {
* @export
*/
parseMedia(data, time) {
return shaka.text.LrcTextParser.getCues(data);
return shaka.text.LrcTextParser.getCues_(data, time.segmentEnd);
}

/**
* @param {BufferSource} data
* @param {number} segmentEnd
* @return {!Array.<!shaka.extern.Cue>}
* @export
* @private
*/
static getCues(data) {
static getCues_(data, segmentEnd) {
const StringUtils = shaka.util.StringUtils;
const LrcTextParser = shaka.text.LrcTextParser;

Expand All @@ -51,19 +54,20 @@ shaka.text.LrcTextParser = class {

/** @type {!Array.<!shaka.extern.Cue>} */
const cues = [];
const parts = str.split(/\r?\n/);
for (const part of parts) {
if (!part || /^\s+$/.test(part)) {
const lines = str.split(/\r?\n/);
for (const line of lines) {
if (!line || /^\s+$/.test(line)) {
continue;
}

// LRC content
const match = LrcTextParser.lyricLine_.exec(part);
const match = LrcTextParser.lyricLine_.exec(line);
if (match) {
const startTime = LrcTextParser.parseTime_(match[1]);
// This time can be overwritten by a subsequent cue.
// By default we add 2 seconds of duration.
const endTime = startTime + 2;
const payload = match[3];
const endTime = segmentEnd ? segmentEnd : startTime + 2;
const payload = match[2];
const cue = new shaka.text.Cue(startTime, endTime, payload);

// Update previous
Expand All @@ -74,8 +78,7 @@ shaka.text.LrcTextParser = class {
prevCue = cue;
continue;
}
shaka.log.warning('LrcTextParser parser encountered an unknown part.',
part);
shaka.log.warning('LrcTextParser encountered an unknown line.', line);
}
if (prevCue) {
cues.push(prevCue);
Expand All @@ -95,27 +98,57 @@ shaka.text.LrcTextParser = class {
const LrcTextParser = shaka.text.LrcTextParser;
const match = LrcTextParser.timeFormat_.exec(string);
const minutes = parseInt(match[1], 10);
const seconds = parseInt(match[2], 10);
const hundredthsOfSeconds = match[4] ? parseInt(match[4], 10) : 0;
return minutes * 60 + seconds + hundredthsOfSeconds / 100;
const seconds = parseFloat(match[2]);
return minutes * 60 + seconds;
}

/**
* Convert a LRC input to WebVTT
*
* @param {BufferSource} data
* @param {number} segmentEnd
* @return {string}
* @export
*/
static convertToWebVTT(data, segmentEnd) {
const LrcTextParser = shaka.text.LrcTextParser;
const cues = LrcTextParser.getCues_(data, segmentEnd);
let webvttString = 'WEBVTT\n\n';
for (const cue of cues) {
const webvttTimeString = (time) => {
const hours = Math.floor(time / 3600);
const minutes = Math.floor(time / 60 % 60);
const seconds = Math.floor(time % 60);
const milliseconds = Math.floor(time * 1000 % 1000);
return (hours < 10 ? '0' : '') + hours + ':' +
(minutes < 10 ? '0' : '') + minutes + ':' +
(seconds < 10 ? '0' : '') + seconds + '.' +
(milliseconds < 100 ? (milliseconds < 10 ? '00' : '0') : '') +
milliseconds;
};
webvttString += webvttTimeString(cue.startTime) + ' --> ' +
webvttTimeString(cue.endTime) + '\n';
webvttString += cue.payload + '\n\n';
}
return webvttString;
}
};

/**
* @const
* @private {!RegExp}
* @example 50t or 50.5t
* @example [mm:ss.xx]Text
*/
shaka.text.LrcTextParser.lyricLine_ =
/^\[(\d{1,2}:\d{1,2}([.,]\d{1,3})?)\](.*)(\r?\n)*$/;
/^\[(\d{1,2}:\d{1,2}(?:[.,]\d{1,3})?)\](.*)/;

/**
* @const
* @private {!RegExp}
* @example 50t or 50.5t
* @example 00:12.00 or 00:12.0 or 00:12.000
*/
shaka.text.LrcTextParser.timeFormat_ =
/^\s*(\d+):(\d{1,2})([.,](\d{1,3}))?\s*$/;
/^(\d+):(\d{1,2}(?:[.,]\d{1,3})?)$/;

shaka.text.TextEngine.registerParser(
'application/x-subtitle-lrc', () => new shaka.text.LrcTextParser());
13 changes: 13 additions & 0 deletions test/text/lrc_text_parser_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ describe('LrcTextParser', () => {
{periodStart: 0, segmentStart: 0, segmentEnd: 0});
});

it('supports different time formats', () => {
verifyHelper(
[
{startTime: 0.1, endTime: 10.001, payload: 'Test'},
{startTime: 10.001, endTime: 20.02, payload: 'Test2'},
{startTime: 20.02, endTime: 22.02, payload: 'Test3'},
],
'[00:00.10]Test\n' +
'[00:10.001]Test2\n' +
'[00:20.02]Test3',
{periodStart: 0, segmentStart: 0, segmentEnd: 0});
});

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