-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfluent.js
36 lines (32 loc) · 1.02 KB
/
fluent.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
//@ts-check
const ffmpeg = require("fluent-ffmpeg");
const fs = require("fs");
const path = `${__dirname}/audio`;
/**
* Merges mp3 audio files
* @param {*} fileCount the number of files to be merged
* @param {*} pathOfOutputFile the path of the output file
*/
function mergeAudioFiles(fileCount, pathOfOutputFile, timing) {
return new Promise((resolve, reject) => {
let files = [];
for (let i = 0; i < fileCount; i++) {
files.push(`audio/${i}.mp3`);
}
files
.reduce((prev, curr) => prev.input(curr), ffmpeg())
.on("error", (err) => {
console.log("an error happened: " + err.message);
reject(err);
})
.on("end", () => {
console.log("files have been merged succesfully");
fs.readdirSync(path).forEach((f) => fs.rmSync(`${path}/${f}`));
timing.end();
console.log(`Finished merging file in ${timing.toHumanReadableTime()}`);
resolve(true);
})
.mergeToFile(pathOfOutputFile);
});
}
exports.mergeAudioFiles = mergeAudioFiles;