我正在尝试将wit ai语音端点与node.js一起使用,但我收到空响应
{
"entities": {},
"intents": [],
"text": "",
"traits": {}
}
当我使用计算机上录制的音频时,我可以获取文本。但是,当我使用浏览器上录制的音频时,或者在本例中,通过 wpp connect api(用于接收和发送 Whatsapp 消息的 API)获得的音频时,返回为空。
我检查了音频,里面有内容。
这是我的代码(但错误也发生在邮递员中)
await message.downloadMedia().then(data => {
const binaryData = Buffer.from(data.data, 'base64')
axios
.post('https://api.wit.ai/speech?v=20230215', binaryData, {
headers: {
Authorization: `Bearer ${TOKEN}`,
'Content-Type': 'audio/ogg'
},
//data: dadosDoArquivo,
responseType: 'json'
})
.then(response => {
console.log(response.data) // Resposta da API
console.log(response.status)
const speechResponse = SpeechResponse.create(response.data)
const chatMessageProps = {
message: speechResponse.text,
type: message.type,
chatUser: chatUser
}
return ChatMessageInput.create(chatMessageProps)
})
.catch(error => {
console.error('Erro na requisição:', error)
})
})
我还尝试使用
fs.createReadStream(`audio7.ogg`)
在本地读写文件
我发现问题出在音频编解码器上。 要使其工作(至少使用 ogg 格式),需要使用“Vorbis”编解码器。
我创建了一个转换编解码器的函数:
convertToVorbis(文件名, 回调) { 常量输入文件 =
${filename}.ogg
常量输出文件 = ${filename}_converted.ogg
ffmpeg(输入文件)
.audioCodec('libvorbis')
.on('结束', 函数 () {
console.log('Conversão Finalizada.')
if (回调) 回调(null, 输出文件)
})
.on('错误', 函数 (err) {
console.error('错误 ao 转换器:', err)
如果(回调)回调(错误)
})
.save(输出文件)
}
重要:您需要安装 ffmpeg,因为它在终端上运行。