-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
49 lines (41 loc) · 1.3 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
'use strict';
const ffmpeg = require('fluent-ffmpeg');
const ffmpeg_static = require('ffmpeg-static');
const mime = require('mime');
const fs = require('fs');
async function linear16(filePathIn, filePathOut) {
if (('object' === typeof filePathIn) && !filePathOut) {
const {inPath, outPath} = filePathIn;
filePathIn = inPath;
filePathOut = outPath;
}
if (!filePathIn || !filePathOut) {
throw new Error('You must specify a path for both input and output files.');
}
if (!fs.existsSync(filePathIn)) {
throw new Error('Input file must exist.');
}
if (mime.getType(filePathIn).indexOf('audio') <= -1) {
throw new Error('File must have audio mime.');
}
return new Promise((resolve, reject) => {
try {
ffmpeg()
.setFfmpegPath(ffmpeg_static.path)
.input(filePathIn)
.outputOptions([
'-f s16le',
'-acodec pcm_s16le',
'-vn',
'-ac 1',
'-ar 16k',
'-map_metadata -1'
])
.save(filePathOut)
.on('end', () => resolve(filePathOut));
} catch (e) {
reject(e);
}
});
}
module.exports = linear16;