如何通过 fetch 或 axios 获取音频文件缓冲区并在 google speech to text 中使用它?

问题描述 投票:0回答:0

我有以下代码:

async function transcribeAudio(audioLink) {
  const audioFile = await fetch(audioLink);
  const audioBuffer = await audioFile.buffer();

  const client = new speech.SpeechClient();

  const request = {
    config: {
      enableAutomaticPunctuation: true,
      encoding: "LINEAR16",
      sampleRateHertz: 48000, // Alterar a taxa de amostragem para 48000
      languageCode: "pt-BR",
      audioChannelCount: 2,
      AudioEncoding: "LINEAR16", // Definir o formato de áudio como LINEAR16
    },
    audio: {
      content: audioBuffer
    },
  };

  const [response] = await client.recognize(request);
  const transcription = response.results
    .map((result) => result.alternatives[0].transcript)
    .join("\n");
  return transcription;
}

我的音频链接是来自 jotform 录音机的 url(例如:https://www.jotform.com/widget-uploads/voiceRecorder/230202903938054/fake_name.wav),我的文件是 WAV。

当我使用 fs 将我的文件放在“内容”上时,它可以工作,但我不想在本地进行,我想使用缓冲区或类似的东西来进行。 使用 fetch/axios 时,我的响应返回空。

Console.log from response and transcription

node.js speech-recognition speech-to-text google-speech-api google-speech-to-text-api
© www.soinside.com 2019 - 2024. All rights reserved.