I am trying to convert mp3 file to wav file but I am not getting idea how to do that, I tried using fluent-ffmpeg library but I don't know how to use that.
How to change mp3 file to wav file in node.js
Asked Answered
I finally figured it out using 'fluent-ffmpeg' library. Here is my code.
const ffmpeg = require('fluent-ffmpeg');
let track = './source.mp3';//your path to source file
ffmpeg(track)
.toFormat('wav')
.on('error', (err) => {
console.log('An error occurred: ' + err.message);
})
.on('progress', (progress) => {
// console.log(JSON.stringify(progress));
console.log('Processing: ' + progress.targetSize + ' KB converted');
})
.on('end', () => {
console.log('Processing finished !');
})
.save('./hello.wav');//path where you want to save your file
if you are facing
An error occurred: Cannot find ffmpeg
then add the ffmpeg
path in system environment variables. Your VSCode still may dont recognise the ffmpeg command so in that case re-start VSCode.
No error with just
var ffmpeg = require('fluent-ffmpeg');
. Only erroring when I use it. –
Cowart make sure ffmpeg is installed in system and executable from everywhere and second thing is make sure that 'fluent-ffmpeg' npm package is also installed in project. –
Jejunum
Both are reinstalled. I added the path in System Environment Variables also (for ffmpeg). Now I'm getting error : ` ENOENTspawn C:\ffmpeg\bin\ffmpeg.exe at exports._errnoException (util.js:1022:11) at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32) at onErrorNT (internal/child_process.js:359:16)` –
Cowart
ok do one thing just go to console and try to execute some ffmpeg command. Whatever you r telling it should do the work or if can you please show me your source then I can resolve issue. –
Jejunum
github.com/fluent-ffmpeg/node-fluent-ffmpeg please refer this link to set properly fluent-ffmpeg library –
Jejunum
ffmpeg works from the command .. The issue was
.setFfmpegPath("C:\\ffmpeg\\bin\\ffmpeg.exe")
Now I am able to convert the file. I am trying to figure out .. how to convert the uploaded file from a form. Can you help please? How to access the file from Multer? –
Cowart Download that file locally somewhere and access it directly it's simple. –
Jejunum
Do you want me to give sample code? let me know .. just use request module and download at some destination. –
Jejunum
I'll be very grateful if you could give a sample code. I've posted the question #43228100". Please have a look .. I'm very confused as to how to integrate
ffmpeg
in multer-s3
module. Thanks. –
Cowart Im able to upload the file with
multer
and convert it with ffmpeg
. That part is working. But its only multer
and not multer-s3
. How can I upload the converted .mp4
to S3
? Please help. –
Cowart give me ur email i'll forward you the code to upload to s3 –
Jejunum
I replied just check :) –
Jejunum
so ffmpeg should be insatlled on the node server!!! is there any other way to solve this?? any library that works without installed extra executable on server? –
Ruckman
i am using modules with import
not require
also i wanted to use Promise
s
the code below is based on this answer : https://mcmap.net/q/860945/-how-to-change-mp3-file-to-wav-file-in-node-js
here are the steps i took :
run
npm i ffmpeg-fluent
download ffmpeg : https://ffmpeg.org/download.html
unzip the download
move the unzipped contents to "C:\Program Files\ffmpeg"
add "C:\Program Files\ffmpeg\bin" to the system path
restart vs code
here was the code :
import ffmpeg from "fluent-ffmpeg";
export async function audio_to_wav(path_input, path_output) {
await new Promise(async (resolve, reject) => {
ffmpeg(path_input)
.toFormat("wav")
.on("error", (err) => {
reject(err);
})
.on("end", () => {
resolve();
})
.save(path_output);
});
}
© 2022 - 2024 — McMap. All rights reserved.
An error occurred: Cannot find ffmpeg
. – Cowart