NodeJS 带有空返回的智能AI语音请求

vd2z7a6w  于 5个月前  发布在  Node.js
关注(0)|答案(1)|浏览(58)

我试图使用智慧人工智能语音端点与node.js,但我收到一个空的响应

{
    "entities": {},
    "intents": [],
    "text": "",
    "traits": {}
}

字符串
当我使用在我的计算机上录制的音频时,我可以获取文本。但是当我使用在浏览器上录制的音频时,或者在这种情况下,通过wpp connect API(用于接收和发送whatsapp消息的API)获取,返回为空。
我检查了音频,它有内容。
这是我的代码(但错误也发生在 Postman )

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)在本地读写文件

t5zmwmid

t5zmwmid1#

我发现问题出在音频编解码器上。要让它工作(至少是ogg格式),需要使用“Vorbis”编解码器。
我添加了一个转换编解码器的函数:
convertToVorbis(filename,callback){ const inputFile = ${filename}.ogg const outputFile = ${filename}_converted.ogg ffmpeg(inputFile).audioCodec(“libvorbis”).on(“end”,function(){ console.log(“Conversão finalizada.”)if(callback)callback(null,outputFile)}).on(“error”,function(err){ console.error(“Erro ao converter:”,err)if(callback)callback(err)}).保存(outputFile)}

**重要提示:**需要安装ffmpeg,因为它在终端上运行。

相关问题