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

Add SubStation Alpha (SSA) support #3060

Merged
merged 20 commits into from
Feb 1, 2021
Merged
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ Shaka Player supports:
- UTF-8 encoding only
- LyRiCs (LRC)
- UTF-8 encoding only
- SubStation Alpha (SSA, ASS)
- UTF-8 encoding only

Subtitles are rendered by the browser by default. Applications can create a
[text display plugin][] for customer rendering to go beyond browser-supported
Expand Down
2 changes: 2 additions & 0 deletions build/types/text
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
+../../lib/text/mp4_ttml_parser.js
+../../lib/text/mp4_vtt_parser.js
+../../lib/text/srt_text_parser.js
+../../lib/text/ssa_text_parser.js
+../../lib/text/ttml_text_parser.js
+../../lib/text/vtt_text_parser.js
+../../lib/text/web_vtt_generator.js
24 changes: 23 additions & 1 deletion lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ goog.require('shaka.net.NetworkingEngine');
goog.require('shaka.routing.Walker');
goog.require('shaka.text.SimpleTextDisplayer');
goog.require('shaka.text.UITextDisplayer');
goog.require('shaka.text.WebVttGenerator');
goog.require('shaka.util.AbortableOperation');
goog.require('shaka.util.ConfigUtils');
goog.require('shaka.util.Error');
Expand Down Expand Up @@ -3900,6 +3901,8 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
'webvtt': 'text/vtt',
'ttml': 'application/ttml+xml',
'lrc': 'application/x-subtitle-lrc',
'ssa': 'text/x-ssa',
'ass': 'text/x-ssa',
}[extension];

if (!mimeType) {
Expand Down Expand Up @@ -4034,6 +4037,8 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
'webvtt': 'text/vtt',
'ttml': 'application/ttml+xml',
'lrc': 'application/x-subtitle-lrc',
'ssa': 'text/x-ssa',
'ass': 'text/x-ssa',
}[extension];

if (!mimeType) {
Expand Down Expand Up @@ -4199,10 +4204,12 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
mimeType);
}
}
/** @type {Array.<!shaka.extern.Cue>} */
let cues;
if (mimeType === 'application/x-subtitle-lrc') {
const LrcTextParser = shaka.text.LrcTextParser;
if (LrcTextParser) {
return LrcTextParser.convertToWebVTT(data, this.video_.duration);
cues = LrcTextParser.getCues(data, this.video_.duration);
} else {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
Expand All @@ -4211,6 +4218,21 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
mimeType);
}
}
if (mimeType === 'text/x-ssa') {
avelad marked this conversation as resolved.
Show resolved Hide resolved
const SsaTextParser = shaka.text.SsaTextParser;
if (SsaTextParser) {
cues = SsaTextParser.getCues(data);
} else {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.TEXT,
shaka.util.Error.Code.MISSING_TEXT_PLUGIN,
mimeType);
}
}
if (cues) {
return shaka.text.WebVttGenerator.convert(cues);
}
throw new shaka.util.Error(
shaka.util.Error.Severity.RECOVERABLE,
shaka.util.Error.Category.TEXT,
Expand Down
37 changes: 3 additions & 34 deletions lib/text/lrc_text_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ shaka.text.LrcTextParser = class {
* @export
*/
parseMedia(data, time) {
return shaka.text.LrcTextParser.getCues_(data, time.segmentEnd);
return shaka.text.LrcTextParser.getCues(data, time.segmentEnd);
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this just forwards, I suggest just using parseMedia directly.

Copy link
Member Author

Choose a reason for hiding this comment

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

We cannot do this because getCues is static and is necessary in the player.

Copy link
Contributor

Choose a reason for hiding this comment

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

Part of my generic suggestion was to avoid checking if (mimeType == 'foo') and instead use the parser interface directly. Doing something like this:

const factory = TextEngine.findParser(mimeType);
if (factory) {
    const obj = factory();
    cues = obj.parseMedia(data);
}

But you don't have to do that now. You could do it in another PR or I could do it instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm going to address it in another PR, I keep it on my radar. Thanks!

}

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

Expand Down Expand Up @@ -101,37 +101,6 @@ shaka.text.LrcTextParser = class {
const seconds = parseFloat(match[2].replace(',', '.'));
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;
}
};

/**
Expand Down
Loading