expo + google speech-to-text transcription

94 Views Asked by At

im trying to transcribe an audio file using google speech-to-text api, if im just using the example code passing the uri, work as expected, but when i change uri to content and pass my audio archive on base64 i got an empty object. How i can solve this?

the uri file that i got on console: uri file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540fakename%252Ffakeapp/Audio/recording-277b833a-748d-48ad-b1b1-f1ca800608b7.m4a

and when i encoding into base64 work as well but doesn't transcribe my audio file.

with google example code(work): enter image description here

with my local audio archive(doesnt work): enter image description here

i just need to transcribe my audios files into text using google speech-to-text api.

1

There are 1 best solutions below

1
On

I can't copy your code for replication, But there is readily sample documentation here for your reference.

Sample:

// Imports the Google Cloud client library
const fs = require('fs');
const speech = require('@google-cloud/speech');

// Creates a client
const client = new speech.SpeechClient();

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const filename = 'Local path to audio file, e.g. /path/to/audio.raw';
// const encoding = 'Encoding of the audio file, e.g. LINEAR16';
// const sampleRateHertz = 16000;
// const languageCode = 'BCP-47 language code, e.g. en-US';

const config = {
  encoding: encoding,
  sampleRateHertz: sampleRateHertz,
  languageCode: languageCode,
};
const audio = {
  content: fs.readFileSync(filename).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');
console.log('Transcription: ', transcription);