@stremio/stremio-video 0.0.14 → 0.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stremio/stremio-video",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
4
4
  "description": "Abstraction layer on top of different media players",
5
5
  "author": "Smart Code OOD",
6
6
  "main": "src/index.js",
@@ -0,0 +1,67 @@
1
+ // from: https://github.com/silviapfeiffer/silviapfeiffer.github.io/blob/master/index.html#L150-L216
2
+
3
+ function srt2webvtt(data) {
4
+ // remove dos newlines
5
+ var srt = data.replace(/\r+/g, '');
6
+ // trim white space start and end
7
+ srt = srt.replace(/^\s+|\s+$/g, '');
8
+ // get cues
9
+ var cuelist = srt.split('\n\n');
10
+ var result = '';
11
+ if (cuelist.length > 0) {
12
+ result += 'WEBVTT\n\n';
13
+ for (var i = 0; i < cuelist.length; i=i+1) {
14
+ result += convertSrtCue(cuelist[i]);
15
+ }
16
+ }
17
+ return result;
18
+ }
19
+
20
+ function convertSrtCue(caption) {
21
+ // remove all html tags for security reasons
22
+ caption = caption.replace(/<[a-zA-Z/][^>]*>/g, '');
23
+
24
+ var cue = '';
25
+ var s = caption.split(/\n/);
26
+ // concatenate muilt-line string separated in array into one
27
+ while (s.length > 3) {
28
+ for (var i = 3; i < s.length; i++) {
29
+ s[2] += '\n' + s[i];
30
+ }
31
+ s.splice(3, s.length - 3);
32
+ }
33
+ var line = 0;
34
+ // detect identifier
35
+ if (!s[0].match(/\d+:\d+:\d+/) && s[1].match(/\d+:\d+:\d+/)) {
36
+ cue += s[0].match(/\w+/) + '\n';
37
+ line += 1;
38
+ }
39
+ // get time strings
40
+ if (s[line].match(/\d+:\d+:\d+/)) {
41
+ // convert time string
42
+ var m = s[1].match(/(\d+):(\d+):(\d+)(?:,(\d+))?\s*--?>\s*(\d+):(\d+):(\d+)(?:,(\d+))?/);
43
+ if (m) {
44
+ cue += m[1]+':'+m[2]+':'+m[3]+'.'+m[4]+' --> '
45
+ +m[5]+':'+m[6]+':'+m[7]+'.'+m[8]+'\n';
46
+ line += 1;
47
+ } else {
48
+ // Unrecognized timestring
49
+ return '';
50
+ }
51
+ } else {
52
+ // file format error or comment lines
53
+ return '';
54
+ }
55
+ // get cue text
56
+ if (s[line]) {
57
+ cue += s[line] + '\n\n';
58
+ }
59
+ return cue;
60
+ }
61
+
62
+ module.exports = {
63
+ convert: function(text) {
64
+ // presume all to be SRT if not WEBVTT
65
+ return text.includes('WEBVTT') ? text : srt2webvtt(text);
66
+ }
67
+ };
@@ -4,6 +4,7 @@ var deepFreeze = require('deep-freeze');
4
4
  var ERROR = require('../error');
5
5
  var subtitlesParser = require('./subtitlesParser');
6
6
  var subtitlesRenderer = require('./subtitlesRenderer');
7
+ var subtitlesConverter = require('./subtitlesConverter');
7
8
 
8
9
  function withHTMLSubtitles(Video) {
9
10
  function VideoWithHTMLSubtitles(options) {
@@ -214,6 +215,9 @@ function withHTMLSubtitles(Video) {
214
215
  .then(function(resp) {
215
216
  return resp.text();
216
217
  })
218
+ .then(function(text) {
219
+ return subtitlesConverter.convert(text);
220
+ })
217
221
  .then(function(text) {
218
222
  return subtitlesParser.parse(text);
219
223
  })