Skip to content

Commit

Permalink
Support enabling custom media subtitle by default
Browse files Browse the repository at this point in the history
  • Loading branch information
animeavi authored and calzoneman committed Nov 10, 2020
1 parent 8fc9513 commit 750509e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
5 changes: 4 additions & 1 deletion docs/custom-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ Each text track entry is a JSON object with the following keys:
[`text/vtt`](https://developer.mozilla.org/en-US/docs/Web/API/WebVTT_API).
* `name`: A name for the text track. This is displayed in the menu for the
viewer to select a text track.
* `default`: Enable track by default. Optional boolean attribute to enable
a subtitle track to the user by default.

**Important note regarding text tracks and CORS:**

Expand Down Expand Up @@ -148,7 +150,8 @@ for more information about setting this header.
{
"url": "https://example.com/subtitles.vtt",
"contentType": "text/vtt",
"name": "English Subtitles"
"name": "English Subtitles",
"default": true
}
]
}
Expand Down
8 changes: 6 additions & 2 deletions player/videojs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,16 @@ window.VideoJSPlayer = class VideoJSPlayer extends Player
if data.meta.textTracks
data.meta.textTracks.forEach((track) ->
label = track.name
$('<track/>').attr(
attrs =
src: track.url
kind: 'subtitles'
type: track.type
label: label
).appendTo(video)

if track.default? and track.default
attrs.default = ''

$('<track/>').attr(attrs).appendTo(video)
)

@player = videojs(video[0],
Expand Down
10 changes: 10 additions & 0 deletions src/custom-media.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ function validateTextTracks(textTracks) {
if (!Array.isArray(textTracks))
throw new ValidationError('textTracks must be a list');

let default_count = 0;
for (let track of textTracks) {
if (typeof track.url !== 'string')
throw new ValidationError('text track URL must be a string');
Expand All @@ -223,6 +224,15 @@ function validateTextTracks(textTracks) {
throw new ValidationError('text track name must be a string');
if (!track.name)
throw new ValidationError('text track name must be nonempty');

if (typeof track.default !== 'undefined') {
if (default_count > 0)
throw new ValidationError('only one default text track is allowed');
else if (typeof track.default !== 'boolean' || track.default !== true)
throw new ValidationError('text default attribute must be set to boolean true');
else
default_count++;
}
}
}

Expand Down

0 comments on commit 750509e

Please sign in to comment.