My code works only once correctly and starts to work correctly only after I restart my program. I’m trying to get two audio files to help Node.js with FFMPEG. Є be-yakі ideї why tse mozhe trapletisyas? My code is shown below.
async songMerge(bg_id, text) {
const bgMusic = await this.airtable
.table("BackgroundMusic")
.select({ view: "View", filterByFormula: `ID = '${bg_id}'` })
.firstPage();
if (!bgMusic.length) throw new Error("is not found");
const result = await fetch(
`https://api.elevenlabs.io/v1/text-to-speech/VR6AewLTigWG4xSOukaG`,
{
method: "POST",
headers: {
accept: "audio/mpeg",
"xi-api-key": "token",
"Content-Type": "application/json",
},
body: JSON.stringify({
text,
voice_settings: {
stability: 0,
similarity_boost: 0,
},
}),
}
);
const audioBuffer = await result.arrayBuffer();
const filePath = path.join(__dirname, "../../../", "audio.mp3");
await fs.writeFileSync(filePath, Buffer.from(audioBuffer));
const test = await fetch(`${bgMusic[0].fields.AudioLink}`);
const audioBufferr = await test.arrayBuffer();
const filePathh = path.join(__dirname, "../../../", "test.mp3");
await fs.writeFileSync(filePathh, Buffer.from(audioBufferr));
await mergeAudio(filePathh, filePath);
}
and the mergeAudio function:
async function mergeAudio(bgMusic, path) {
return await command
.addInput(path)
.addInput(bgMusic)
.complexFilter([
{
filter: "amix",
inputs: ["0:0", "1:0"],
},
])
.outputOptions("-y") // add this line to overwrite the output file
.output(`./overlayed.mp3`)
.setStartTime("00:00:00")
.setDuration("15")
.on("error", function (err) {
console.log(err);
})
.on("end", function () {
return "ok";
})
.run();
}
enter code here