Skip to content

Instantly share code, notes, and snippets.

@wesbos
Created September 30, 2020 14:47
Show Gist options
  • Save wesbos/2a911e02ad705fdc8819360c0729b057 to your computer and use it in GitHub Desktop.
Save wesbos/2a911e02ad705fdc8819360c0729b057 to your computer and use it in GitHub Desktop.
Syntax Speech to Text
import speech from '@google-cloud/speech';
import fs from 'fs';
import dotenv from 'dotenv';
dotenv.config();
async function main() {
const client = new speech.SpeechClient();
const config = {
encoding: 'MP3',
sampleRateHertz: 44100,
languageCode: 'en-US',
diarizationConfig: {
enableSpeakerDiarization: true,
minSpeakerCount: 2,
maxSpeakerCount: 3,
},
metadata: {
interactionType: 'PROFESSIONALLY_PRODUCED',
// 541511 Custom Computer Programming Services Web (i.e., Internet) page design services, custom
industryNaicsCodeOfAudio: 541511,
microphoneDistance: 'NEARFIELD',
originalMediaType: 'AUDIO',
recordingDeviceType: 'OTHER_INDOOR_DEVICE',
recordingDeviceName: 'Cardioid Microphone',
originalMimeType: 'audio/mp3',
audioTopic: 'Syntax Podcast',
},
speechContexts: [
{
phrases: [
'syntax',
'Hasty Treat',
'Wes Bos',
'Scott Tolinski',
'tuples',
'Log Rocket',
'logrocket.com',
],
},
],
};
const audio = {
// uri: 'gs://syntax/Syntax287.mp3',
uri: 'gs://syntax/clip.mp3',
};
const request = {
config,
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}`);
fs.writeFileSync('syntax3.txt', transcription);
}
main().catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment