// Imports the Google Cloud client library
const speech = require('@google-cloud/speech');
const fs = require('fs');
var speechToText = function(filetoProcess , callback){
// Creates a client
const client = new speech.SpeechClient();
// The name of the audio file to transcribe
const fileName = filetoProcess;
// Reads a local audio file and converts it to base64
const file = fs.readFileSync(fileName);
const audioBytes = file.toString('base64');
// The audio file's encoding, sample rate in hertz, and BCP-47 language code
const audio = {
content: audioBytes,
};
const config = {
encoding: 'LINEAR16',
sampleRateHertz: 8000,
languageCode: 'en-IN',
profanityFilter: true,
// speechContexts: {
// phrases:["yes","no"]
// },
metadata: {
interactionType: "VOICEMAIL",
//industryNaicsCodeOfAudio: 23810,
microphoneDistance: "NEARFIELD",
originalMediaType: "AUDIO",
numberOfSpeakers: "ONE_SPEAKER",
//recordingDeviceType: "OTHER_INDOOR_DEVICE",
//recordingDeviceName: "Polycom SoundStation IP 6000",
//originalMimeType: "audio/mp3",
//obfuscatedId: "11235813",
//audioTopic: "questions about landmarks in NYC"
}
};
const request = {
audio: audio,
config: config,
};
// Detects speech in the audio file
client
.recognize(request)
.then(data => {
const response = data[0];
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
callback(transcription);
})
.catch(err => {
console.error('ERROR:', err);
});
}
module.exports.speechToText = speechToText;
const speech = require('@google-cloud/speech');
const fs = require('fs');
var speechToText = function(filetoProcess , callback){
// Creates a client
const client = new speech.SpeechClient();
// The name of the audio file to transcribe
const fileName = filetoProcess;
// Reads a local audio file and converts it to base64
const file = fs.readFileSync(fileName);
const audioBytes = file.toString('base64');
// The audio file's encoding, sample rate in hertz, and BCP-47 language code
const audio = {
content: audioBytes,
};
const config = {
encoding: 'LINEAR16',
sampleRateHertz: 8000,
languageCode: 'en-IN',
profanityFilter: true,
// speechContexts: {
// phrases:["yes","no"]
// },
metadata: {
interactionType: "VOICEMAIL",
//industryNaicsCodeOfAudio: 23810,
microphoneDistance: "NEARFIELD",
originalMediaType: "AUDIO",
numberOfSpeakers: "ONE_SPEAKER",
//recordingDeviceType: "OTHER_INDOOR_DEVICE",
//recordingDeviceName: "Polycom SoundStation IP 6000",
//originalMimeType: "audio/mp3",
//obfuscatedId: "11235813",
//audioTopic: "questions about landmarks in NYC"
}
};
const request = {
audio: audio,
config: config,
};
// Detects speech in the audio file
client
.recognize(request)
.then(data => {
const response = data[0];
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
callback(transcription);
})
.catch(err => {
console.error('ERROR:', err);
});
}
module.exports.speechToText = speechToText;
very good blog thanks for sharing your valuable blog keep it up! i have some good tips for your blog.
ReplyDeleteSpeech to Text