Repository navigation
Expand file tree
/
Copy pathprocessAudio.js
More file actions
62 lines (58 loc) · 1.78 KB
/
Copy pathprocessAudio.js
File metadata and controls
62 lines (58 loc) · 1.78 KB
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
const async = require("async");
const { exec } = require("child_process");
const fs = require("fs");
const audioProcessor = require("./audio_processor");
function processAudio({ filePath, outputFormat }, callback = () => {}) {
const processingStepsFunc = [
(cb) => {
const fileExtension = filePath.split(".").pop().toLowerCase();
if (fileExtension === outputFormat) return cb(null, filePath);
console.log("converint to " + outputFormat);
audioProcessor.convertExtension(
filePath,
outputFormat,
(err, outputPath) => {
if (err) {
console.log(err);
return cb(null, filePath);
}
fs.unlink(filePath, () => {});
return cb(null, outputPath);
}
);
},
(filePath, cb2) => {
console.log("processing", filePath);
audioProcessor.clearBackgroundNoise(filePath, (err, outputPath) => {
fs.unlink(filePath, () => {});
console.log("after clear", err);
if (err) {
return cb2(err);
}
console.log("output path", outputPath);
return cb2(null, outputPath);
});
},
];
async.waterfall(processingStepsFunc, (err, finalFilePath) => {
console.log("Processed succesfully", err, filePath);
if (err || !fs.existsSync(finalFilePath)) return callback(err);
return callback(null, finalFilePath);
});
}
function speedAudio(audioPath, atempoRatio, callback = () => {}) {
const targetPath = `speeded_audio_${Date.now()}.${audioPath
.split(".")
.pop()}`;
exec(
`ffmpeg -i ${audioPath} -filter:a "atempo=${atempoRatio}" -vn ${targetPath}`,
(err) => {
if (err) return callback(err);
return callback(null, targetPath);
}
);
}
module.exports = {
processAudio,
speedAudio,
};