This repository was 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
/
Copy pathffmpeg.js
48 lines (40 loc) · 1.49 KB
/
ffmpeg.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
process.env['FFMPEG_PATH'] = '/tmp/ffmpeg';
process.env['PATH'] = `${process.env['PATH']}:/tmp/:${process.env['LAMBDA_TASK_ROOT']}`;
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const readFile = util.promisify(require('fs').readFile);
const path = require('./path.js');
const moment = require('./moment.js');
function createTimeStamp(ms) {
let dur = moment.duration(ms);
return `${
dur.hours() > 9 ? dur.hours() : '0' + dur.hours()
}:${
dur.minutes() > 9 ? dur.minutes() : '0' + dur.minutes()
}:${
dur.seconds() > 9 ? dur.seconds() : '0' + dur.seconds()
}`;
}
function getTrimParameter({trimEnabled = false, trimFrom, trimTo}) {
if (!trimEnabled) {
return '';
}
return `-ss ${createTimeStamp(trimFrom*1000)} -to ${createTimeStamp(trimTo*1000)} -c copy`;
}
module.exports = {
setup: async () => {
console.log("cp ffmpeg");
return exec(`cp /var/task/bin/ffmpeg /tmp/.; chmod 755 /tmp/ffmpeg;`)
},
//todo: trim
transcode: async ({codec, extension, trimEnabled = false, trimFrom=0, trimTo=0} = {}) => {
console.log('transcode file...');
await exec(`/tmp/ffmpeg -y -i ${path.in} ${getTrimParameter({
trimEnabled,
trimFrom,
trimTo
})} -acodec ${codec} -mode mono -ac 1 ${path.out}${extension}`);
console.log('... and read outgoing file');
return readFile(`${path.out}${extension}`);
}
};