1

I am running a cloud function where I use Google speech to text api but I always get empty results. I am using a .wav audio file. The audio file is 4 minutes long and it's size is approximately 40mb. There might be some error with configuration but I have checked the docs and didn't find any clue. Here is my code:

exports.transcribeAudio = functions
  .runWith({ timeoutSeconds: 540 })
  .firestore.document("users/{userId}/transcriptions/{transcriptionId}")
  .onCreate(async (snap, context) => {
    const file = snap.data();
    const userId = context.params.userId;
    const userRef = admin.firestore().collection("users").doc(userId);
    const userDoc = await userRef.get();
    const credits = userDoc.data().credits;
    try {
      if (credits <= 0) {
        throw "Not enough credits";
      }
      const downloadUrl = new URL(file.url);
      const request = {
        config: {
          encoding: "LINEAR16",
          sampleRateHertz: 16000,
          languageCode: "en-US",
        },
        audio: {
          uri: downloadUrl,
        },
      };

      const [operation] = await speechClient.longRunningRecognize(request);
      const [response] = await operation.promise();

      const transcription = response.results
        .map((result) => result.alternatives[0].transcript)
        .join("\n");

      const transcriptionsRef = userRef
        .collection("transcriptions")
        .doc(context.params.transcriptionId);
      await transcriptionsRef.update({ text: transcription });
      const newCredits = credits - 1;
      userRef.update({ credits: newCredits });
    } catch (error) {
      console.log(error);
    }
  });

Here the response.results returns empty string.

Harsh Vardhan
  • 559
  • 1
  • 3
  • 12

0 Answers0