Firebase Admin
Firebase analytics
Pricing
Privacy and Security
Useful resources
Firebase Extensions
Cloud Run
STT + Cloud Function
​
This implementation is not suitable for long files as google cloud function times out after 1min to 9min ​
But for shorter files, you can add your media to a google cloud storage and run google cloud STT inside a cloud function and save the result to firestore.
​Firestore events​
1
// Goolge App Engine calls STT SDK
2
// this function returns null.
3
exports.createTranscript = functions.firestore
4
.document("transcripts/{transcriptId}")
5
.onCreate(async (change, context) => {
6
// Get an object representing the document
7
const newValue = change.data();
8
// for now only run STT for uploaded files
9
// eg if running from STT then don't run this
10
if (newValue.transcriptionType === "upload") {
11
// access a particular field as you would any JS property
12
const storageRef = newValue.storageRef;
13
console.log("storageRef", storageRef);
14
// https://firebase.google.com/docs/storage/admin/start
15
const storage = admin.storage();
16
// https://github.com/firebase/firebase-tools/issues/1573#issuecomment-517000981
17
const bucket = storage.appInternal.options.storageBucket;
18
// // const gcsUri = 'gs://my-bucket/audio.raw';
19
const gcsUri = `gs://${bucket}/${storageRef}`;
20
console.log("gcsUri", gcsUri);
21
//////// STT ////////
22
// Creates a client for STT
23
const client = new speech.SpeechClient();
24
const encoding = "mp3"; //'Encoding of the audio file, e.g. LINEAR16';
25
const sampleRateHertz = "48000"; //16000;
26
// const languageCode = newValue.language.value;
27
const languageCode = "en-US"; //'BCP-47 language code, e.g. en-US';
28
​
29
const config = {
30
encoding: encoding,
31
sampleRateHertz: sampleRateHertz,
32
languageCode: languageCode
33
};
34
​
35
const audio = {
36
uri: gcsUri
37
};
38
​
39
const request = {
40
config: config,
41
audio: audio
42
};
43
​
44
// // Detects speech in the audio file. This creates a recognition job that you
45
// // can wait for now, or get its result later.
46
const [operation] = await client.longRunningRecognize(request);
47
// Get a Promise representation of the final result of the job
48
const [response] = await operation.promise();
49
// TODO: Convert to DPE
50
// TODO: Save DPE json format to firebase as collection
51
​
52
// get text - tmp
53
const transcription = response.results
54
.map(result => result.alternatives[0].transcript)
55
.join("\n");
56
console.log(`Transcription: ${transcription}`);
57
​
58
// Then return a promise of a set operation to update the document
59
return change.ref.set(
60
{
61
// TODO: change transcription to transcript
62
transcription: transcription
63
},
64
{ merge: true }
65
);
66
} else {
67
return null;
68
}
69
});
Copied!

links

​SpeechClient - SDK​
Copy link
Contents
links