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

Karoke-style captions #2217

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ Here's a list of the properties and what they are used for:
| `--plyr-font-weight-bold` | The bold font weight. | `600` |
| `--plyr-line-height` | The line height used within the player. | `1.7` |
| `--plyr-font-smoothing` | Whether to enable font antialiasing within the player. | `false` |
| `--plyr-pending-caption-opacity` | The opacity of WebVTT "future text" for karaoke style captions | `30%` |

You can set them in your CSS for all players:

Expand Down
40 changes: 39 additions & 1 deletion src/js/captions.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ import { parseUrl } from './utils/urls';
const captions = {
// Setup captions
setup() {
window.show = [];
const media = this.media;
media.ontimeupdate = function () {
while (window.show[0] && media.currentTime > window.show[0].time) {
const caption = window.show.shift();
document.getElementById(caption.id).className = '';
}
};

// Requires UI support
if (!this.supported.ui) {
return;
Expand Down Expand Up @@ -383,7 +392,36 @@ const captions = {
const track = captions.getCurrentTrack.call(this);

cues = Array.from((track || {}).activeCues || [])
.map((cue) => cue.getCueAsHTML())
.map((cue) => {
const cueFragment = cue.getCueAsHTML();
const newFragment = document.createDocumentFragment();

const array = Array.prototype.slice.call(cueFragment.childNodes);
let currentSegment = 0;
window.show = [];
for (let child of array) {
if (child.nodeType === 7) {
let id = 'captionSegment' + (currentSegment + 1);
window.show.push({
time: Number(new Date('1970-01-01T' + child.data + 'Z')) / 1000,
id: id,
});
/* setTimeout(() => {
console.log(id)
document.getElementById(id).className = ''
}, Number(new Date('1970-01-01T' + child.data + 'Z')) - (this.media.currentTime * 1000)) */
currentSegment += 1;
continue;
}
if (currentSegment !== 0) {
child.id = 'captionSegment' + currentSegment;
child.className = 'pendingCaption';
}
newFragment.append(child);
}

return newFragment;
})
.map(getHTML);
}

Expand Down
4 changes: 4 additions & 0 deletions src/sass/components/captions.scss
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@
display: inline;
}
}

.plyr__caption span.pendingCaption {
opacity: $plyr-pending-caption-opacity;
}
2 changes: 2 additions & 0 deletions src/sass/settings/captions.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ $plyr-font-size-captions-base: $plyr-font-size-base !default;
$plyr-font-size-captions-small: $plyr-font-size-small !default;
$plyr-font-size-captions-medium: $plyr-font-size-large !default;
$plyr-font-size-captions-large: $plyr-font-size-xlarge !default;

$plyr-pending-caption-opacity: var(--plyr-pending-caption-opacity, 30%) !default;