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

Add subtitles to video previews #91

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Changes from 4 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
103 changes: 102 additions & 1 deletion previewers/betatest/js/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Member

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?

const id = queryParams.get("datasetid");
const siteUrl = queryParams.get("siteUrl");
const versionUrl = `${siteUrl}/api/datasets/${id}/versions/`
Copy link
Member

Choose a reason for hiding this comment

The 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}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should keep the ?gbrecs=true param - with that, Dataverse doesn't count this as a download, which is what we want when the use is in a preview and not from an actual download by the user.

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);
}