1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
|
8 |
|
9 | var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
|
10 |
|
11 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
12 |
|
13 | var _textEncoding = require('text-encoding');
|
14 |
|
15 | var _writer = require('./writer');
|
16 |
|
17 | var _writer2 = _interopRequireDefault(_writer);
|
18 |
|
19 | var _parser = require('./parser');
|
20 |
|
21 | var _parser2 = _interopRequireDefault(_parser);
|
22 |
|
23 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
24 |
|
25 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
26 |
|
27 | var kEnvironment = process.env.NODE_ENV && process.env.NODE_ENV.trim().toLowerCase() === 'production' ? 'production' : 'development';
|
28 | var fs = null;
|
29 |
|
30 | var SubtitleConverter = function () {
|
31 | function SubtitleConverter() {
|
32 | for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
|
33 | args[_key] = arguments[_key];
|
34 | }
|
35 |
|
36 | _classCallCheck(this, SubtitleConverter);
|
37 |
|
38 | this.loaded = false;
|
39 | if (arguments.length > 0) {
|
40 | this.load.apply(this, args);
|
41 | }
|
42 | }
|
43 |
|
44 | _createClass(SubtitleConverter, [{
|
45 | key: 'load',
|
46 | value: function load(filename, ext, encode) {
|
47 | var targetencode = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'UTF-8';
|
48 |
|
49 | try {
|
50 | {
|
51 | this.encoded = filename;
|
52 | this.originext = ext;
|
53 | }
|
54 | this.originext = this.originext.toUpperCase();
|
55 | this.targetencode = targetencode;
|
56 | this.parsed = false;
|
57 | this.loaded = true;
|
58 | return true;
|
59 | } catch (e) {
|
60 | console.log(e);
|
61 | return false;
|
62 | }
|
63 | }
|
64 | }, {
|
65 | key: 'parse',
|
66 | value: function parse(object) {
|
67 | if (object !== undefined) {
|
68 | if (typeof object === 'string') {
|
69 | try {
|
70 | var that = JSON.parse(object);
|
71 | this.originext = that.originext;
|
72 | this.targetencode = that.targetencode;
|
73 | this.parsed = that.parsed;
|
74 | this.loaded = that.loaded;
|
75 | this.parsed.cueList = this.parsed.cueList.map(function (el) {
|
76 | var newel = Object.assign({}, el);
|
77 | newel.start = new Date(el.start);
|
78 | newel.end = new Date(el.end);
|
79 | return newel;
|
80 | });
|
81 | return true;
|
82 | } catch (e) {
|
83 | return false;
|
84 | }
|
85 | } else {
|
86 | return false;
|
87 | }
|
88 | }
|
89 | if (!this.loaded) {
|
90 | return false;
|
91 | }
|
92 | switch (this.originext) {
|
93 | case '.SMI':
|
94 | this.parsed = _parser2.default.SMIParser(this.encoded);
|
95 | break;
|
96 | case '.VTT':
|
97 | this.parsed = _parser2.default.VTTParser(this.encoded);
|
98 | break;
|
99 | default:
|
100 | return false;
|
101 | }
|
102 | return true;
|
103 | }
|
104 | }, {
|
105 | key: 'convert',
|
106 | value: function convert(type, target) {
|
107 | var res = false;
|
108 | if (!this.parsed && !this.parse()) {
|
109 | console.log('\n Parse failed! you may forgot to load before covert or\n there is no method to parse : ' + this.originext + '.\n ');
|
110 | return false;
|
111 | }
|
112 | if (type === undefined) {
|
113 | console.log('\n There is no type to convert : ' + type + '. Please check arguments.\n ');
|
114 | return false;
|
115 | }
|
116 | var upperType = type.toUpperCase();
|
117 | if (upperType === '.VTT') {
|
118 | res = new _writer2.default.VTTWriter(this.parsed, type, this.targetencode, fs).write(target);
|
119 | } else {
|
120 | console.log('\n There are no such types to convert : ' + upperType + '\n ');
|
121 | return res;
|
122 | }
|
123 | return res;
|
124 | }
|
125 | }, {
|
126 | key: 'delay',
|
127 | value: function delay(time, index, end, resize) {
|
128 | if (!this.parsed) {
|
129 | console.log('\n You must parse data before using this method.\n ');
|
130 | return false;
|
131 | }
|
132 |
|
133 | var _time$toString$split = time.toString(10).split('.'),
|
134 | _time$toString$split2 = _slicedToArray(_time$toString$split, 2),
|
135 | sec = _time$toString$split2[0],
|
136 | milli = _time$toString$split2[1];
|
137 |
|
138 | if (milli !== undefined) {
|
139 | milli = parseInt(milli, 10);
|
140 | if (milli < 10) {
|
141 | milli *= 100;
|
142 | } else if (milli < 100) {
|
143 | milli *= 10;
|
144 | }
|
145 | if (sec.match('-')) {
|
146 | milli *= -1;
|
147 | }
|
148 | }
|
149 | sec = parseInt(sec, 10);
|
150 | var cueIndex = index || 0;
|
151 | var cueIndexEnd = index || this.parsed.cueList.length - 1;
|
152 | if (cueIndex < this.parsed.cueList.length) {
|
153 | if (index && typeof end === 'number') {
|
154 | if (end >= 0) {
|
155 | cueIndexEnd = end;
|
156 | } else {
|
157 | cueIndexEnd = this.parsed.cueList.length - 1;
|
158 | }
|
159 | }
|
160 | if (cueIndexEnd < this.parsed.cueList.length) {
|
161 | for (var i = cueIndex; i <= cueIndexEnd; i += 1) {
|
162 | resize !== undefined || this.parsed.cueList[i].start.setUTCSeconds(this.parsed.cueList[i].start.getUTCSeconds() + sec);
|
163 | this.parsed.cueList[i].end.setUTCSeconds(this.parsed.cueList[i].end.getUTCSeconds() + sec);
|
164 | if (milli !== undefined) {
|
165 | resize !== undefined || this.parsed.cueList[i].start.setUTCMilliseconds(this.parsed.cueList[i].start.getUTCMilliseconds() + milli);
|
166 | this.parsed.cueList[i].end.setUTCMilliseconds(this.parsed.cueList[i].end.getUTCMilliseconds() + milli);
|
167 | }
|
168 | }
|
169 | return true;
|
170 | }
|
171 | } else {
|
172 | return false;
|
173 | }
|
174 | return true;
|
175 | }
|
176 | }, {
|
177 | key: 'resize',
|
178 | value: function resize(time, index, end) {
|
179 | return this.delay(time, index, end, true);
|
180 | }
|
181 | }, {
|
182 | key: 'stringify',
|
183 | value: function stringify() {
|
184 | var that = {
|
185 | originext: this.originext,
|
186 | targetencode: this.targetencode,
|
187 | loaded: this.loaded,
|
188 | parsed: this.parsed
|
189 | };
|
190 | return JSON.stringify(that);
|
191 | }
|
192 | }, {
|
193 | key: 'apply',
|
194 | value: function apply(cueList) {
|
195 | if ((typeof cueList === 'undefined' ? 'undefined' : _typeof(cueList)) === 'object') {
|
196 | |
197 |
|
198 |
|
199 |
|
200 |
|
201 | for (var i = 0; i < cueList.length; i += 1) {
|
202 | var arr = cueList[i];
|
203 | var el = this.parsed.cueList[parseInt(arr.id, 10)];
|
204 | arr.endTime = el.end.getUTCMinutes() * 60 + el.end.getUTCSeconds() + el.end.getUTCMilliseconds() * 0.001;
|
205 | arr.startTime = el.start.getUTCMinutes() * 60 + el.start.getUTCSeconds() + el.start.getUTCMilliseconds() * 0.001;
|
206 | arr.text = el.text;
|
207 | }
|
208 | return true;
|
209 | }
|
210 | return false;
|
211 | }
|
212 | }]);
|
213 |
|
214 | return SubtitleConverter;
|
215 | }();
|
216 |
|
217 | exports.default = SubtitleConverter;
|