import { promises as fs } from "fs";
import path from "path";
import ffmpeg from "fluent-ffmpeg";
import ffmpegInstaller from "@ffmpeg-installer/ffmpeg";
ffmpeg.setFfmpegPath(ffmpegInstaller.path);
export default defineComponent({
async run({ steps, $ }) {
const mimeType = steps.mime_type.$return_value.mimeType;
const filePath = steps.mime_type.$return_value.filePath;
const fileName = steps.mime_type.$return_value.fileName;
// Define the path for the new WAV file in the temp folder
const tempFolder = "/tmp";
const newFileName = path.basename(fileName, path.extname(fileName)) + ".wav";
const newFilePath = path.join(tempFolder, newFileName);
// Check if the MIME type is already audio/wav
if (mimeType === "audio/wav") {
$.export("newFilePath", filePath);
console.log("File is already in WAV format. No conversion needed.");
return {
newFilePath: filePath
};
}
try {
// Convert the audio file to WAV using ffmpeg
await new Promise((resolve, reject) => {
ffmpeg(filePath)
.output(newFilePath)
.on("end", () => {
console.log(`File converted successfully: ${newFilePath}`);
resolve();
})
.on("error", (err) => {
console.error(`Error converting file: ${err}`);
reject(err);
})
.run();
});
// Ensure the new file exists before proceeding
await fs.access(newFilePath);
$.export("newFilePath", newFilePath);
$.export("newFileName", newFileName)
return {
newFilePath,
newFileName,
};
} catch (error) {
console.error(`Error converting file: ${error}`);
throw new Error(`Error converting file: ${error}`);
}
}
});