Create audio file, from URL

I need the messenger audio file, to use in the Google Cloud Speech API. When an audio is received in messenger, a url is generated that downloads this file in .mp4. How can I retrieve the audio from this url, and use it in the Google API?

Author: Thiago R., 2016-10-07

1 answers

The trick here is to take streams, with the http module (or equivalent) to download the audio file and then pipe to a stream, and then do whatever you want.. In this example, save to a file:

var http = require('http');
var fs = require('fs');

var audioUrl = "http://www.exemplo/audio.mp4",
    fileName = "audio.mp4";

var file = fs.createWriteStream(fileName);
var request = http.get(audioUrl, function(response) {
  response.pipe(file);
});
 1
Author: Roberto14, 2016-10-21 07:42:12