-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconcat.ts
45 lines (38 loc) · 1.3 KB
/
concat.ts
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
import fs from "fs";
import path from "path";
import ffmpeg from "fluent-ffmpeg";
export default function concatenateAudioFiles() {
const directoryPath = "./public/voice/";
const silenceAudioFile = "./public/silence.mp3";
if (!fs.existsSync("tmp/")) {
fs.mkdirSync("tmp/");
}
const files = fs
.readdirSync(directoryPath)
.filter((file) => file.endsWith(".mp3") && !file.startsWith("silence"));
files.sort((a, b) => {
const numberA = parseInt(a.split("-")[1], 10);
const numberB = parseInt(b.split("-")[1], 10);
return numberA - numberB;
});
const command = ffmpeg();
files.forEach((file, index) => {
console.log("Adding file to ffmpeg:", file);
command.input(path.join(directoryPath, file));
if (index < files.length - 1) {
console.log("Adding silence to ffmpeg");
command.input(silenceAudioFile);
}
});
command
.on("start", (commandLine) => {
console.log("Spawned Ffmpeg with command:", commandLine);
})
.on("error", (err) => {
console.log("Error:", err.message);
})
.on("end", () => {
console.log("Finished concatenating audio files!");
})
.mergeToFile(`./public/audio.mp3`, "tmp/");
}