Firebase的云功能:完成长流程而不会触及最大超时

Sco*_*ing 8 javascript ffmpeg firebase fluent-ffmpeg google-cloud-functions

当他们上传到firebase存储时,我必须将视频从webm转码到mp4.我在这里有一个代码演示,但如果上传的视频太大,firebase功能将在转换完成之前超时.我知道可以增加该功能的超时限制,但这看起来很麻烦,因为我无法确认该过程所需的时间比超时限制少.

有没有办法阻止firebase超时,而不仅仅是增加最大超时限制?

如果没有,有没有办法完成耗时的过程(如视频转换),同时仍然让每个进程开始使用firebase函数触发器?

如果即使使用firebase函数完成耗时的过程也不是真正存在的东西,有没有办法加速fluent-ffmpeg的转换而不会影响质量那么多?(我意识到这一部分有很多问题.如果我绝对必须,我打算降低质量,因为网络转换为mp4的原因是针对IOS设备)

作为参考,这是我提到的演示的主要部分.正如我之前所说的,这里可以看到完整的代码,但复制过的代码的这一部分是创建Promise以确保转码完成的部分.完整的代码只有70行,所以如果需要它应该相对容易.

const functions = require('firebase-functions');
const mkdirp = require('mkdirp-promise');
const gcs = require('@google-cloud/storage')();
const Promise = require('bluebird');
const ffmpeg = require('fluent-ffmpeg');
const ffmpeg_static = require('ffmpeg-static');
Run Code Online (Sandbox Code Playgroud)

(这里有一堆文本解析代码,然后是onChange事件中的下一个代码块)

function promisifyCommand (command) {
    return new Promise( (cb) => {
        command
        .on( 'end',   ()      => { cb(null)  } )
        .on( 'error', (error) => { cb(error) } )
        .run();
    })
}
return mkdirp(tempLocalDir).then(() => {
    console.log('Directory Created')
    //Download item from bucket
    const bucket = gcs.bucket(object.bucket);
    return bucket.file(filePath).download({destination: tempLocalFile}).then(() => {
      console.log('file downloaded to convert. Location:', tempLocalFile)
      cmd = ffmpeg({source:tempLocalFile})
               .setFfmpegPath(ffmpeg_static.path)
               .inputFormat(fileExtension)
               .output(tempLocalMP4File)
      cmd = promisifyCommand(cmd)
      return cmd.then(() => {
        //Getting here takes forever, because video transcoding takes forever!
        console.log('mp4 created at ', tempLocalMP4File)
        return bucket.upload(tempLocalMP4File, {
            destination: MP4FilePath
        }).then(() => {
          console.log('mp4 uploaded at', filePath);
        });
      })
    });
  });
Run Code Online (Sandbox Code Playgroud)

Dou*_*son 6

Firebase的Cloud Functions不适用于(也不支持)长时间运行的任务,这些任务可能超过最大超时时间。你只在真正使用的机会只有云功能进行非常繁重的计算操作是要找到一种方法来拆分工作到多个函数调用,然后再加入所有这些工作的成果转化为最终产品。对于像视频转码这样的事情,这听起来像是一个非常艰巨的任务。

相反,请考虑使用函数来触发App EngineCompute Engine中的长时间运行的任务。

  • “用于Firebase的Cloud Functions不太适合(且不受支持)长时间运行的任务”,该文档将为我节省数周的时间。现在正在调查App Engine。感谢:D (2认同)