我使用下面的代码片段进行谷歌语音识别,
var speech = require('google-speech-api');
var opts = {
file: 'speech.mp3',
key: '<Google API Key>'
};
speech(opts, function (err, results) {
console.log(results);
// [{result: [{alternative: [{transcript: '...'}]}]}]
});
然后我试着做
从命令提示符“npm install google-speech-api”
。它给出了错误。 然后,我做了
“npm install googleapis”
它成功了。 我从命令提示符“node myspeech.js”执行了Node.js脚本......它正在抛出错误,
module.js:341
throw err;
^
Error: Cannot find module 'google-speech-api'
at Function.Module._resolveFilename (module.js:339:15)
at Function.Module._load (module.js:290:25)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (C:\myspeechtest.js:1:76)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
答案 0 :(得分:1)
正如您在错误日志中看到的那样:
npm ERR! code ENOGIT
npm ERR! not found: git npm
ERR! npm ERR! Failed using git.
npm ERR! This is most likely not a problem with npm itself.
npm ERR! Please check if you have git installed and in your PATH.
您需要在系统和PATH中安装git。
对于Windows,您可以使用git-bash,对于Debian / Ubuntu,一个简单的sudo apt-get install git
应该使用这些技巧。
答案 1 :(得分:0)
const projectId = 'yourGoogleProjectId';
let file="conf.json"//google exported this for you
var speech = require('@google-cloud/speech')({
projectId: projectId,
keyFilename: file
});
const fs = require('fs');
const fileName = 'yourMp3FilePath';
// Reads a local audio file and converts it to base64
const fileMp3 = fs.readFileSync(fileName);
const audioBytes = fileMp3.toString('base64');
const audio = {
content:audioBytes
};
const config = {
encoding: 'AMR_WB',
sampleRateHertz: 16000,
languageCode: 'en-US'
};
const request = {
audio: audio,
config: config
};
speech.recognize(request)
.then((results) => {
const transcription = results[0].results[0].alternatives[0].transcript;
console.log(`Transcription: `, transcription);
})
.catch((err) => {
console.error('ERROR:', err);
});