【问题标题】:GCS binary upload - audio file not playing upon downloadGCS 二进制上传 - 下载时不播放音频文件
【发布时间】:2019-08-08 19:39:02
【问题描述】:

此代码使用 opus 编码流作为云语音识别的输入(选项:contentType:'OGG_OPUS',sampleRateHertz:48000,)并且工作正常。

但是,在示例之后,将同一流的 bufferCopy 发送到云存储 api,二进制 upload code 会导致下载后无法播放音频的错误。同样在下载时,同时使用“ogg”和“opus”文件类型,ffprobe 将无法正确检测编码。

上传到 GCS 的控制台和输出看起来不错 - 大小正确的二进制文件会出现在那里,并且可以使用 gsutils 下载。

适用于 google 云识别 api 的 opus 流不适合作为音频流的简单文件接收器的云存储 api。我使用 gsutil 从云端获取音频文件,但它不在任何播放器中播放,而且 ffprobe 没有检测到编码。

我不知道如何调试问题。客户端上的原始音频 blob 播放正常,其大小(以字节为单位)非常接近,但不等于上传到 GCS api/从 GCS api 下载的文件的大小。

代码细节如下:socket-io 用于从 js 层获取数据到这个 express 服务器实例......

  client.on('startGoogleCloudStream', function (data) {
    // startRecognitionStream(this, data);
    console.log('STRMbeg ' + typeof recognizeStream);
    rs = new stream.Readable();
    rs._read = function () {};
    readStream1 = new ReadableStreamClone(rs);
    readStream2 = new ReadableStreamClone(rs);
    startRecognitionStream(this);

    let rfil = 'audio/' +uuidv4() + '.ogg'; //typ '.opus' same error
    const file = myBucket.file(rfil);
    var otstrm = file.createWriteStream({
      metadata: {
        contentType: 'audio/ogg'
      },
      gzip: false,
      resumable: false
    });
    readStream1.pipe(recognizeStream);// works fine
    readStream2.pipe(otstrm) // gets a corrupted binary up on cloud
    .on('error', function(err) {
      console.log('second strm ' + err);
    })
    .on('finish', function() {
      console.log('Done BcketFilaudio local');
    });
  });

  client.on('endGoogleCloudStream', function (data) {
    console.log('STRMend');
    // stopRecognitionStream();
    rs.push(null); // null is Stream.END
    recognizeStream = null;
  });

  client.on('binaryData', function (data) {
    console.log('data ' + data.length ); // log binary data
    if (recognizeStream !== null) {
      let _bfr = Buffer.from(data.buffer);
      rs.push(_bfr);
    }
  });

  function startRecognitionStream (client, data) {
    recognizeStream = speechClient.streamingRecognize(request)
      .on('error', console.error)
      .on('data', (data) => { // back to client on socket.io
      //  Dev only logging
        process.stdout.write(
          (data.results[0] && data.results[0].alternatives[0])
          ? `Transcription: ${data.results[0].alternatives[0].transcript}\n`
          : `\n\nReached transcription time limit, press Ctrl+C\n`
        );

        client.emit('speechData', data);
        if (data.results[0] && data.results[0].isFinal) {
          postRecSpeech(data.results[0].alternatives[0].transcript);
        }
      });
  }

【问题讨论】:

  • 跟进:这涉及到原始音频流的 2 个消费者(客户端上的 mic - 以 express 作为客户端,将 inputStreams 表示为 2,google API 的 #1 STT #2 GCS )。尽管流式处理与一种 API (STT) 非常匹配,但它似乎不适用于另一种(GCS 似乎不支持 node.js 客户端管道流)。我看到了流到 GCS 的“boto”库和一些“gsutil”示例,但这还不够。我放弃了将流复制到正在使用的各种 API 的想法。
  • 您好,很难跟进您的问题。您能否提供一些可重现的步骤?
  • 如果我让我的项目在一个分支中处于可工作状态,我就把它放在 git 上。关于从电话到表达后端到云语音 API 的音频流(二进制/音频/ogg),它看起来像 blob 的 POST 在 https 上表达,然后执行 http.request.body.pipe(writeStream-for-GCS ) 会正常工作。那不是我想要的。我从麦克风流式传输音频块来表达。 onData, on express, forEach(chunk) 我想复制流并将其通过管道传输到相应的 API(cloudspeech STT,GCS).. 这就是 GCS 悄悄失败的地方(文件到达服务,但作为损坏的二进制文件

标签: google-cloud-storage ogg node-streams


【解决方案1】:

出于某种原因,GCS 不接受与 Cloud Speech 一起使用的流的副本,并且将服务器端流作为多个 API 的输入的重用不起作用。

-- 解决方法

尽管流(录音机)已经作为流在服务器端,但流块必须连接到客户端上的 blob,然后再次发布以表达喜欢 curl/POST ...

curl -X POST  --header "Transfer-Encoding: chunked" --header "Content-Type: audio/ogg; rate=48000"  --data-binary @myaudio.opus "https://localhost${PORT}/audio/upload"

基于this code

我下面的快速代码运行良好,GCS 音频可以正常下载。

app.post("/audio/upload", gcsAudio);

const gcsAudio = (req, res) => {
  const type = req.get('Content-Type');
  let gcsname = 'audio/' +uuidv4() + '.opus';
  const files = myBucket.file(gcsname);
  const stream = files.createWriteStream({
    metadata: {
      contentType: type
    },
    resumable: false
  });

 req
   .pipe(stream)
   .on("error", (err) => {
     restify.InternalServerError(err);
   })
   .on('finish', () => {
     res.json({
      success: true,
      fileUrl: `https://storage.googleapis.com/${_bucket}/${gcsname}`
    })
   });
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多