projeto-hit/backend/src/helpers/ConvertAudio.ts

49 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-09-08 19:50:51 +00:00
import ffmpeg from "fluent-ffmpeg";
import util from "util";
import { exec as execCallback } from "child_process";
const exec = util.promisify(execCallback);
async function convertAudioToOgg(
inputFile: string,
outputFile: string
): Promise<void> {
try {
const command = `ffmpeg -i ${inputFile} -c:a libopus ${outputFile}.ogg && rm ${inputFile}`;
const { stdout, stderr } = await exec(command);
console.log("Conversion finished");
console.log("stdout:", stdout);
console.error("stderr:", stderr);
} catch (error) {
console.error("Error:", error);
throw error;
}
}
function convertAudioToWav(
inputFile: string,
outputFile: string
): Promise<void> {
return new Promise<void>((resolve, reject) => {
const command = ffmpeg(inputFile)
.audioCodec("pcm_s16le") // Set the audio codec to libvorbis (OGG)
.format("wav") // Set the output format to OGG
.on("end", () => {
console.log("Conversion finished");
resolve(); // Resolve the promise when the conversion is successful
})
.on("error", (err: any) => {
console.error("Error:", err);
reject(err); // Reject the promise if there is an error
});
// Save the output to the specified file
command.save(outputFile);
});
}
export { convertAudioToWav, convertAudioToOgg };