Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -4178,7 +4178,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
if (mimeType === 'application/x-subtitle-lrc') {
const LrcTextParser = shaka.text.LrcTextParser;
if (LrcTextParser) {
return LrcTextParser.convertToWebVTT(data);
return LrcTextParser.convertToWebVTT(data, this.video_.duration);
} else {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
Expand Down
27 changes: 16 additions & 11 deletions lib/text/lrc_text_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,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>}
* @private
*/
static getCues_(data) {
static getCues_(data, segmentEnd) {
const StringUtils = shaka.util.StringUtils;
const LrcTextParser = shaka.text.LrcTextParser;

Expand All @@ -63,9 +64,10 @@ shaka.text.LrcTextParser = class {
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 Down Expand Up @@ -96,20 +98,21 @@ 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) + parseFloat(match[3]);
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) {
static convertToWebVTT(data, segmentEnd) {
const LrcTextParser = shaka.text.LrcTextParser;
const cues = LrcTextParser.getCues_(data);
const cues = LrcTextParser.getCues_(data, segmentEnd);
let webvttString = 'WEBVTT\n\n';
for (const cue of cues) {
const webvttTimeString = (time) => {
Expand All @@ -134,18 +137,20 @@ shaka.text.LrcTextParser = class {
/**
* @const
* @private {!RegExp}
* @example 50t or 50.5t
* @example [00:12.0]Text or [00:12.00]Text or [00:12.000]Text or
* [00:12,0]Text or [00:12,00]Text or [00:12,000]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 00:12.00 or 00:12.0 or 00:12.000
* @example 00:12.0 or 00:12.00 or 00:12.000 or
* 00:12,0 or 00:12,00 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());
12 changes: 9 additions & 3 deletions test/text/lrc_text_parser_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,17 @@ describe('LrcTextParser', () => {
[
{startTime: 0.1, endTime: 10.001, payload: 'Test'},
{startTime: 10.001, endTime: 20.02, payload: 'Test2'},
{startTime: 20.02, endTime: 22.02, payload: 'Test3'},
{startTime: 20.02, endTime: 30.1, payload: 'Test3'},
{startTime: 30.1, endTime: 40.001, payload: 'Test4'},
{startTime: 40.001, endTime: 50.02, payload: 'Test5'},
{startTime: 50.02, endTime: 52.02, payload: 'Test6'},
],
'[00:00.10]Test\n' +
'[00:00.1]Test\n' +
'[00:10.001]Test2\n' +
'[00:20.02]Test3',
'[00:20.02]Test3\n' +
'[00:30.1]Test4\n' +
'[00:40.001]Test5\n' +
'[00:50.02]Test6',
Copy link
Member

Choose a reason for hiding this comment

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

I think maybe these were meant to use commas as the decimal separator.

Copy link
Member Author

Choose a reason for hiding this comment

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

ups!

{periodStart: 0, segmentStart: 0, segmentEnd: 0});
});

Expand Down