38 lines
1,008 B
JavaScript
38 lines
1,008 B
JavaScript
|
|
const speech = require('@google-cloud/speech');
|
|
|
|
// Creates a client
|
|
const client = new speech.SpeechClient();
|
|
|
|
const encoding = 'LINEAR16';
|
|
const sampleRateHertz = 16000;
|
|
const languageCode = 'en-US';
|
|
|
|
const config = {
|
|
encoding: encoding,
|
|
sampleRateHertz: sampleRateHertz,
|
|
languageCode: languageCode,
|
|
};
|
|
|
|
/**
|
|
* Note that transcription is limited to 60 seconds audio.
|
|
* Use a GCS file for audio longer than 1 minute.
|
|
*/
|
|
async function transcribeSpeech (audio) {
|
|
const request = {
|
|
config: config,
|
|
audio: audio,
|
|
};
|
|
|
|
// Detects speech in the audio file. This creates a recognition job that you
|
|
// can wait for now, or get its result later.
|
|
const [operation] = await client.longRunningRecognize(request);
|
|
|
|
// Get a Promise representation of the final result of the job
|
|
const [response] = await operation.promise();
|
|
|
|
const transcription = response.results
|
|
.map(result => result.alternatives[0].transcript)
|
|
.join('\n');
|
|
console.log(`Transcription: ${transcription}`);
|
|
}
|