Attempt to support subtitles on Chromecast media too

This commit is contained in:
Fabio Manganiello 2019-02-12 11:13:19 +01:00
parent 5cbd0fdfe7
commit f68efc0d68

View file

@ -131,7 +131,7 @@ $(document).ready(function() {
}); });
}; };
const startStreaming = function(media, subtitles) { const startStreaming = function(media, subtitles, relativeURIs) {
if (media.startsWith('magnet:?')) { if (media.startsWith('magnet:?')) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
startStreamingTorrent(media) startStreamingTorrent(media)
@ -148,21 +148,29 @@ $(document).ready(function() {
data: JSON.stringify({ data: JSON.stringify({
'source': media, 'source': media,
'subtitles': subtitles, 'subtitles': subtitles,
}), })
}).done((response) => {
var url = response.url;
var subs;
if ('subtitles_url' in response) {
subs = response.subtitles_url;
}
complete: (xhr, textStatus) => { if (relativeURIs) {
if (xhr.status == 200) { url = url.match(/https?:\/\/[^\/]+(\/media\/.*)/)[1];
var uri = xhr.responseJSON.url.match(/https?:\/\/[^\/]+(\/media\/.*)/)[1] if (subs) {
var subtitles; subs = subs.match(/https?:\/\/[^\/]+(\/media\/.*)/)[1];
if ('subtitles_url' in xhr.responseJSON) {
subtitles = xhr.responseJSON.subtitles_url.match(/https?:\/\/[^\/]+(\/media\/.*)/)[1]
}
resolve(uri, subtitles);
} else {
reject(xhr.responseText);
} }
}, }
var ret = { 'url': url, 'subtitles': undefined };
if (subs) {
ret.subtitles = subs;
}
resolve(ret);
}).fail((xhr) => {
reject(xhr.responseText);
}); });
}); });
}; };
@ -229,33 +237,43 @@ $(document).ready(function() {
var requestArgs = { var requestArgs = {
action: 'media.chromecast.play', action: 'media.chromecast.play',
args: { args: {
'resource': resource,
'chromecast': device, 'chromecast': device,
}, },
}; };
// TODO support for subtitles on Chromecast through internal streaming server startStreaming(resource, subtitles).then((response) => {
requestArgs.args.resource = response.url;
// XXX subtitles currently break the Chromecast playback,
// see https://github.com/balloob/pychromecast/issues/74
// if (response.subtitles) {
// requestArgs.args.subtitles = response.subtitles;
// }
run(requestArgs).then((response) => { return run(requestArgs);
resolve(response); }).then((response) => {
if ('subtitles' in requestArgs) {
resolve(requestArgs.args.resource, requestArgs.args.subtitles);
} else {
resolve(requestArgs.args.resource);
}
}).catch((error) => { }).catch((error) => {
reject(error.message); reject(error);
}); });
}); });
}; };
const playInBrowser = (resource, subtitles) => { const playInBrowser = (resource, subtitles) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
startStreaming(resource, subtitles).then((url, subtitles) => { startStreaming(resource, subtitles, true).then((response) => {
browserVideoWindow = window.open( browserVideoWindow = window.open(
url + '?webplayer', '_blank'); response.url + '?webplayer', '_blank');
browserVideoWindow.addEventListener('load', () => { browserVideoWindow.addEventListener('load', () => {
browserVideoElement = browserVideoWindow.document browserVideoElement = browserVideoWindow.document
.querySelector('#video-player'); .querySelector('#video-player');
}); });
resolve(url, subtitles); resolve(response.url, response.subtitles);
}).catch((error) => { }).catch((error) => {
reject(error); reject(error);
}); });
@ -357,9 +375,9 @@ $(document).ready(function() {
}; };
onVideoLoading(); onVideoLoading();
startStreaming(resource) startStreaming(resource, undefined, true)
.then((url) => { .then((response) => {
url = url + '?download' var url = response.url + '?download'
window.open(url, '_blank'); window.open(url, '_blank');
resolve(url); resolve(url);
}) })
@ -470,12 +488,7 @@ $(document).ready(function() {
run({ run({
action: 'media.remove_subtitles' action: 'media.remove_subtitles'
}).then((response) => { }).then((response) => {
return run({ return setSubtitles(subtitles);
action: 'media.set_subtitles',
args: {
'filename': subtitles,
}
})
}).then((response) => { }).then((response) => {
resolve(response); resolve(response);
}).catch((error) => { }).catch((error) => {
@ -693,6 +706,7 @@ $(document).ready(function() {
onSuccess = function(results) { onSuccess = function(results) {
$devsList.find('.cast-device[data-remote]').remove(); $devsList.find('.cast-device[data-remote]').remove();
$devsRefreshBtn.removeClass('disabled');
if (!results || results.response.errors.length) { if (!results || results.response.errors.length) {
return; return;