我想向 facebook messenger 发送语音片段,并使用 Google Cloud Speech to text 将其翻译成文本。但是,facebook messenger 格式将 url 设置为文件,我不知道将其转换为 base 64。如果它是 .png 和 .jpg 之类的图像,那很好,有相应的软件包。但我正在尝试转换像 .mp3 和 .m4a 文件这样的音频。是否有任何工具可以与 nodejs 一起使用。
我不希望将音频文件保存为本地文件,因为我正在使用 Google App Engine 部署服务器,这样做只会让事情变得 super 复杂。
最佳答案
我从 here 得到了我需要的东西.虽然问题是关于图像的,但代码似乎运行良好。
const getBase64 = async (url) => {
try {
var result = await axios
.get(url, {
responseType: 'arraybuffer'
})
.then(response => new Buffer.from(response.data, 'binary').toString('base64'))
return { data: result}
}catch (e) {
return {error: e};
}
}
关于javascript - 在nodejs中将音频从url转换为base64?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56466573/