簡體   English   中英

將流從 Fluent-ffmpeg 傳遞到 Google Cloud Storage

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

有沒有辦法將 stream 從 Fluent-mmpeg 傳遞到 Google Cloud Storage? 我正在嘗試允許用戶上傳任何類型的媒體(音頻或視頻),並且我想在將其上傳到 GCS 之前將其轉換為 flac。

我在路線上使用了一些中間件,例如:


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

我能夠從 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();
  }

但我想使用 .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'));
  }

問題是我找不到任何地方解釋如何將 stream 傳遞給下一個中間件。

可能嗎?

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

像這樣的東西:

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

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

參考:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM