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

feat(withHTMLSubtitles): handle local subtitles #93

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
},
"env": {
"commonjs": true,
"browser": true
"browser": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 8
Expand Down
3 changes: 3 additions & 0 deletions src/StremioVideo/StremioVideo.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ function StremioVideo() {
video.on('extraSubtitlesTrackLoaded', function(track) {
events.emit('extraSubtitlesTrackLoaded', track);
});
video.on('extraSubtitlesTrackAdded', function(track) {
events.emit('extraSubtitlesTrackAdded', track);
});
if (Video.manifest.external) {
video.on('implementationChanged', function(manifest) {
events.emit('implementationChanged', manifest);
Expand Down
69 changes: 56 additions & 13 deletions src/withHTMLSubtitles/withHTMLSubtitles.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,34 @@ function withHTMLSubtitles(Video) {
if (selectedTrack) {
selectedTrackId = selectedTrack.id;
delay = 0;
function loadSubtitleFromUrl(url, isFallback) {
fetch(url)
.then(function(resp) {
if (resp.ok) {
return resp.text();
}

throw new Error(resp.status + ' (' + resp.statusText + ')');
})
function getSubtitlesData(track) {
if (typeof track.url === 'string') {
return fetch(track.url)
.then(function(resp) {
if (resp.ok) {
return resp.text();
}

throw new Error(resp.status + ' (' + resp.statusText + ')');
});
}

if (track.buffer instanceof ArrayBuffer) {
try {
const uInt8Array = new Uint8Array(track.buffer);
const text = new TextDecoder().decode(uInt8Array);
return Promise.resolve(text);
} catch(e) {
return Promise.reject(e);
}
}

return Promise.reject('No `url` or `buffer` field available for this track');
}

function loadSubtitles(track, isFallback) {
getSubtitlesData(track)
.then(function(text) {
return subtitlesConverter.convert(text);
})
Expand All @@ -255,7 +274,7 @@ function withHTMLSubtitles(Video) {
}

if (!isFallback && typeof selectedTrack.fallbackUrl === 'string') {
loadSubtitleFromUrl(selectedTrack.fallbackUrl, true);
loadSubtitles(selectedTrack, true);
return;
}

Expand All @@ -266,7 +285,7 @@ function withHTMLSubtitles(Video) {
}));
});
}
loadSubtitleFromUrl(selectedTrack.url);
loadSubtitles(selectedTrack);
}
renderSubtitles();
onPropChanged('selectedExtraSubtitlesTrackId');
Expand Down Expand Up @@ -374,7 +393,6 @@ function withHTMLSubtitles(Video) {
.filter(function(track, index, tracks) {
return track &&
typeof track.id === 'string' &&
typeof track.url === 'string' &&
typeof track.lang === 'string' &&
typeof track.label === 'string' &&
typeof track.origin === 'string' &&
Expand All @@ -386,6 +404,31 @@ function withHTMLSubtitles(Video) {

return true;
}
case 'addLocalSubtitles': {
if (commandArgs && typeof commandArgs.filename === 'string' && commandArgs.buffer instanceof ArrayBuffer) {
var id = 'LOCAL_' + tracks
.filter(function(track) { return track.local; })
.length;

var track = {
id: id,
url: null,
buffer: commandArgs.buffer,
lang: 'local',
label: commandArgs.filename,
origin: 'LOCAL',
local: true,
embedded: false,
};

tracks.push(track);

onPropChanged('extraSubtitlesTracks');
events.emit('extraSubtitlesTrackAdded', track);
}

return true;
}
case 'load': {
command('unload');
if (commandArgs.stream && Array.isArray(commandArgs.stream.subtitles)) {
Expand Down Expand Up @@ -485,9 +528,9 @@ function withHTMLSubtitles(Video) {
external: Video.manifest.external,
props: Video.manifest.props.concat(['extraSubtitlesTracks', 'selectedExtraSubtitlesTrackId', 'extraSubtitlesDelay', 'extraSubtitlesSize', 'extraSubtitlesOffset', 'extraSubtitlesTextColor', 'extraSubtitlesBackgroundColor', 'extraSubtitlesOutlineColor', 'extraSubtitlesOpacity'])
.filter(function(value, index, array) { return array.indexOf(value) === index; }),
commands: Video.manifest.commands.concat(['load', 'unload', 'destroy', 'addExtraSubtitlesTracks'])
commands: Video.manifest.commands.concat(['load', 'unload', 'destroy', 'addExtraSubtitlesTracks', 'addLocalSubtitles'])
.filter(function(value, index, array) { return array.indexOf(value) === index; }),
events: Video.manifest.events.concat(['propValue', 'propChanged', 'error', 'extraSubtitlesTrackLoaded'])
events: Video.manifest.events.concat(['propValue', 'propChanged', 'error', 'extraSubtitlesTrackLoaded', 'extraSubtitlesTrackAdded'])
.filter(function(value, index, array) { return array.indexOf(value) === index; })
};

Expand Down
Loading