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

feat: kareoke style subtitles #563

Merged
merged 8 commits into from
Feb 26, 2024
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
6 changes: 5 additions & 1 deletion src/assets/styles/components/text-tracks.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
> div {
margin: 3% !important;
}
// Word highlight
&.cld-paced-text-tracks b {
color: var(--color-accent);
}
}
.vjs-text-track-cue {
top: auto !important;
Expand All @@ -27,7 +31,7 @@
.vjs-text-track-display:not(.cld-styled-text-tracks-theme-videojs-default) {
.vjs-text-track-cue {
font-family: inherit !important;
& > div {
> div {
font-weight: 700;
background-color: transparent !important;
text-shadow: 0 0 0.2em rgba(0, 0, 0, 0.8);
Expand Down
65 changes: 50 additions & 15 deletions src/plugins/paced-transcript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ function pacedTranscript(config) {
source.publicId(),
extendCloudinaryConfig(player.cloudinary.cloudinaryConfig(), { resource_type: 'raw' }),
) + '.transcript',
maxWords: config.maxWords || 5 // Number of words per caption
maxWords: config.maxWords || 5, // Number of words per caption
tsi marked this conversation as resolved.
Show resolved Hide resolved
wordHighlight: config.wordHighlight
};

const classNames = player.textTrackDisplay.el().classList;
classNames.add('cld-paced-text-tracks');

// Load the transcription file
const initTranscript = async () => {
try {
Expand Down Expand Up @@ -46,26 +50,57 @@ function pacedTranscript(config) {

// Generate captions from the transcription data
const parseTranscript = transcriptionData => {
const maxWords = options.maxWords;
const captions = [];

transcriptionData.forEach(segment => {
const words = segment.words;
const maxWords = options.maxWords || words.length;

for (let i = 0; i < words.length; i += maxWords) {
const startTime = words[i].start_time;
const endTime = words[Math.min(i + maxWords - 1, words.length - 1)].end_time;

const captionText = words
.slice(i, i + maxWords)
.map(word => word.word)
.join(' ');

captions.push({
startTime: startTime,
endTime: endTime,
text: captionText
});
if (options.wordHighlight) {
// Create a caption for every word, in which the current word is highlighted
const slicedWords = words.slice(i, Math.min(i + maxWords, words.length));
slicedWords.forEach((word, idx) => {
const captionText = words
.slice(i, i + maxWords)
.map(w => (w === word ? `<b>${w.word}</b>` : w.word))
.join(' ');

captions.push({
startTime: word.start_time,
endTime: word.end_time,
text: captionText
});

// if we haven't reached the end of the words array, and there's a gap between the current word end_time and the next word start_time, add a non-highlighted caption to fill the gap
if (words[idx + 1] && word.end_time < words[idx + 1].start_time) {
const captionText = words
.slice(i, i + maxWords)
.map(word => word.word)
.join(' ');

captions.push({
startTime: word.end_time,
endTime: words[idx + 1].start_time,
text: captionText
});
}
});
} else {
const startTime = words[i].start_time;
const endTime = words[Math.min(i + maxWords - 1, words.length - 1)].end_time;

const captionText = words
.slice(i, i + maxWords)
.map(word => word.word)
.join(' ');

captions.push({
startTime: startTime,
endTime: endTime,
text: captionText
});
}
tsi marked this conversation as resolved.
Show resolved Hide resolved
}
});

Expand Down
11 changes: 10 additions & 1 deletion src/plugins/styled-text-tracks/styled-text-tracks.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const styledTextTracks = (config, player) => {
fontSize: config.fontSize,
gravity: config.gravity || 'bottom',
box: config.box,
style: config.style
style: config.style,
wordHighlightStyle: config.wordHighlightStyle
};

// Class Names - Theme/Gravity
Expand Down Expand Up @@ -75,6 +76,14 @@ const styledTextTracks = (config, player) => {
'.vjs-text-track-display.cld-styled-text-tracks .vjs-text-track-cue > div'
);
}

// Custom styles
if (options.wordHighlightStyle) {
applyImportantStyle(
options.wordHighlightStyle,
'.vjs-text-track-display.cld-styled-text-tracks .vjs-text-track-cue b'
);
}
};

export default styledTextTracks;
6 changes: 3 additions & 3 deletions src/plugins/styled-text-tracks/styled-text-tracks.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
.vjs-text-track-display {

&.cld-styled-text-tracks-theme-yellow-outlined {
.vjs-text-track-cue {
div.vjs-text-track-cue {
& > div {
color: #FEF94A !important;
text-shadow:
Expand All @@ -44,7 +44,7 @@
}

&.cld-styled-text-tracks-theme-3d {
.vjs-text-track-cue {
div.vjs-text-track-cue {
& > div {
$base-size: 0.03em;
$base-color: #ff76ad;
Expand All @@ -59,7 +59,7 @@
}

&.cld-styled-text-tracks-theme-player-colors {
.vjs-text-track-cue {
div.vjs-text-track-cue {
& > div {
color: var(--color-text) !important;
background-color: var(--color-accent) !important;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/cloudinary.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const isKeyInTransformation = (transformation, key) => {

const addTextTracks = (tracks, videojs) => {
tracks.forEach(track => {
if (track.maxWords && videojs.pacedTranscript) {
if ((track.maxWords || track.wordHighlight) && videojs.pacedTranscript) {
videojs.pacedTranscript(track);
} else if (track.src) {
fetch(track.src, GET_ERROR_DEFAULT_REQUEST).then(r => {
Expand Down
4 changes: 3 additions & 1 deletion src/utils/get-analytics-player-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ const getSourceOptions = (sourceOptions = {}) => ({
...(sourceOptions.textTracks ? {
textTracks: hasConfig(sourceOptions.textTracks),
pacedTextTracks: hasConfig(sourceOptions.textTracks) && JSON.stringify(sourceOptions.textTracks || {}).includes('"maxWords":'),
wordHighlight: hasConfig(sourceOptions.textTracks) && JSON.stringify(sourceOptions.textTracks || {}).includes('"wordHighlight":'),
...(sourceOptions.textTracks.options ? {
styledTextTracksTheme: sourceOptions.textTracks.options.theme,
styledTextTracksFont: sourceOptions.textTracks.options.fontFace,
styledTextTracksFontSize: sourceOptions.textTracks.options.fontSize,
styledTextTracksGravity: sourceOptions.textTracks.options.gravity,
styledTextTracksBox: hasConfig(sourceOptions.textTracks.options.box),
styledTextTracksStyle: hasConfig(sourceOptions.textTracks.options.style)
styledTextTracksStyle: hasConfig(sourceOptions.textTracks.options.style),
styledTextTracksWordHighlightStyle: hasConfig(sourceOptions.textTracks.options.wordHighlightStyle)
} : {})
} : {})
});
Expand Down
9 changes: 6 additions & 3 deletions src/validators/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,24 @@ export const sourceValidators = {
fontSize: validator.isString,
gravity: validator.isString,
box: validator.isPlainObject,
style: validator.isPlainObject
style: validator.isPlainObject,
wordHighlightStyle: validator.isPlainObject
},
captions: {
label: validator.isString,
language: validator.isString,
default: validator.isBoolean,
url: validator.isString,
maxWords: validator.isNumber
maxWords: validator.isNumber,
wordHighlight: validator.isBoolean
},
subtitles: validator.isArrayOfObjects({
label: validator.isString,
language: validator.isString,
default: validator.isBoolean,
url: validator.isString,
maxWords: validator.isNumber
maxWords: validator.isNumber,
wordHighlight: validator.isBoolean
})
},
info: {
Expand Down
Loading