-2

I have used below code snippet for google speech to text recognition,

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: '...'}]}]}]
});

Then I tried to do

"npm install google-speech-api"

from command prompt. It's giving error. Then, I did

"npm install googleapis"

and it succeeded. I executed the Node.js script from command prompt "node myspeech.js"...it's throwing error as,

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)
Gimby
  • 5,095
  • 2
  • 35
  • 47
  • What is the error when you do `npm install google-speech-api`? – DrakaSAN Jul 08 '16 at 12:17
  • @DrakaSAN npm ERR! Windows_NT 6.1.7601 npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\ node_modules\\npm\\bin\\npm-cli.js" "install" "google-speech-api" npm ERR! node v5.10.1 npm ERR! npm v3.8.3 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. npm ERR! Please include the following file with any support request: npm ERR! C:\Prasanta\npm-debug.log – Prasanta K Chakravarty Jul 09 '16 at 18:54

2 Answers2

1

As you can see in your error logs:

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.

You need to have git installed on your system, and in your PATH.

For Windows, you can use git-bash, for Debian/Ubuntu, a simple sudo apt-get install git should do the tricks.

DrakaSAN
  • 7,673
  • 7
  • 52
  • 94
  • @DrakaSAN..Yes, it works now. But in the next step again I stuck. Made some changes in the script as below, var request = require('superagent'); var speech = require('google-speech-api'); var opts = { filetype: 'flac', key: '' }; request .get('./audio/0001.flac') .pipe(speech(opts, function (err, results) { if (err) console.log(err); else console.log(JSON.stringify(results, null, 2)); })); Now when I execute the script from command prompt, node myspeechtest.js it doesn't give any output. – Prasanta K Chakravarty Jul 10 '16 at 14:14
  • @PrasantaKChakravarty: You will need a new question for that, SO do not support moving goal questions. You should read [the help center](http://stackoverflow.com/help/asking) about that. – DrakaSAN Jul 10 '16 at 19:11
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);
            });
Vahap Gencdal
  • 1,900
  • 18
  • 17