-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathqencode.js
404 lines (341 loc) · 13.7 KB
/
qencode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* ========================================================================
* Qencode: qencode.js v1.2
* https://www.qencode.com/lib/qencode.js
* ========================================================================
* Copyright 2016-2020 Qencode, Inc.
* Licensed under MIT (https://www.qencode.com/lib/LICENSE)
======================================================================== */
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Qencode = (function() {
var call_server = async function(url, data, error_text, is_get, send_files) {
var request_type = is_get === true ? 'GET' : 'POST';
data = data === undefined ? {} : data;
if (error_text === undefined || error_text === null) {
error_text = 'error execute ' + url;
}
try {
const response = await fetch(url, {
method: request_type,
mode: 'cors',
cache: 'no-cache',
body: data
});
if (response.status === 0) {
console.log('Not connect.\n Verify Network.');
} else if (response.status == 404) {
console.log('Requested page not found. [404]');
} else if (response.status == 500) {
console.log('Internal Server Error [500].');
}
var result = await response.json();
console.log("Response:" + result);
if (!result) {
result = {
'error': false,
'message': error_text
};
}
} catch (err) {
result = {
'error': true,
'message': 'Error description: ' + err.message + '\n'
};
console.log("Connection error", err);
}
return result;
};
var call_server_get = async function(url, data, error_text) {
return await call_server(url, data, error_text, true);
};
var call_server_post = async function(url, data, error_text, send_files) {
return await call_server(url, data, error_text, false, send_files);
};
async function _create_task(url, token) {
var data = new FormData();
data.append('token', token);
return await call_server_post(url, data, null, true);
}
async function _start_encode(url, task_token, options) {
var data = new FormData();
data.append('task_token', task_token);
data.append('profiles', options.profile_ids.toString());
if (options.uri) {
data.append('uri', options.uri);
}
if (options.stitch) {
data.append('stitch', options.stitch);
}
if (options.transfer_method) {
data.append('transfer_method', transfer_method);
}
if (options.payload) {
data.append('payload', options.payload);
}
if (options.output_path_variables) {
data.append('output_path_variables', options.output_path_variables);
}
if (options.subtitles) {
data.append('subtitles', subtitles);
}
return await call_server_post(url, data, null, true);
}
async function _start_encode2(url, task_token, options) {
var data = new FormData();
data.append('task_token', task_token);
data.append('query', JSON.stringify({ "query": options.query }));
if (options.payload) {
data.append('payload', options.payload);
}
return await call_server_post(url, data, null, true);
}
var tus_upload = [];
function _upload_file(file, options) {
var ss = new tus.Upload(file, options);
tus_upload.push(ss);
try {
ss.start();
} catch (e) {
console.log('Tus error ' + e.name + ":" + e.message + "\n" + e.stack);
}
}
async function _get_status(url, tokens) {
var data = new FormData();
data.append('task_tokens', tokens);
data
return await call_server_post(url, data, null, true);
}
var defaultOptions = {
interval: 15,
endpoint: 'https://api.qencode.com1'
};
// --- lib ---
function Qencode(token, data, options) {
_classCallCheck(this, Qencode);
if (options == undefined) {
this.options = defaultOptions;
} else {
this.options = options;
}
this.options.token = token;
if ('query' in data) {
this.options.query = data.query;
if ('file' in data.query)
this.options.file = data.query.file;
} else {
this.options.profile_ids = data.profiles;
if ('file' in data)
this.options.file = data.file;
if ('uri' in data)
this.options.uri = data.uri;
if ('transfer_method' in data) {
this.options.transfer_method = data.transfer_method;
}
if ('payload' in data) {
this.options.payload = data.payload;
}
if ('subtitles' in data) {
this.options.subtitles = data.subtitles;
}
if ('start_time' in data) {
this.options.start_time = data.start_time;
}
if ('duration' in data) {
this.options.duration = data.duration;
}
if ('output_path_variables' in data) {
this.options.output_path_variables = data.output_path_variables;
}
}
this.task_token = '';
this.tus_url = null;
}
Qencode.prototype.start = function(job_done_callback, upload_progress_callback, chunk_size = 0, parallel_num = 1) {
this._launch_job(
this._start_encode_with_callback,
job_done_callback,
upload_progress_callback,
chunk_size, parallel_num
);
};
Qencode.prototype.start_custom = async function(job_done_callback, upload_progress_callback, chunk_size = 0, parallel_num = 1) {
await this._launch_job(
this._start_encode2_with_callback,
job_done_callback,
upload_progress_callback,
chunk_size, parallel_num
);
};
Qencode.prototype.get_metadata - async function(video_url, job_done_callback) {
var params = {
"query": {
"source": video_url,
"format": [{ "output": "metadata", "metadata_version": "4.1.5" }]
}
};
var token = this.options.token;
var create_task_response = await _create_task(this.options.endpoint + '/v1/create_task', token);
this.task_token = create_task_response.task_token;
this.upload_url = create_task_response.upload_url;
if (create_task_response.error) {
job_done_callback(create_task_response);
}
this._start_encode2(options.endpoint + '/v1/start_encode2',
task_token,
options
).then((start_encode_response) => {
_process_launch_job_callback(this.task_token, start_encode_response, job_done_callback);
});
}
Qencode.prototype.create_task = async function(job_done_callback) {
var token = this.options.token;
var create_task_response = await _create_task(this.options.endpoint + '/v1/create_task', token);
this.task_token = create_task_response.task_token;
this.upload_url = create_task_response.upload_url;
if (create_task_response.error) {
job_done_callback(create_task_response);
return false;
}
return true;
}
Qencode.prototype._launch_job = async function(launch_job_func, job_done_callback, upload_progress_callback, chunk_size, parallel_num) {
if (this.task_token == '' || this.task_token == null || this.task_token === undefined) {
var isTaskCreate = await this.create_task(job_done_callback);
if (isTaskCreate === false)
return;
}
if (this.options.file) {
var upload_url = this.upload_url + '/' + this.task_token;
var task_token = this.task_token;
var encode_options = this.options;
var chunk_size_file = chunk_size;
if (chunk_size_file <= 0) {
chunk_size_file = Math.round(this.options.file.size / 30);
var min_size = 200000;
var max_size = 104857600;
if (chunk_size_file < min_size) {
chunk_size_file = min_size;
} else {
if (chunk_size_file > max_size) {
chunk_size_file = max_size;
}
}
}
var upload_options = this._get_upload_options(task_token,
upload_url,
encode_options.file.name,
chunk_size_file,
parallel_num,
encode_options,
launch_job_func,
upload_progress_callback,
job_done_callback
);
_upload_file(encode_options.file, upload_options);
} else if (this.options.uri || this.options.stitch || this.options.query) {
launch_job_func(this.task_token, this.options, job_done_callback)
} else {
job_done_callback({ error: true, message: 'File or video url is required' });
}
};
Qencode.prototype._get_upload_options = function(task_token, upload_url,
filename,
chunk_size,
parallel_num,
encode_options,
launch_job_func,
upload_progress_callback,
job_done_callback) {
return {
endpoint: upload_url,
chunkSize: chunk_size,
parallelUploads: parallel_num,
retryDelays: [0, 1000, 3000, 5000],
metadata: { filename: filename },
onProgress: function(bytesUploaded, bytesTotal) {
var percentage = (bytesUploaded / bytesTotal * 100).toFixed(2);
var tus_upload_response = {
'percentage': percentage,
'task_token': task_token,
'filename': filename
};
if (upload_progress_callback)
upload_progress_callback(tus_upload_response);
},
onSuccess: function() {
if (tus_upload.length > 0) {
var tt_tus_upload = tus_upload.find(tt => tt.file.name == filename);
const index = tus_upload.indexOf(tt_tus_upload);
var url = tt_tus_upload.url.split("/");
if (index > -1) {
tus_upload.splice(index, 1);
}
var file_uuid = url[url.length - 1];
var uri = 'tus:' + file_uuid;
if ('query' in encode_options) {
encode_options.query.source = uri;
} else {
encode_options.uri = uri;
}
launch_job_func(task_token, encode_options, job_done_callback);
}
}
}
};
Qencode.prototype._start_encode_with_callback = function(task_token, options, callback) {
_start_encode(options.endpoint + '/v1/start_encode',
task_token,
options
).then((start_encode_response) => {
_process_launch_job_callback(task_token, start_encode_response, callback);
});
};
Qencode.prototype._start_encode2_with_callback = function(task_token, options, callback) {
_start_encode2(options.endpoint + '/v1/start_encode2',
task_token,
options
).then((start_encode_response) => {
_process_launch_job_callback(task_token, start_encode_response, callback);
});
};
function _process_launch_job_callback(task_token, start_encode_response, callback) {
if (start_encode_response.error) {
callback(start_encode_response);
return;
}
start_encode_response.task_token = task_token;
callback(start_encode_response);
}
Qencode.prototype.status = function(data, callback) {
if ((data.token == undefined || data.token == '' || data.token == null) ||
(data.url == undefined || data.url == '' || data.url == null)) {
callback(data.token, data.filename, { error: true, message: 'token and url is required' });
return;
}
var interval = this.options.interval >= 15 ? this.options.interval : 15;
_get_status(data.url, [data.token]).then((statuses) => {
callback(data.token, statuses, data.filename);
if (statuses.error) return;
var status_url = data.url;
var timer = setInterval(function() {
_get_status(status_url, [data.token]).then((statuses) => {
callback(data.token, statuses, data.filename);
if (statuses.error) clearInterval(timer);
var status = statuses.statuses[data.token];
if (status['status_url'])
status_url = status['status_url'];
if (status['status'] == "completed" || status['error'] == 1) clearInterval(timer);
});
}, interval * 1000);
this.timer = timer;
});
};
Qencode.prototype.clear = function() {
if (this.timer) {
clearInterval(this.timer);
}
this.task_token = undefined;
this.tus_url = undefined;
this.options = undefined;
}
return Qencode;
})();