【发布时间】:2020-01-27 20:27:40
【问题描述】:
我想识别 NodeJS 服务器中的音频文件。
我在Windows 10上使用命令行,Node的版本是10.6.0,我已经安装了@google-cloud/speech 经常使用 npm。更重要的是,我已经声明了凭据的环境变量(在此解释 https://cloud.google.com/docs/authentication/getting-started?hl=en),并且我已将 json 文件复制到“凭据”文件夹中:
设置 GOOGLE_APPLICATION_CREDENTIALS="C:\Users\Me\Documents\NodeJs\Project1\credentials\RDCommandeVocale-b521de3b57d9.json"
文件是通过 ffmpeg 使用以下命令编码的:
ffmpeg -i newRecording.aac -vol 512 -c flac -ar 16000 newRecording.flac
我的源代码是:
const folderName = "uploaded";
const fileName = "newRecording";
const client = new speech.SpeechClient();
const config = {
encoding:"FLAC",
sampleRateHertz: 16000,
languageCode: "fr-FR"
};
const audioBytes = fs.readFileSync(`${__dirname}\\` + folderName + "\\" + fileName + ".flac").toString('base64');
//This doesn't work else with this :
//const audioBytes = fs.readFileSync(".\\uploaded" + fileName + ".flac").toString('base64');
// ... nor this one
//const audioBytes = fs.readFileSync("./uploaded" + fileName + ".flac").toString('base64');
const request = {
config: config,
audio: audioBytes,
};
client.recognize(request).then( response=>{
const transcription = response.results
.map(result => result.alternatives[0].transcript)// récupérer uniquement la première alternative
.join('\n');
console.log("Textual transcription: ", transcription );
res.status(200)
.json({ status: "success", message: transcription });
},
(err)=>{
console.log("Transcription ERROR : ", JSON.stringify(err));
res.status(500)
.json({ status: "error", message: JSON.stringify(err) });
});
我收到此错误:
文本转录:{"errno":-4058,"syscall":"lstat","code":"ENOENT","path":"c:\Users\Me\Documents\Me\NodeJs\Project1\ \"C:"}
Google Cloud API 文档中的任何地方都引用了这种错误类型吗?
【问题讨论】:
-
我认为这可能是目录错误(此处采用的想法:[stackoverflow.com/questions/48370690/…),但您能告诉我这是否可能是身份验证问题吗?向 Google Cloud Speech API 发送请求是否需要 Google Cloud 的身份验证?
标签: node.js windows google-cloud-speech