lexica-dialog-core
Version:
Core server for Lexica dialog agent.
63 lines • 2.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const AWS = require("aws-sdk");
const lodash_1 = require("lodash");
class ElasticTranscoderService {
constructor(config) {
this.config = config;
this.transcoder = new AWS.ElasticTranscoder({
apiVersion: config.apiVersion,
});
}
transcodeAudio(source) {
return this.transcode(source, this.config.audio);
}
transcodeVideo(source) {
return this.transcode(source, this.config.video);
}
async transcode(source, typeConfig) {
const preset = typeConfig.preset;
const file = {
contentType: preset.contentType,
path: `${source.path}${preset.suffix}`,
};
const params = {
Input: {
Key: source.path,
},
Outputs: [
{
Key: file.path,
PresetId: preset.id,
},
],
PipelineId: typeConfig.pipelineId,
};
const data = await this.transcoder.createJob(params).promise();
if (!lodash_1.isNil(data.Job) && !lodash_1.isNil(data.Job.Id)) {
const jobId = data.Job.Id;
let attempts = 0;
while (attempts < this.config.maxAttempts) {
const jobResp = await this.transcoder.readJob({ Id: jobId }).promise();
if (!lodash_1.isNil(jobResp.Job)) {
if (jobResp.Job.Status === 'Complete') {
return file;
}
if (jobResp.Job.Status === 'Canceled') {
throw new Error(`Transcoder job canceled. ID: ${jobId}`);
}
if (jobResp.Job.Status === 'Error') {
throw new Error(`Transcoder job occur error. ID: ${jobId}`);
}
}
attempts += 1;
await new Promise(resolve => setTimeout(resolve, this.config.delay));
}
throw new Error(`Transcoder job over max attempts. ID: ${jobId}`);
}
throw new Error('AWS transcoder return empty Job or Job ID');
}
}
exports.ElasticTranscoderService = ElasticTranscoderService;
exports.default = ElasticTranscoderService;
//# sourceMappingURL=ElasticTranscoderService.js.map