-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
148 lines (122 loc) · 3.97 KB
/
server.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const express = require('express');
const bodyParser = require('body-parser');
const multer = require('multer');
const pug = require('pug');
const app = express();
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
// app.set("view engine", "pug");
// app.set("views", path.join(__dirname, "views"));
// SET STORAGE
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads')
},
filename: function (req, file, cb) {
cb(null, file.fieldname+'.flac')
}
})
var times = [];
var upload = multer({ storage: storage })
app.post('/uploadfile', upload.single('myFile'), (req, res, next) => {
const file = req.file
if (!file) {
const error = new Error('Please upload a file')
error.httpStatusCode = 400
return next(error)
}
uploadAudio();
res.send(file)
})
app.post('/transcribe', (req, res) => {
transcribe(req.body["key-val"]);
//res.json(req.body);
//res.render('index.html',{timestamps: times});
res.send('Transcribed! <br> <a href="./index.html">Go Back</a>');
})
async function transcribe(keyToFind) {
// Imports the Google Cloud client library
const speech = require('@google-cloud/speech');
const fs = require('fs');
// Creates a client
const client = new speech.SpeechClient();
const key = keyToFind;
// Clear the previous time array
times = [];
// The audio file's encoding, sample rate in hertz, and BCP-47 language code
const audio = {
uri: "gs://cmdfaudio/narrative.flac",
};
const config = {
enableWordTimeOffsets: true,
encoding: 'FLAC',
sampleRateHertz: 22050,
languageCode: 'en-US',
};
const request = {
audio: audio,
config: config,
};
// 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();
await response.results.forEach(result => {
console.log(`Transcription: ${result.alternatives[0].transcript}`);
result.alternatives[0].words.forEach(wordInfo => {
if (key == wordInfo.word) {
const startSecs =
`${wordInfo.startTime.seconds}` +
`.` +
wordInfo.startTime.nanos / 100000000;
times.push(startTime);
}
});
});
times.forEach(element => {
console.log(element);
});
}
transcribe().catch(console.error);
async function uploadAudio() {
const {Storage} = require('@google-cloud/storage');
// Creates a client
const storage = new Storage();
const bucketName = 'cmdfaudio';
const audios = storage.bucket(bucketName);
const filename = './uploads/myFile.flac';
const options = {
entity: 'allUsers',
role: storage.acl.READER_ROLE
};
audios.acl.add(options, function(err, aclObject) {});
//-
// Make any new objects added to a bucket publicly readable.
//-
audios.acl.default.add(options, function(err, aclObject) {});
// Uploads a local file to the bucket
await storage.bucket(bucketName).upload(filename, {
// Support for HTTP requests made with `Accept-Encoding: gzip`
gzip: true,
metadata: {
// Enable long-lived HTTP caching headers
// Use only if the contents of the file will never change
// (If the contents will change, use cacheControl: 'no-cache')
cacheControl: 'public, max-age=31536000',
},
});
console.log(`${filename} uploaded to ${bucketName}.`);
const filename_bucket = 'myFile.flac';
await storage
.bucket(bucketName)
.file(filename_bucket)
.makePublic();
console.log(`gs://${bucketName}/${filename} is now public.`);
}
app.listen(3000, () => {
console.log('app is running on port 3000');
});