Convert .m4a to .wav

只愿长相守 提交于 2020-01-25 07:06:10

问题


I need to convert buffer audio file .m4a to buffer audio file .wav for send to google speech api by NodeJS

var toWav = require('audiobuffer-to-wav')
var xhr = require('xhr')
var context = new AudioContext()

// request the MP3 as binary 
xhr({
  uri: 'audio/track.mp3',
  responseType: 'arraybuffer'
}, function (err, body, resp) {
  if (err) throw err
  // decode the MP3 into an AudioBuffer 
  audioContext.decodeAudioData(resp, function (buffer) {
    // encode AudioBuffer to WAV 
    var wav = toWav(buffer)

    // do something with the WAV ArrayBuffer ... 
  })
})

I got error

AudioContext is not defined

回答1:


If you need to use this library server side, you can emulate AudioContext functionality with web-audio-api package.

const fs = require('fs');
const toWav = require('audiobuffer-to-wav');
const AudioContext = require('web-audio-api').AudioContext;
const audioContext = new AudioContext;

let resp = fs.readFileSync('sample.m4a');

audioContext.decodeAudioData(resp, buffer => {
  let wav = toWav(buffer);
  // do something with the WAV ArrayBuffer ...
});



回答2:


using ffmpeg

(to install ffmpeg on mac brew install ffmpeg)

var ffmpeg = require('fluent-ffmpeg');

function convertFileFormat(file, destination, error, progressing, finish) {

ffmpeg(file)
    .on('error', (err) => {
        console.log('An error occurred: ' + err.message);
        if (error) {
            error(err.message);
        }
    })
    .on('progress', (progress) => {
        // console.log(JSON.stringify(progress));
        console.log('Processing: ' + progress.targetSize + ' KB converted');
        if (progressing) {
            progressing(progress.targetSize);
        }
    })
    .on('end', () => {
        console.log('converting format finished !');
        if (finish) {
            finish();
        }
    })
    .save(destination);

    }

    convertFileFormat('file.m4a', 'file.wav', function (errorMessage) {

        }, null, function () {
            console.log("success");
        });



回答3:


ref: https://askubuntu.com/questions/65331/how-to-convert-a-m4a-sound-file-to-mp3

sudo apt-get install libav-tools

avconv -i input.m4a ouptut.wav

It works for me!



来源:https://stackoverflow.com/questions/46389843/convert-m4a-to-wav

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!