安裝百度語音sdk,從官網(wǎng)下載或者使用npm安裝
npm install baidu-aip-sdk
在開發(fā)中要需要幾個(gè)配置文件,在登陸百度云后點(diǎn)擊->百度語音 - 應(yīng)用列表-創(chuàng)建應(yīng)用,填報(bào)好相應(yīng)內(nèi)容后得到后續(xù)配置所需的如下內(nèi)容
// 設(shè)置APPID/AK/SK
var APP_ID = "你的 App ID";
var API_KEY = "你的 Api Key";
var SECRET_KEY = "你的 Secret Key";
1、獲取token
第一步請(qǐng)求在觀察httpClient文件中request 通過ID key 密鑰返回生成的token志衣,并返回轉(zhuǎn)嗎所需client;
const AipSpeechClient = require("baidu-aip-sdk").speech;
const fs = require('fs');
const path = require("path");
// 設(shè)置APPID/AK/SK
var APP_ID = "你的 App ID";
var API_KEY = "你的 Api Key";
var SECRET_KEY = "你的 Secret Key";
// 新建一個(gè)對(duì)象猛们,建議只保存一個(gè)對(duì)象調(diào)用服務(wù)接口
var client = new AipSpeechClient(APP_ID, API_KEY, SECRET_KEY);
var HttpClient = require("baidu-aip-sdk").HttpClient;
// 設(shè)置request庫(kù)的一些參數(shù)念脯,例如代理服務(wù)地址,超時(shí)時(shí)間等
// request參數(shù)請(qǐng)參考 https://github.com/request/request#requestoptions-callback
HttpClient.setRequestOptions({timeout: 5000});
// 也可以設(shè)置攔截每次請(qǐng)求(設(shè)置攔截后弯淘,調(diào)用的setRequestOptions設(shè)置的參數(shù)將不生效),
// 可以按需修改request參數(shù)(無論是否修改绿店,必須返回函數(shù)調(diào)用參數(shù))
// request參數(shù)請(qǐng)參考 https://github.com/request/request#requestoptions-callback
HttpClient.setRequestInterceptor(function(requestOptions) {
// 查看參數(shù)
console.log(requestOptions)
// 修改參數(shù)
requestOptions.timeout = 5000;
// 返回參數(shù)
return requestOptions;
});
2、獲取本地錄音并轉(zhuǎn)文字
轉(zhuǎn)換需要獲取語音轉(zhuǎn)buffer庐橙,client.recognize參數(shù)為
1惯吕、語音buffer
2惕它、語音類型,string (強(qiáng)類型)
3废登、采樣率 , int (強(qiáng)類型)
4、 {
dev_pid:"1537" , //string 語言類型普通話或簡(jiǎn)單英語郁惜,參看api
cuid:"string" , //string 用戶id堡距,隨意設(shè)置字符串
}
let voice = fs.readFileSync(path.resolve(__dirname,'./Playtemp_69427412-730c-4593-8e2f-acee8a01d33d.wav'));
let voiceBuffer = new Buffer(voice);
// 識(shí)別本地文件,附帶參數(shù)
client.recognize(voiceBuffer, 'wav', 8000, {dev_pid: '1537', cuid:"15001307"}).then(function (result) {
console.log('<recognize>: ' + JSON.stringify(result));
// console.log(result);
}, function(err) {
console.log(err);
});
https請(qǐng)求及源碼跟蹤
最終發(fā)送請(qǐng)求文件為node_modules/baidu-aip-sdk/src/http/httpClient.js兆蕉,
源碼為
req(options) {
// 首先處理設(shè)置INTERCEPTOR的情況
if (objectTools.isFunction(HttpClient.REQUEST_INTERCEPTOR)) {
options = HttpClient.REQUEST_INTERCEPTOR(options);
// 其次設(shè)置全局request options的
} else if (objectTools.isObject(HttpClient.REQUEST_GLOBAL_OPTIONS)) {
options = objectTools.merge(HttpClient.REQUEST_GLOBAL_OPTIONS, options);
}
return new Promise(function(resolve, reject) {
request(options, function(error, response, body) {
if (error === null) {
try {
resolve(JSON.parse(body));
} catch (e) {
// 無法解析json請(qǐng)求羽戒,就返回原始body
resolve(body);
}
} else {
reject(error);
}
});
});
}
轉(zhuǎn)換共發(fā)送請(qǐng)求兩次
1、 發(fā)送三個(gè)key 返回token
//發(fā)送
request({
form:{
client_id:"你的 API_KEY",
client_secret:"你的 SECRET_KEY",
grant_type:"client_credentials"
},
method:"post",
url:"https://aip.baidubce.com/oauth/2.0/token"
})
返回內(nèi)容:重點(diǎn)是需要access_token
{"refresh_token":"25.ac58af3ae3860f61431e3b3009446641.315360000.1860652752.282335-15001307","expires_in":2592000,"session_key":"9mzdCuKObdoovJYJ4qS9w1jJxRXbTfAZgGc4OEGsBRr9UWnGic5Zy70IjCCvH7ebIaHW1kQRfIWKP8fpSGmBlZ4F4bmzTQ==","access_token":"24.1f5c1dfebfae9768b31f6c39156e2466.2592000.1547884752.282335-15001307","scope":"audio_voice_assistant_get audio_tts_post public brain_all_scope wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\u6743\u9650 vis-classify_flower lpq_\u5f00\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify","session_secret":"cced641eda376083be3566f33e2c1f43"}
2虎韵、二次請(qǐng)求
二次請(qǐng)求易稠,應(yīng)該帶第一次請(qǐng)求到的access_token ,但是在nodejs的sdk中的二次請(qǐng)求數(shù)據(jù)中并未跟蹤到包蓝,只有如下請(qǐng)求驶社。
request({
body:{
"dev_pid":1537,
"cuid":"15001307",
"speech":"你的語音文件buffer"
encoding:null
headers:{Host: "vop.baidu.com", Content-Type: "application/json"}
method:"POST"
timeout:5000
url:"https://vop.baidu.com/server_api"
})
二次請(qǐng)求加強(qiáng)版
請(qǐng)求類型必須為application/json,
body channel 测萎,len亡电,rate值必須為int類型 ,其他為string類型硅瞧,注意buffer轉(zhuǎn)為base64格式字符串份乒,跟上一個(gè)還是有很大差別的,親測(cè)可用腕唧。
var options = {
method: 'POST',
url: 'https://vop.baidu.com/server_api',
headers:
{
"Content-Type": "application/json",
},
body:{
channel: 1,
cuid: '15001307',
dev_pid: 1537,
format: 'wav',
len: 54768,
rate: 8000,
token:'第一次請(qǐng)求返回的access_token',
speech: `語音文件buffer`
},
json: true
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});