-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (53 loc) · 1.66 KB
/
index.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
60
61
// Imports the Google Cloud client library
const speech = require('@google-cloud/speech');
const ffmpegInstaller = require('@ffmpeg-installer/ffmpeg');
const ffmpeg = require('fluent-ffmpeg');
const fs = require('fs');
const fileName =
'/Users/mertsusur/Downloads/Acik-kaynak-kodlu-projeler-edited.mp3';
ffmpeg.setFfmpegPath(ffmpegInstaller.path);
ffmpeg(fileName)
.toFormat('wav')
.on('error', err => {
console.log('An error occurred: ' + err.message);
})
.on('progress', progress => {
// console.log(JSON.stringify(progress));
console.log('Processing: ' + progress.targetSize + ' KB converted');
})
.on('end', () => {
console.log('Done.');
// Reads a local audio file and converts it to base64
const file = fs.readFileSync('./track.wav');
const audioBytes = file.toString('base64');
console.log('Loaded the new file and converted to base64');
// The audio file's encoding, sample rate in hertz, and BCP-47 language code
const audio = {
content: audioBytes
};
const config = {
encoding: 'LINEAR16',
sampleRateHertz: 16000,
languageCode: 'tr-TR'
};
const request = {
audio: audio,
config: config
};
console.log('Uploading');
// Detects speech in the audio file
recognise(request);
})
.save('./track.wav');
const recognise = async request => {
const client = new speech.SpeechClient();
try {
const [response] = await client.recognize(request);
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
} catch (e) {
console.log(e);
}
};