I have a binaries storage bucket (in Firebase Storage) and I am implementing a node.js Cloud Function which gets an audio (audio/m4a or audio/caf), verifies it format and duration, and then associate its uri with a document in my database (or deletes the audio file if its duration is not valid).
Before creating the document in my database, I need to validate the audio file.
For downloading the files from the storage I am using this function:
exports.downloadBinaryFileFromUrl = function (fileUrl) {
/* This function downloads a binary-encoded file from its URL and returns it */
return axios
.get(fileUrl, {
responseType: "arraybuffer",
})
.then((res) => Buffer.from(res.data, "binary"))
.catch((err) => {
if (err.response) {
/*
The request was made and the server responded with a status code
that falls out of the range of 2xx
*/
throw err.response.data;
} else if (err.request) {
// Client never received a response, or request never left
throw err.request;
} else {
// Something happened in setting up the request that triggered an Error
throw new Error(`Error: ${err.message}`);
}
});
};
Any ideas how to get the audio file's duration using Node.js?