-
Notifications
You must be signed in to change notification settings - Fork 19
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 subtitles to video previews #91
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,104 @@ function translateBaseHtmlPage() { | |
|
||
function writeContent(fileUrl, file, title, authors) { | ||
addStandardPreviewHeader(file, title, authors); | ||
$(".preview").append($("<video/>").prop("controls",true).append($('<source/>').attr("src",fileUrl))); | ||
|
||
const queryParams = new URLSearchParams(window.location.search.substring(1)); | ||
const id = queryParams.get("datasetid"); | ||
const siteUrl = queryParams.get("siteUrl"); | ||
const versionUrl = `${siteUrl}/api/datasets/${id}/versions/` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Someday it would make sense to have the retriever.js, which already gets this URL, either pass the json it gets or at least pass the signed URL (and other signed URLs, api key, etc.) through the writeContent method interface). Happy to avoid that now, but, per the comment below, it looks like at least the api key would have to be made available if this is to work with draft/restricted files. |
||
+ queryParams.get("datasetversion"); | ||
const videoId =queryParams.get("fileid") * 1; // converted to number | ||
const userLanguages = [...navigator.languages]; | ||
const locale = queryParams.get("locale"); | ||
if (locale && !userLanguages.includes(locale)) { | ||
userLanguages.unshift(locale); // add as first element | ||
} | ||
|
||
$.ajax({ | ||
type: 'GET', | ||
dataType: 'json', | ||
crosssite: true, | ||
url: versionUrl, | ||
success: function(data, status) { | ||
appendVideoElements(fileUrl, videoId, data.data.files, siteUrl, userLanguages); | ||
}, | ||
error: function(request, status, error) { | ||
// fallback to simple video element | ||
$(".preview").append($("<video/>") .prop("controls", true) .append($('<source/>').attr("src", fileUrl))) | ||
} | ||
}); | ||
} | ||
|
||
function appendVideoElements(fileUrl, videoId, files, siteUrl, userLanguages) { | ||
|
||
const baseName = files // the video file name without extension | ||
.find(item => item.dataFile.id === videoId) | ||
.label.replace(/\.[a-z0-9]+$/i,''); | ||
|
||
// find labels like "baseName.en.vtt", "baseName.de-CH.vtt" or "baseName.vtt" | ||
const regex = new RegExp(`${baseName}(\\.([-a-z]+))?\\.vtt$`, 'i') | ||
|
||
// create a map of URLs with their (optional) language | ||
const subtitles = files | ||
.filter(item => regex.test(item.label)) | ||
.reduce((map, item) => { | ||
const lang = item.label.match(regex)[2]; | ||
const url = `${siteUrl}/api/access/datafile/${item.dataFile.id}?gbrecs=true&key=93423e09-848c-47cb-a979-219dafcfa4da`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hardcoded key? It looks like this will have to run with the apikey sent in (for draft versions anyway) rather than signedURLs because we can't currently support allowing you to get signedUrls for other files? If so, it might be better to make this a separate previewer (videoWithCaptions.js?) or maybe just use the presence of the API key ( just for non-draft datasets/non-restricted files) as a way to decide whether to try and get the captions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dropping the API key resulted in desired behavior. When logged in, a restricted video appears with subtitles. When not logged in and viewing a not restricted video with restricted subtitles, the menu shows available languages but the subtitles don't appear. |
||
map.set(url, lang); | ||
return map; | ||
}, new Map()); | ||
|
||
// sort subtitles by language value, 'de-CH' before 'de' | ||
const sortedSubtitles = new Map([...subtitles.entries()].sort((a, b) => { | ||
if (!a[1]) return 1; | ||
if (!b[1]) return -1; | ||
if (a[1].startsWith(b[1])) return -1; | ||
if (b[1].startsWith(a[1])) return 1; | ||
return a[1].localeCompare(b[1]); | ||
})); | ||
|
||
// determine default track | ||
let defaultTrackUrl = null; | ||
let trackUrlWithoutLang = null; | ||
loop: for (const lang of userLanguages) { | ||
for (const [url, trackLang] of sortedSubtitles) { | ||
if (trackLang) { | ||
if (trackLang === lang || trackLang.startsWith(lang.replace(/-.*/, ''))) { | ||
defaultTrackUrl = url; | ||
break loop; | ||
} | ||
} else { | ||
trackUrlWithoutLang = url; | ||
} | ||
} | ||
} | ||
if (!defaultTrackUrl) { | ||
defaultTrackUrl = trackUrlWithoutLang; | ||
} | ||
if (!defaultTrackUrl && subtitles) { | ||
defaultTrackUrl = subtitles.values().next().value; | ||
} | ||
|
||
const videoElement = $("<video/>") | ||
.prop("controls", true) | ||
.append($('<source/>').attr("src", fileUrl)); | ||
|
||
sortedSubtitles.forEach((trackLang, url) => { | ||
const trackElement = $('<track/>') | ||
.attr("kind", "subtitles") | ||
.attr("src", url); | ||
if (trackLang) { | ||
trackElement | ||
.attr("label", trackLang) | ||
.attr("srclang", trackLang); | ||
} else { | ||
trackElement.attr("label", "???"); | ||
} | ||
if (url === defaultTrackUrl) { | ||
trackElement.attr("default", true); | ||
} | ||
videoElement.append(trackElement); | ||
}); | ||
|
||
$(".preview").append(videoElement); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only works when signedUrls aren't used. Otherwise, the id and siteUrl aren't sent as params and those variables get null below, leading to error messages when the ajax call at line 25 is made. Can you add an if siteUrl is null check that would also just revert to the fallback in line 35 to avoid that error message?