So I wanted to take MP3 file as input from user and then I wanted to convert it into base64 string.
I was able to take input using the <input type="file">
tag but when I try to use 'fs' for converting it into base64 string I get an error :
error - TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL.
I basically want to take an input file MP3 as input from user and then use Google Speech-To-Text Api to convert it into text.
Heres my front-end code :
import React, {useEffect,Component} from 'react';
import Head from 'next/head'
import styles from '../styles/Home.module.css'
import axios from 'axios';
class Home extends Component {
state = {
selectedFile:null
}
onFileChange = event => {
// Update the state
this.setState({ selectedFile: event.target.files[0] });
};
onFileUpload = async () => {
let res = await axios.post('/api/backend',this.state.selectedFile);
console.log("response is ",res);
}
render()
{
return (
<div className={styles.container}>
<Head>
<title>Text to speech</title>
<meta name="description" content="Generated by create next app" />
<link rel="icon" href="/favicon.ico" />
</Head>
<div>
<input type="file" onChange={this.onFileChange} />
<button onClick={this.onFileUpload}>
Upload!
</button>
</div>
</div>
)
}
};
export default Home;
Backend.js:
// Imports the Google Cloud client library
import speech from '@google-cloud/speech'
const fs = require('fs');
export default async function Speech(req,res) {
// Creates a client
const client = new speech.SpeechClient();
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
const filename = req.body.name;
const encoding = 'MP3';
const sampleRateHertz = 16000;
const languageCode = 'az-AZ';
const config = {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
};
/**
* Note that transcription is limited to 60 seconds audio.
* Use a GCS file for audio longer than 1 minute.
*/
const audio = {
content: fs.readFileSync(filename).toString('base64'),
};
const request = {
config: config,
audio: audio,
};
// 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();
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
res.status(200).json(transcription)
}