Yatender Singh
Yatender Singh

Reputation: 3322

How to change mp3 file to wav file in node.js

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.

Upvotes: 13

Views: 12459

Answers (2)

JESUS rose to life
JESUS rose to life

Reputation: 29

i am using modules with import not require

also i wanted to use Promises

the code below is based on this answer : https://stackoverflow.com/a/40233702/25584927

here are the steps i took :

  1. run npm i ffmpeg-fluent

  2. download ffmpeg : https://ffmpeg.org/download.html

  3. unzip the download

  4. move the unzipped contents to "C:\Program Files\ffmpeg"

  5. add "C:\Program Files\ffmpeg\bin" to the system path

  6. restart vs code

  7. 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);
  });
}

Upvotes: 0

Yatender Singh
Yatender Singh

Reputation: 3322

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.

Upvotes: 22

Related Questions