I am trying to use Google Speech API to convert audio files to strings but I can’t make it work when I try to transcribe an audio file that is uploaded by my form element.
<Form onSubmit={handleSubmit}>
<input type="file" ref={audioFileRef} />
<Button>Import</Button>
</Form>
This is my fetch:
fetch(path, {
method: "POST",
headers: {
"Content-Type": "audio/wave",
},
body: formData,
})
This is the server:
async function main(content) {
const client = new speech.SpeechClient();
const config = {
encoding: "LINEAR16",
sampleRateHertz: 16000,
languageCode: "en-US",
};
const audio = {
uri: content,
};
const request = {
audio: audio,
config: config,
};
const [response] = await client.recognize(request);
return await response.results
.map((result) => result.alternatives[0].transcript)
.join("n");
}
app.post("/import-audio", async (req, res) => {
const transcription = main(req.body);
transcription.then(console.log).catch(console.error);
});
I am trying to transcribe the audio file from the form element