Javascript 使用Electron的gRPC实时转录Google云语音API

Javascript 使用Electron的gRPC实时转录Google云语音API,javascript,google-cloud-platform,electron,Javascript,Google Cloud Platform,Electron,我想要实现的是与Web语音API相同的实时转录过程,但使用Google云语音API 主要目标是使用gRPC协议,通过带有语音API的电子应用程序转录实况录音 这是我实现的简化版本: const{desktopCapturer}=window.require('electron'); const speech=require(“@googlecloud/speech”); const client=speech.v1({ projectId:“我的项目id”, 证书:{ 客户电子邮件:“我的客户电

我想要实现的是与Web语音API相同的实时转录过程,但使用Google云语音API

主要目标是使用gRPC协议,通过带有语音API的电子应用程序转录实况录音

这是我实现的简化版本:

const{desktopCapturer}=window.require('electron');
const speech=require(“@googlecloud/speech”);
const client=speech.v1({
projectId:“我的项目id”,
证书:{
客户电子邮件:“我的客户电子邮件”,
私钥:“我的私钥”,
},
});
desktopCapturer.getSources({types:['window','screen']},(错误,源)=>{
navigator.mediaDevices
.getUserMedia({
音频:是的,
})
。然后((流)=>{
让fileReader=newFileReader();
让arrayBuffer;
fileReader.onloadend=()=>{
arrayBuffer=fileReader.result;
让speechStreaming=客户端
.流线型识别({
配置:{
编码:speech.v1.types.RecognitionConfig.AudioEncoding.LINEAR16,
语言代码:“en US”,
赫兹:44100,
},
一句话:没错,
})
.on('数据',(响应)=>响应);
speechStreaming.write(arrayBuffer);
};
readAsArrayBuffer(流);
});
});
语音API的错误响应是音频流太慢,我们没有实时发送它


我觉得原因是我传递的流没有任何格式或对象初始化,因此无法执行流识别。

Github上的这个官方示例项目似乎与您所寻找的内容相匹配:

此应用程序演示了如何使用谷歌云语音API的StreamingRecognite操作执行无限流

另见Electron中使用OtterAI转录服务的替代方案。(这是我将很快尝试的方法)

您可以使用该模块录制音频,并通过管道直接连接到像谷歌这样的语音识别系统

在存储库中,有一个using wit.ai

对于Google语音识别,您可以使用以下内容:

'use strict'
const { SpeechClient } = require('@google-cloud/speech')
const recorder = require('node-record-lpcm16')

const RECORD_CONFIG = {
  sampleRate: 44100,
  recorder: 'arecord'
}

const RECOGNITION_CONFIG = {
  config: {
    sampleRateHertz: 44100,
    language: 'en-US',
    encoding: 'LINEAR16'
  },
  interimResults: true
}

const client = new SpeechClient(/* YOUR CREDENTIALS */)

const recognize = () => {
  client
    .streamingRecognize(RECOGNITION_CONFIG)
    .on('error', err => {
      console.error('Error during recognition: ', err)
    })
    .once('writing', data => {
      console.log('Recognition started!')
    }
    .on('data', data => {
      console.log('Received recognition data: ', data)
    }
}

const recording = recorder.record(RECORD_CONFIG)
recording
  .stream()
  .on('error', err => {
     console.error('Error during recognition: ', err)
  .pipe(recognize)

你能用electron解决这个问题吗?我也有同样的任务。我也在寻找电子的实时转录。关于如何在Electron中使用Google Cloud Speech,我没有答案,但我想我应该提到另一个选择:创建一个iframe到“otter.ai”(每月免费600分钟的转录服务),让用户登录(使用open auth登录,因此速度非常快),然后在iframe中插入自定义代码(webview预加载)允许您在需要时启动转录并检索其中转录的文本。不寻常的方法,但水獭的转录是相当好的,每个用户每月600分钟的免费时间是不错的。