-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscribe.js
More file actions
75 lines (65 loc) · 2.24 KB
/
transcribe.js
File metadata and controls
75 lines (65 loc) · 2.24 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Imports the Google Cloud client library for Beta API
/**
* TODO(developer): Update client library import to use new
* version of API when desired features become available
*/
const speech = require('@google-cloud/speech').v1p1beta1;
const fs = require('fs');
const path = require('path');
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
// Creates a client
const client = new speech.SpeechClient();
const dir = path.join('C:', 'Users', 'mweis', 'Downloads', 'foolish_ambitions')
async function transcriptAndRename(filename) {
const oldPath = path.join(dir, filename);
console.log("Old path: " + oldPath)
const model = 'video';
const encoding = 'MP3';
const sampleRateHertz = 48000;
const languageCode = 'en-us';// BCP-47 language code, e.g. en-US
const config = {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
model: model,
};
const audio = {
content: fs.readFileSync(oldPath).toString('base64'),
};
const request = {
config: config,
audio: audio,
};
// Detects speech in the audio file
const [response] = await client.recognize(request);
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n').replace(/[^a-zA-Z ]/g, "");
console.log('Transcription: ', transcription);
if (transcription != "") {
const newPath = path.join('C:', 'Users', 'mweis', 'Downloads', 'foolish_ambitions_transcribed', transcription + ".mp3")
console.log('newpath', newPath);
fs.renameSync(oldPath, newPath)
}
else {
console.log("Could not transcribe: " + oldPath)
const newPath = path.join('C:', 'Users', 'mweis', 'Downloads', 'trash', filename);
console.log('newpath', newPath);
fs.renameSync(oldPath, newPath)
}
}
let count = 0
let sleep = require('util').promisify(setTimeout);
fs.readdir(dir, async (err, files) => {
if (err) {
throw err;
}
// files object contains all files names
// log them on console
for (let file of files) {
console.log(file)
transcriptAndRename(file);
// Only 900 attempts per minute
await sleep(69)
}
});