-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add LyRiCs (LRC) support #3036
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
Merged
Merged
Add LyRiCs (LRC) support #3036
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d791c61
Add LyRiCs (LRC) support
fffdb95
Merge branch 'master' into lrc
f78191c
Add link to wikipedia
93bb2f1
Rename parts to lines
d03b2e5
Move some logic from the player to the plugin
d030b17
Fix regex example
f7602ad
Fix time parsing
20b9b07
Add more tests
96bb375
Fix example
07e9425
Update regex
9d53dcf
Fix endTime
03994c1
Add more examples
4141d8f
Add more tests
1242b33
Fix tests
86eb708
Fix test typo
39ceaf4
Fix decimal parse with comma
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /*! @license | ||
| * Shaka Player | ||
| * Copyright 2016 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| goog.provide('shaka.text.LrcTextParser'); | ||
|
|
||
| goog.require('goog.asserts'); | ||
| goog.require('shaka.log'); | ||
| goog.require('shaka.text.Cue'); | ||
| goog.require('shaka.text.TextEngine'); | ||
| goog.require('shaka.util.StringUtils'); | ||
|
|
||
|
|
||
| /** | ||
| * @implements {shaka.extern.TextParser} | ||
avelad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * @export | ||
| */ | ||
| shaka.text.LrcTextParser = class { | ||
| /** | ||
| * @override | ||
| * @export | ||
| */ | ||
| parseInit(data) { | ||
| goog.asserts.assert(false, 'LRC does not have init segments'); | ||
| } | ||
|
|
||
| /** | ||
| * @override | ||
| * @export | ||
| */ | ||
| parseMedia(data, time) { | ||
| return shaka.text.LrcTextParser.getCues(data); | ||
| } | ||
|
|
||
| /** | ||
| * @param {BufferSource} data | ||
| * @return {!Array.<!shaka.extern.Cue>} | ||
| * @export | ||
| */ | ||
| static getCues(data) { | ||
| const StringUtils = shaka.util.StringUtils; | ||
| const LrcTextParser = shaka.text.LrcTextParser; | ||
|
|
||
| // Get the input as a string. | ||
| const str = StringUtils.fromUTF8(data); | ||
|
|
||
| /** @type {shaka.extern.Cue} */ | ||
| let prevCue = null; | ||
|
|
||
| /** @type {!Array.<!shaka.extern.Cue>} */ | ||
| const cues = []; | ||
| const parts = str.split(/\r?\n/); | ||
avelad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for (const part of parts) { | ||
| if (!part || /^\s+$/.test(part)) { | ||
| continue; | ||
| } | ||
|
|
||
| // LRC content | ||
| const match = LrcTextParser.lyricLine_.exec(part); | ||
| if (match) { | ||
| const startTime = LrcTextParser.parseTime_(match[1]); | ||
| // By default we add 2 seconds of duration. | ||
| const endTime = startTime + 2; | ||
avelad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const payload = match[3]; | ||
avelad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const cue = new shaka.text.Cue(startTime, endTime, payload); | ||
|
|
||
| // Update previous | ||
| if (prevCue) { | ||
| prevCue.endTime = startTime; | ||
| cues.push(prevCue); | ||
| } | ||
| prevCue = cue; | ||
| continue; | ||
| } | ||
| shaka.log.warning('LrcTextParser parser encountered an unknown part.', | ||
avelad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| part); | ||
| } | ||
| if (prevCue) { | ||
| cues.push(prevCue); | ||
| } | ||
|
|
||
| return cues; | ||
| } | ||
|
|
||
| /** | ||
| * Parses a LRC time from the given parser. | ||
| * | ||
| * @param {string} string | ||
| * @return {number} | ||
| * @private | ||
| */ | ||
| static parseTime_(string) { | ||
| 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; | ||
avelad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * @const | ||
| * @private {!RegExp} | ||
| * @example 50t or 50.5t | ||
avelad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| shaka.text.LrcTextParser.lyricLine_ = | ||
| /^\[(\d{1,2}:\d{1,2}([.,]\d{1,3})?)\](.*)(\r?\n)*$/; | ||
avelad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * @const | ||
| * @private {!RegExp} | ||
| * @example 50t or 50.5t | ||
avelad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| shaka.text.LrcTextParser.timeFormat_ = | ||
| /^\s*(\d+):(\d{1,2})([.,](\d{1,3}))?\s*$/; | ||
avelad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| shaka.text.TextEngine.registerParser( | ||
| 'application/x-subtitle-lrc', () => new shaka.text.LrcTextParser()); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /*! @license | ||
| * Shaka Player | ||
| * Copyright 2016 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| goog.require('shaka.text.LrcTextParser'); | ||
| goog.require('shaka.util.BufferUtils'); | ||
| goog.require('shaka.util.StringUtils'); | ||
|
|
||
| describe('LrcTextParser', () => { | ||
| it('supports no cues', () => { | ||
| verifyHelper([], | ||
| '', | ||
| {periodStart: 0, segmentStart: 0, segmentEnd: 0}); | ||
| }); | ||
|
|
||
| it('handles a blank line at the start of the file', () => { | ||
| verifyHelper( | ||
| [ | ||
| {startTime: 0, endTime: 2, payload: 'Test'}, | ||
| ], | ||
| '\n\n' + | ||
| '[00:00.00]Test', | ||
| {periodStart: 0, segmentStart: 0, segmentEnd: 0}); | ||
| }); | ||
|
|
||
| it('handles a blank line at the end of the file', () => { | ||
| verifyHelper( | ||
| [ | ||
| {startTime: 0, endTime: 2, payload: 'Test'}, | ||
| ], | ||
| '[00:00.00]Test' + | ||
| '\n\n', | ||
| {periodStart: 0, segmentStart: 0, segmentEnd: 0}); | ||
| }); | ||
|
|
||
| it('handles no blank line at the end of the file', () => { | ||
| verifyHelper( | ||
| [ | ||
| {startTime: 0, endTime: 2, payload: 'Test'}, | ||
| ], | ||
| '[00:00.00]Test', | ||
| {periodStart: 0, segmentStart: 0, segmentEnd: 0, | ||
| }); | ||
| }); | ||
|
|
||
| it('supports multiple cues', () => { | ||
| verifyHelper( | ||
| [ | ||
| {startTime: 0, endTime: 10, payload: 'Test'}, | ||
| {startTime: 10, endTime: 20, payload: 'Test2'}, | ||
| {startTime: 20, endTime: 22, payload: 'Test3'}, | ||
| ], | ||
| '[00:00.00]Test\n' + | ||
| '[00:10.00]Test2\n' + | ||
| '[00:20.00]Test3', | ||
| {periodStart: 0, segmentStart: 0, segmentEnd: 0}); | ||
| }); | ||
|
|
||
avelad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** | ||
| * @param {!Array} cues | ||
| * @param {string} text | ||
| * @param {shaka.extern.TextParser.TimeContext} time | ||
| */ | ||
| function verifyHelper(cues, text, time) { | ||
| const BufferUtils = shaka.util.BufferUtils; | ||
| const StringUtils = shaka.util.StringUtils; | ||
|
|
||
| const data = BufferUtils.toUint8(StringUtils.toUTF8(text)); | ||
|
|
||
| const parser = new shaka.text.LrcTextParser(); | ||
| const result = parser.parseMedia(data, time); | ||
|
|
||
| const expected = cues.map((cue) => { | ||
| if (cue.nestedCues) { | ||
| cue.nestedCues = cue.nestedCues.map( | ||
| (nestedCue) => jasmine.objectContaining(nestedCue) | ||
| ); | ||
| } | ||
| return jasmine.objectContaining(cue); | ||
| }); | ||
| expect(result).toEqual(expected); | ||
| } | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.