将google语音API与Node.js一起使用时出现的问题
我使用了下面的代码片段进行谷歌语音到文本识别将google语音API与Node.js一起使用时出现的问题,node.js,google-speech-api,Node.js,Google Speech Api,我使用了下面的代码片段进行谷歌语音到文本识别 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: '...'}]
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: '...'}]}]}]
});
var speech=require('google-speech-api');
变量选项={
文件:“speech.mp3”,
键:“”
};
语音(选项、功能(错误、结果){
控制台日志(结果);
//[{结果:[{备选方案:[{转录本:'..'}]}]}]
});
然后我试着去做
“npm安装谷歌语音api”
从命令提示符。这是一个错误。
然后,我做到了
“npm安装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)
module.js:341
犯错误;
^
错误:找不到模块“google语音api”
在Function.Module.\u解析文件名(Module.js:339:15)
在Function.Module.\u加载(Module.js:290:25)
at Module.require(Module.js:367:17)
根据需要(内部/module.js:16:19)
反对。(C:\myspeechtest.js:1:76)
在模块处编译(Module.js:413:34)
在Object.Module._extensions..js(Module.js:422:10)
在Module.load(Module.js:357:32)
在Function.Module.\u加载(Module.js:314:12)
位于Function.Module.runMain(Module.js:447:10)
您可以在错误日志中看到:
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.
您需要在您的系统和路径中安装git
对于Windows,您可以使用Debian/Ubuntu,一个简单的
sudo-apt-get-install-git
就可以了。当您执行npm-install-google-speech-api
时会出现什么错误?@DrakaSAN-npm-ERR!Windows\u NT 6.1.7601 npm错误!argv“C:\\Program Files\\nodejs\\node.exe”“C:\\Program Files\\nodejs\\node\u modules\\npm\\bin\\npm cli.js”“安装”“谷歌语音api”npm ERR!节点v5.10.1 npm错误!npm v3.8.3 npm错误!代码ENOGIT npm ERR!未找到:git npm ERR!npm错误!使用git失败。npm错误!这很可能不是npm本身的问题。npm错误!请检查您的路径中是否安装了git。npm错误!请在任何支持请求中包括以下文件:npm ERR!C:\Prasanta\npm调试。log@DrakaSAN..Yes,它现在可以工作了。但下一步我又卡住了。在脚本中做了如下更改,var request=require('superagent');var speech=require('google-speech-api');var opts={filetype:'flac',键:'};request.get('./audio/0001.flac').pipe(speech(opts,function(err,results){if(err)console.log(err);else console.log(JSON.stringify(results,null,2));});现在,当我从命令提示符节点myspeechtest.js执行脚本时,它不会给出任何输出。@PrasantaKChakravarty:你需要一个新的问题,所以不支持移动目标问题。你应该读一下。
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);
});