Skip to content

Commit cf63bf5

Browse files
authored
feat: chapters/transcription template urls (applets) (#888)
* feat: chapters/transcription template urls (applets)
1 parent 45b6491 commit cf63bf5

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

src/plugins/chapters/chapters.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import videojs from 'video.js';
22

33
import './chapters.scss';
4-
import { extendCloudinaryConfig, getCloudinaryUrl } from '../cloudinary/common';
4+
import { getCloudinaryUrlPrefix } from '../cloudinary/common';
5+
import { utf8ToBase64 } from '../../utils/utf8Base64';
56

67
/**
78
* Chapters plugin.
@@ -67,10 +68,9 @@ const ChaptersPlugin = (function () {
6768
return null;
6869
}
6970

70-
const fullUrl = getCloudinaryUrl(
71-
`${currentPublicId}-chapters.vtt`,
72-
extendCloudinaryConfig(this.player.cloudinary.cloudinaryConfig(), { resource_type: 'raw', version: '1' })
73-
);
71+
const { type: deliveryType } = this.player.cloudinary.source().resourceConfig();
72+
const urlPrefix = getCloudinaryUrlPrefix(this.player.cloudinary.cloudinaryConfig());
73+
const fullUrl = `${urlPrefix}/_applet_/video_service/chapters/${deliveryType}/${utf8ToBase64(currentPublicId)}.vtt`;
7474
return `${fullUrl}?t=${Date.now()}`;
7575
};
7676

src/plugins/paced-transcript/index.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { getCloudinaryUrl, extendCloudinaryConfig } from 'plugins/cloudinary/common';
1+
import { getCloudinaryUrlPrefix } from '../cloudinary/common';
2+
import { utf8ToBase64 } from '../../utils/utf8Base64';
23

34
const fallbackFetch = async (url, fallback) => {
45
try {
@@ -32,6 +33,9 @@ function pacedTranscript(config) {
3233
const classNames = player.textTrackDisplay.el().classList;
3334
classNames.add('cld-paced-text-tracks');
3435

36+
const getTranscriptionFileUrl = (urlPrefix, deliveryType, publicId, languageCode = null) =>
37+
`${urlPrefix}/_applet_/video_service/transcription/${deliveryType}/${languageCode ? `${languageCode}/` : ''}${utf8ToBase64(publicId)}.transcript`;
38+
3539
// Load the transcription file
3640
const initTranscript = async () => {
3741

@@ -42,14 +46,16 @@ function pacedTranscript(config) {
4246
} else {
4347
// If not, and provided language, try fetching translated transcript, fallback to base transcript
4448
const source = player.cloudinary.source();
45-
const basePath = getCloudinaryUrl(
46-
source.publicId(),
47-
extendCloudinaryConfig(player.cloudinary.cloudinaryConfig(), { resource_type: 'raw' })
48-
);
49+
const sourcePublicId = source.publicId();
50+
const sourceDeliveryType = source.resourceConfig().type;
51+
const urlPrefix = getCloudinaryUrlPrefix(player.cloudinary.cloudinaryConfig());
52+
const transcriptionFileUrl = getTranscriptionFileUrl(urlPrefix, sourceDeliveryType, sourcePublicId);
53+
4954
if (options.srclang) {
50-
transcriptResponse = await fallbackFetch(`${basePath}.${options.srclang}.transcript`, `${basePath}.transcript`);
55+
const transcriptionTranslationFileUrl = getTranscriptionFileUrl(urlPrefix, sourceDeliveryType, sourcePublicId, options.srclang);
56+
transcriptResponse = await fallbackFetch(transcriptionTranslationFileUrl, transcriptionFileUrl);
5157
} else {
52-
transcriptResponse = await fallbackFetch(`${basePath}.transcript`);
58+
transcriptResponse = await fallbackFetch(transcriptionFileUrl);
5359
}
5460
}
5561
if (!transcriptResponse?.ok) return;

src/utils/utf8Base64.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function utf8ToBase64(str) {
2+
const utf8Bytes = new TextEncoder().encode(str);
3+
const binaryStr = String.fromCharCode(...utf8Bytes);
4+
return btoa(binaryStr);
5+
}

0 commit comments

Comments
 (0)