简体   繁体   English

将流从 Fluent-ffmpeg 传递到 Google Cloud Storage

[英]Passing streams from Fluent-ffmpeg to Google Cloud Storage

Is there a way to pass a stream from Fluent-mmpeg to Google Cloud Storage?有没有办法将 stream 从 Fluent-mmpeg 传递到 Google Cloud Storage? I'm trying to allow the user to upload any kind of media (audio or video), and I want to convert it to flac before uploading it to GCS.我正在尝试允许用户上传任何类型的媒体(音频或视频),并且我想在将其上传到 GCS 之前将其转换为 flac。

I'm using a few middlewares on my route, such as:我在路线上使用了一些中间件,例如:


routes.post(
  '/upload',
  multer.single('audio'),
  ConvertController.convert,
  UploadController.upload,
  FileController.save,
  (req, res, next) => res.send('ok')
);

I was able to stream from Multer to Fluent-mmpeg and save to a file using this code on ConvertController:我能够从 Multer 到 Fluent-mmpeg 的 stream 并在 ConvertController 上使用以下代码保存到文件中:

async convert(req, res, next) {
    ffmpeg(streamifier.createReadStream(req.file.buffer))
      .format('flac')
      .output('outputfile.flac')
      .audioChannels(1)
      .on('progress', function(progress) {
        console.log(progress);
      })
      .run();
  }

But I would like to use.pipe() to pass it to UploadController, where I would then upload to GCS:但我想使用 .pipe() 将其传递给 UploadController,然后我将上传到 GCS:

class UploadController {
  async upload(req, res, next) {
    const gcsHelpers = require('../helpers/google-cloud-storage');
    const { storage } = gcsHelpers;

    const DEFAULT_BUCKET_NAME = 'my-bucket-name';

    const bucketName = DEFAULT_BUCKET_NAME;
    const bucket = storage.bucket(bucketName);
    const fileName = `test.flac`;
    const newFile = bucket.file(fileName);

    newFile.createWriteStream({
      metadata: {
        contentType: file.mimetype
      }
    })

    file.on('error', err => {
      throw err;
    });

    file.on('finish', () => console.log('finished'));
  }

The problem is that I cannot find anywhere explaining how I can pass down a stream to the next middleware.问题是我找不到任何地方解释如何将 stream 传递给下一个中间件。

Is it possible?可能吗?

Attach your ffmpeg stream to the req object, and then you can pass your ffmpeg stream to the next middleware using the next() . Attach your ffmpeg stream to the req object, and then you can pass your ffmpeg stream to the next middleware using the next() . Then you can pipe the stream to your GCS file.然后您可以将 pipe stream 到您的 GCS 文件中。

Something like this:像这样的东西:

// ConvertController
req.ffpmegStream = ffmpeg(streamifier.createReadStream(req.file.buffer))
  .on('finish', next)

// UploadController
req.ffmpegStream.pipe(newFile.createWriteStream())

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM