This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (71 loc) · 2.13 KB
/
index.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
const s3Api = require('./s3Api.js');
const path = require('./path.js');
const file = require('./file.js');
const ffmpeg = require('./ffmpeg.js');
const ffprobe = require('./ffprobe.js');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
exports.handler = async (event) => {
console.log(`REQUEST: ${JSON.stringify(event)}`);
let {Records: [{Sns: {Message}}]} = event;
let {
"in": {
bucket: inBucket,
key: inKey
},
out: {
bucket: outBucket,
key: outKey,
codec: outCodec,
trimEnabled = false,
trimFrom = 0,
trimTo = 180
}
} = JSON.parse(Message);
console.log(`Create base folder ${path.base}`);
await exec(`mkdir -p ${path.base}`);
let Metadata;
let incomingFile = s3Api.getObject({
Bucket: inBucket,
Key: inKey
}).then(e => {
Metadata = e.Metadata;
return e;
}).then(e => e.Body);
await Promise.all([
file.setup(incomingFile),
ffmpeg.setup(),
ffprobe.setup()
]);
console.log('start transcoding');
let Body = await ffmpeg.transcode({...getCodecInfo(outCodec), ...{trimEnabled, trimFrom, trimTo}});
let report = await ffprobe.report(getCodecInfo(outCodec));
console.log(`report: ${report}`);
let {streams: [{sample_rate}]} = report;
console.log(`detected sample rate: ${JSON.stringify(sample_rate)}`);
console.log('put transcoded file so s3');
return s3Api.putObject({
Bucket: outBucket,
Key: outKey,
Body,
Metadata: {
...Metadata, ...{"sample-rate": sample_rate}
}
});
};
const getCodecInfo = (codec) => {
switch (codec) {
case "opus":
return {
codec: "libopus",
extension: ".opus"
};
case "flac":
return {
codec: "flac",
extension: ".flac"
};
default:
throw "unknown codec"
}
};