Gallery
a list for 'against comparison'
Share
Explore
Pipedream

FFMPEG - Convert to WAV

A forgiving node script that converts pretty much any audio file into a *.WAV, as it includes a check that the file is not already a WAV- this check is expecting confirmation of the mime-type being avalible from a previous step, however. By default, the WAV renders at 44.1 PCM - however, you can go deep with FFMPEG and I highly recommend the for exploring and learning about these variables.
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}`);
}
}
});

Share
 
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.