首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用node.js将文件转换为二进制格式以发送到wit.ai api

无法使用node.js将文件转换为二进制格式以发送到wit.ai api
EN

Stack Overflow用户
提问于 2019-07-03 16:33:23
回答 1查看 223关注 0票数 0

我正面临着一个问题,在转换音频文件为二进制格式。我需要将它发送到Wit.AI应用程序接口,它需要该格式的数据。我正在使用node.js。在我的前端,我使用麦克风录音模块记录用户的声音。欢迎任何建议。

我的前端代码:

代码语言:javascript
复制
var recorder;
function startRecording() {
    recorder = new MicRecorder({
        bitRate: 128
    });
    recorder.start()
}

function stopRecording() {
    recorder.stop().getMp3().then(([buffer, blob]) => {
        console.log(buffer, blob);
        const file = new File(buffer, 'music.mp3', {
            type: blob.type,
            lastModified: Date.now()
        })
        console.log(file)
        axios({
            method: 'post',
            url: `${appUrl}/open_api/voice/send?data=${buffer}`
        }).then(function (res) {
            console.log(res)
            if (res.data.success) {
                console.log('done',res)
            } else {
                console.log(res.data)
            }
        })
    })
};

录制成功后,我想把文件发送给我的接口,以便调用wit.ai /speech接口。

我的后端代码是:

代码语言:javascript
复制
router.post('/voice/send',                                //chatbot response api
    async (req, res, next) => {
        let thread_id = '99-99-99-99'
        let audioBinary = req.query.data
        console.log(audioBinary)
        let appId = "5d07621d6b79be66a73f4005"
        let sessionId ="10-10-10-10"
        let accessToken = await db.model('ChatBotApp').findOne({
            _id: req.query.key
        }, {
            access_token: 1
        }).lean() 
        var options = {

            method: 'POST',
            uri: 'https://api.wit.ai/speech?v=20190513',
            body : audioBinary,
            encoding: null,
            headers: {
                'Authorization': 'Bearer ' + "HY3ZWSUGPBPD5LWZLRSZ3QJCDC27M6EW",
                'Content-Type': 'audio/mpeg',
            },
            // json: true // Automatically stringifies the body to JSON
        };
        rp(options)
        .then(async function (parsedBody) {
            console.log('this called',parsedBody)
            return
            // let response = await firstEntityValue(parsedBody, appId, message, thread_id)
            // events.emit('Chats', appId, thread_id, message, sessionId, response);
            return res.apiOk(response)
        })
        .catch(function (err) {
            console.log(err)
            return res.apiError('Issue while creating app!', err);
        })


    }
)
EN

回答 1

Stack Overflow用户

发布于 2019-07-04 19:22:11

代码语言:javascript
复制
 var recorder

   function startRecording() {
     recorder = new MicRecorder({
       bitRate: 128
     });
     recorder.start()
   }

   function stopRecording() {
     recorder.stop().getMp3().then(([buffer, blob]) => {
       console.log(buffer, blob);

       const file = new File(buffer, 'music.mp3', {
         type: blob.type,
         lastModified: Date.now()
       })
       var bodyFormData = new FormData();
       bodyFormData.append('file', file);
       console.log(file)

       axios({
         method: 'post',
         url: `${appUrl}/open_api/voice/send`,
         headers: {
           'Content-Type': 'multipart/form-data'
         },
         data: bodyFormData
       }).then(function (res) {
         console.log(res)
         if (res.data.success) {
           console.log('done', res)
         } else {
           console.log(res.data)
         }
       })
     })
   };


API 
router.post('/voice/send',upload.single('file'), //chatbot response api
        async (req, res, next) => {

            console.log(req.file)
            let thread_id = '99-99-99-99'
            let audioBinary = req.file.buffer
            let appId = "5d07621d6b79be66a73f4005"
            let sessionId = "10-10-10-10"
            let accessToken = await db.model('ChatBotApp').findOne({
                _id: req.query.key
            }, {
                access_token: 1
            }).lean()
            var options = {

                method: 'POST',
                uri: 'https://api.wit.ai/speech?v=20190513',
                headers: {
                    'Authorization': 'Bearer ' + "HY3ZWSUGPBPD5LWZLRSZ3QJCDC27M6EW",
                    'Content-Type': 'audio/mpeg',
                },
                body: audioBinary

                // json: true // Automatically stringifies the body to JSON
            };
            rp(options)
                .then(async function (parsedBody) {
                    console.log('this called', parsedBody)
                    return
                    // let response = await firstEntityValue(parsedBody, appId, message, thread_id)
                    // events.emit('Chats', appId, thread_id, message, sessionId, response);
                    return res.apiOk(response)
                })
                .catch(function (err) {
                    console.log(err)
                    return res.apiError('Issue while creating app!', err);
                })
        })
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56866023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档