From 3d624a5f0fb47b7015618d90bd924ccc04e06b19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 30 Mar 2020 09:39:42 +0300 Subject: [PATCH] Allow loading .vtt from remote urls This bypasses need for local server and need to keep command running --- plugins/subtitles.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/plugins/subtitles.js b/plugins/subtitles.js index 4acd435..0a66cd7 100644 --- a/plugins/subtitles.js +++ b/plugins/subtitles.js @@ -34,10 +34,23 @@ var findSubtitles = function(options) { } return; -} +}; + +var isRemote = function(path) { + var url = new URL(path); + return ['http:', 'https:'].indexOf(url.protocol) !== -1; +}; + +var isExtension = function(path, extension) { + return path.substr(-4).toLowerCase() === '.' + extension; +}; var isSrt = function(path) { - return path.substr(-4).toLowerCase() === '.srt'; + return isExtension(path, 'srt'); +}; + +var isVtt = function(path) { + return isExtension(path, 'vtt'); }; var attachSubtitles = function(ctx) { @@ -90,10 +103,16 @@ var subtitles = function(ctx, next) { } } + if (isVtt(ctx.options.subtitles) && isRemote(ctx.options.subtitles)) { + debug('attaching remote subtitles', ctx.options.subtitles); + attachSubtitles(ctx); + return next(); + } + var port = ctx.options['subtitle-port'] || 4101; srtToVtt(ctx.options, function(err, data) { if (err) return next(); - debug('loading subtitles', ctx.options.subtitles); + debug('attaching local subtitles', ctx.options.subtitles); if (err) return next(); var ip = ctx.options.myip || internalIp.v4.sync(); var addr = 'http://' + ip + ':' + port;