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 |
|
29 | var Iconv = null;
|
30 | var path = null;
|
31 | var fs = null;
|
32 | var DetectEncoding = null;
|
33 |
|
34 | if (!process.browser) {
|
35 | Iconv = require('iconv').Iconv;
|
36 | path = require('path');
|
37 | fs = require('fs');
|
38 | DetectEncoding = require('detect-character-encoding');
|
39 | }
|
40 |
|
41 | var SubtitleConverter = function () {
|
42 | function SubtitleConverter() {
|
43 | for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
|
44 | args[_key] = arguments[_key];
|
45 | }
|
46 |
|
47 | _classCallCheck(this, SubtitleConverter);
|
48 |
|
49 | this.loaded = false;
|
50 | if (arguments.length > 0) {
|
51 | this.load.apply(this, args);
|
52 | }
|
53 | }
|
54 |
|
55 | _createClass(SubtitleConverter, [{
|
56 | key: 'load',
|
57 | value: function load(filename, ext, encode) {
|
58 | var targetencode = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'UTF-8';
|
59 |
|
60 | try {
|
61 | if (!process.browser) {
|
62 | var raw = fs.readFileSync(filename);
|
63 | var autoencode = DetectEncoding(raw);
|
64 | var enc = null;
|
65 | if (arguments.length <= 2) {
|
66 | enc = autoencode.encoding;
|
67 | } else if (autoencode.encoding !== encode.toUpperCase()) {
|
68 | console.log('[Warning] Expected encoding : ' + autoencode.encoding + ' / Input : ' + encode);
|
69 | }
|
70 | if (!enc) {
|
71 | enc = encode;
|
72 | }
|
73 | var iconv = new Iconv(enc, targetencode);
|
74 | var convraw = iconv.convert(raw);
|
75 | this.encoded = new _textEncoding.TextDecoder(targetencode).decode(convraw);
|
76 | this.originext = ext || path.extname(filename).toUpperCase();
|
77 | } else {
|
78 | this.encoded = filename;
|
79 | this.originext = ext;
|
80 | }
|
81 | this.originext = this.originext.toUpperCase();
|
82 | this.targetencode = targetencode;
|
83 | this.parsed = false;
|
84 | this.loaded = true;
|
85 | return true;
|
86 | } catch (e) {
|
87 | console.log(e);
|
88 | return false;
|
89 | }
|
90 | }
|
91 | }, {
|
92 | key: 'parse',
|
93 | value: function parse(object) {
|
94 | if (object !== undefined) {
|
95 | if (typeof object === 'string') {
|
96 | try {
|
97 | var that = JSON.parse(object);
|
98 | this.originext = that.originext;
|
99 | this.targetencode = that.targetencode;
|
100 | this.parsed = that.parsed;
|
101 | this.loaded = that.loaded;
|
102 | this.parsed.cueList = this.parsed.cueList.map(function (el) {
|
103 | var newel = Object.assign({}, el);
|
104 | newel.start = new Date(el.start);
|
105 | newel.end = new Date(el.end);
|
106 | return newel;
|
107 | });
|
108 | return true;
|
109 | } catch (e) {
|
110 | return false;
|
111 | }
|
112 | } else {
|
113 | return false;
|
114 | }
|
115 | }
|
116 | if (!this.loaded) {
|
117 | return false;
|
118 | }
|
119 | switch (this.originext) {
|
120 | case '.SMI':
|
121 | this.parsed = _parser2.default.SMIParser(this.encoded);
|
122 | break;
|
123 | case '.VTT':
|
124 | this.parsed = _parser2.default.VTTParser(this.encoded);
|
125 | break;
|
126 | default:
|
127 | return false;
|
128 | }
|
129 | return true;
|
130 | }
|
131 | }, {
|
132 | key: 'convert',
|
133 | value: function convert(type, target) {
|
134 | var res = false;
|
135 | if (!this.parsed && !this.parse()) {
|
136 | console.log('\n Parse failed! you may forgot to load before covert or\n there is no method to parse : ' + this.originext + '.\n ');
|
137 | return false;
|
138 | }
|
139 | if (type === undefined) {
|
140 | console.log('\n There is no type to convert : ' + type + '. Please check arguments.\n ');
|
141 | return false;
|
142 | }
|
143 | var upperType = type.toUpperCase();
|
144 | if (upperType === '.VTT') {
|
145 | res = new _writer2.default.VTTWriter(this.parsed, type, this.targetencode, fs).write(target);
|
146 | } else {
|
147 | console.log('\n There are no such types to convert : ' + upperType + '\n ');
|
148 | return res;
|
149 | }
|
150 | return res;
|
151 | }
|
152 | }, {
|
153 | key: 'delay',
|
154 | value: function delay(time, index, end, resize) {
|
155 | if (!this.parsed) {
|
156 | console.log('\n You must parse data before using this method.\n ');
|
157 | return false;
|
158 | }
|
159 |
|
160 | var _time$toString$split = time.toString(10).split('.'),
|
161 | _time$toString$split2 = _slicedToArray(_time$toString$split, 2),
|
162 | sec = _time$toString$split2[0],
|
163 | milli = _time$toString$split2[1];
|
164 |
|
165 | if (milli !== undefined) {
|
166 | milli = parseInt(milli, 10);
|
167 | if (milli < 10) {
|
168 | milli *= 100;
|
169 | } else if (milli < 100) {
|
170 | milli *= 10;
|
171 | }
|
172 | if (sec.match('-')) {
|
173 | milli *= -1;
|
174 | }
|
175 | }
|
176 | sec = parseInt(sec, 10);
|
177 | var cueIndex = index || 0;
|
178 | var cueIndexEnd = index || this.parsed.cueList.length - 1;
|
179 | if (cueIndex < this.parsed.cueList.length) {
|
180 | if (index && typeof end === 'number') {
|
181 | if (end >= 0) {
|
182 | cueIndexEnd = end;
|
183 | } else {
|
184 | cueIndexEnd = this.parsed.cueList.length - 1;
|
185 | }
|
186 | }
|
187 | if (cueIndexEnd < this.parsed.cueList.length) {
|
188 | for (var i = cueIndex; i <= cueIndexEnd; i += 1) {
|
189 | resize !== undefined || this.parsed.cueList[i].start.setUTCSeconds(this.parsed.cueList[i].start.getUTCSeconds() + sec);
|
190 | this.parsed.cueList[i].end.setUTCSeconds(this.parsed.cueList[i].end.getUTCSeconds() + sec);
|
191 | if (milli !== undefined) {
|
192 | resize !== undefined || this.parsed.cueList[i].start.setUTCMilliseconds(this.parsed.cueList[i].start.getUTCMilliseconds() + milli);
|
193 | this.parsed.cueList[i].end.setUTCMilliseconds(this.parsed.cueList[i].end.getUTCMilliseconds() + milli);
|
194 | }
|
195 | }
|
196 | return true;
|
197 | }
|
198 | } else {
|
199 | return false;
|
200 | }
|
201 | return true;
|
202 | }
|
203 | }, {
|
204 | key: 'resize',
|
205 | value: function resize(time, index, end) {
|
206 | return this.delay(time, index, end, true);
|
207 | }
|
208 | }, {
|
209 | key: 'stringify',
|
210 | value: function stringify() {
|
211 | var that = {
|
212 | originext: this.originext,
|
213 | targetencode: this.targetencode,
|
214 | loaded: this.loaded,
|
215 | parsed: this.parsed
|
216 | };
|
217 | return JSON.stringify(that);
|
218 | }
|
219 | }, {
|
220 | key: 'apply',
|
221 | value: function apply(cueList) {
|
222 | if ((typeof cueList === 'undefined' ? 'undefined' : _typeof(cueList)) === 'object') {
|
223 | |
224 |
|
225 |
|
226 |
|
227 |
|
228 | for (var i = 0; i < cueList.length; i += 1) {
|
229 | var arr = cueList[i];
|
230 | var el = this.parsed.cueList[parseInt(arr.id, 10)];
|
231 | arr.endTime = el.end.getUTCMinutes() * 60 + el.end.getUTCSeconds() + el.end.getUTCMilliseconds() * 0.001;
|
232 | arr.startTime = el.start.getUTCMinutes() * 60 + el.start.getUTCSeconds() + el.start.getUTCMilliseconds() * 0.001;
|
233 | arr.text = el.text;
|
234 | }
|
235 | return true;
|
236 | }
|
237 | return false;
|
238 | }
|
239 | }]);
|
240 |
|
241 | return SubtitleConverter;
|
242 | }();
|
243 |
|
244 | exports.default = SubtitleConverter; |
\ | No newline at end of file |