Skip to content

Commit

Permalink
Merge pull request #10 from Stremio/srt-to-vtt
Browse files Browse the repository at this point in the history
Subtitles Converter: SRT to VTT
  • Loading branch information
nklhtv authored Feb 21, 2022
2 parents ad0f328 + c714778 commit b43613b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/withHTMLSubtitles/subtitlesConverter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// from: https://github.com/silviapfeiffer/silviapfeiffer.github.io/blob/master/index.html#L150-L216

function srt2webvtt(data) {
// remove dos newlines
var srt = data.replace(/\r+/g, '');
// trim white space start and end
srt = srt.replace(/^\s+|\s+$/g, '');
// get cues
var cuelist = srt.split('\n\n');
var result = '';
if (cuelist.length > 0) {
result += 'WEBVTT\n\n';
for (var i = 0; i < cuelist.length; i=i+1) {
result += convertSrtCue(cuelist[i]);
}
}
return result;
}

function convertSrtCue(caption) {
// remove all html tags for security reasons
caption = caption.replace(/<[a-zA-Z/][^>]*>/g, '');

var cue = '';
var s = caption.split(/\n/);
// concatenate muilt-line string separated in array into one
while (s.length > 3) {
for (var i = 3; i < s.length; i++) {
s[2] += '\n' + s[i];
}
s.splice(3, s.length - 3);
}
var line = 0;
// detect identifier
if (!s[0].match(/\d+:\d+:\d+/) && s[1].match(/\d+:\d+:\d+/)) {
cue += s[0].match(/\w+/) + '\n';
line += 1;
}
// get time strings
if (s[line].match(/\d+:\d+:\d+/)) {
// convert time string
var m = s[1].match(/(\d+):(\d+):(\d+)(?:,(\d+))?\s*--?>\s*(\d+):(\d+):(\d+)(?:,(\d+))?/);
if (m) {
cue += m[1]+':'+m[2]+':'+m[3]+'.'+m[4]+' --> '
+m[5]+':'+m[6]+':'+m[7]+'.'+m[8]+'\n';
line += 1;
} else {
// Unrecognized timestring
return '';
}
} else {
// file format error or comment lines
return '';
}
// get cue text
if (s[line]) {
cue += s[line] + '\n\n';
}
return cue;
}

module.exports = {
convert: function(text) {
// presume all to be SRT if not WEBVTT
return text.includes('WEBVTT') ? text : srt2webvtt(text);
}
};
4 changes: 4 additions & 0 deletions src/withHTMLSubtitles/withHTMLSubtitles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var deepFreeze = require('deep-freeze');
var ERROR = require('../error');
var subtitlesParser = require('./subtitlesParser');
var subtitlesRenderer = require('./subtitlesRenderer');
var subtitlesConverter = require('./subtitlesConverter');

function withHTMLSubtitles(Video) {
function VideoWithHTMLSubtitles(options) {
Expand Down Expand Up @@ -214,6 +215,9 @@ function withHTMLSubtitles(Video) {
.then(function(resp) {
return resp.text();
})
.then(function(text) {
return subtitlesConverter.convert(text);
})
.then(function(text) {
return subtitlesParser.parse(text);
})
Expand Down

0 comments on commit b43613b

Please sign in to comment.