-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-test.js
59 lines (50 loc) · 1.52 KB
/
index-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
async function main() {
// Imports the Google Cloud client library
const speech = require('@google-cloud/speech');
const fs = require('fs');
const { Translate } = require('@google-cloud/translate');
// Your Google Cloud Platform project ID
const projectId = 'robot-229713';
// Creates a client
const client = new speech.SpeechClient();
// The name of the audio file to transcribe
const fileName = './scratched.flac';
// 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: 'LINEAR',
sampleRateHertz: 32000,
languageCode: 'sv-SE',
};
const request = {
audio: audio,
config: config,
};
// Detects speech in the audio file
const [response] = await client.recognize(request);
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
const translate = new Translate({
projectId: projectId,
});
// The target language
const target = 'sv';
// Translates some text into Russian
translate
.translate(transcription, target)
.then(results => {
const translation = results[0];
console.log(`Translation: ${translation}`);
})
.catch(err => {
console.error('ERROR:', err);
});
}
main().catch(console.error);