-
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 all commits
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,106 @@ 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 | ||
let trackUrlWithoutLang = null; | ||
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` | ||
map.set(url, lang); | ||
if (!lang) { | ||
trackUrlWithoutLang = url; | ||
} | ||
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; | ||
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; | ||
} | ||
} | ||
} | ||
} | ||
if (!defaultTrackUrl) { | ||
defaultTrackUrl = trackUrlWithoutLang; | ||
} | ||
if (!defaultTrackUrl && subtitles) { | ||
defaultTrackUrl = subtitles.keys().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", "???"); | ||
} | ||
console.log("url: ", url, "defaultTrackUrl: ", defaultTrackUrl); | ||
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?