将.m4a转换为.wav

时间:2017-09-24 12:05:16

标签: node.js buffer google-speech-api

我需要将缓冲区音频文件.m4a转换为缓冲音频文件.wav,以便通过NodeJS发送到谷歌语音API

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

我收到了错误

AudioContext is not defined

3 个答案:

答案 0 :(得分:0)

如果您需要使用此库服务器端,则可以使用web-audio-api包模拟AudioContext功能。

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

答案 1 :(得分:0)

使用ffmpeg

(要在Mac brew install ffmpeg上安装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");
        });

答案 2 :(得分:-1)

参考: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

对我有用!