Advertisement
Arthurious

Untitled

Nov 18th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. async function main() {
  2. // Imports the Google Cloud client library
  3. const speech = require('@google-cloud/speech');
  4. const fs = require('fs');
  5.  
  6. // Creates a client
  7. const client = new speech.SpeechClient();
  8.  
  9. // The name of the audio file to transcribe
  10. const fileName = './resources/audio.raw';
  11.  
  12. // Reads a local audio file and converts it to base64
  13. const file = fs.readFileSync(fileName);
  14. const audioBytes = file.toString('base64');
  15.  
  16. // The audio file's encoding, sample rate in hertz, and BCP-47 language code
  17. const audio = {
  18. content: audioBytes,
  19. };
  20. const config = {
  21. encoding: 'LINEAR16',
  22. sampleRateHertz: 16000,
  23. languageCode: 'en-US',
  24. };
  25. const request = {
  26. audio: audio,
  27. config: config,
  28. };
  29.  
  30. // Detects speech in the audio file
  31. const [response] = await client.recognize(request);
  32. const transcription = response.results
  33. .map(result => result.alternatives[0].transcript)
  34. .join('\n');
  35. console.log(`Transcription: ${transcription}`);
  36. }
  37. main().catch(console.error);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement