javascript - Firebase 的云功能 : completing long processes without touching maximum timeout

标签 javascript firebase ffmpeg google-cloud-functions fluent-ffmpeg

当视频上传到 firebase 存储时,我必须将视频从 webm 转码为 mp4。 I have a code demo here that works , 但如果上传的视频太大,转换完成前 firebase 函数会超时。我知道可以增加函数的超时限制,但这看起来很麻烦,因为我永远无法确认该过程将花费比超时限制更少的时间。

有没有什么方法可以在不增加最大超时限制的情况下阻止 firebase 超时?

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

如果即使使用 firebase 函数完成耗时的过程也不是真正存在的东西,是否有某种方法可以在不影响质量的情况下加快 fluent-ffmpeg 的转换速度? (我意识到这部分有很多问题。如果绝对必要,我计划降低质量,因为将 webms 转换为 mp4 的原因是针对 IOS 设备)

作为引用,这里是我提到的演示的主要部分。正如我之前所说,完整的代码可以是 seen here ,但是复制过来的这部分代码是创建确保转码完成的 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');

(这里有一堆文本解析代码,然后是 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);
        });
      })
    });
  });

最佳答案

Cloud Functions for Firebase 不太适合(也不支持)可能超过最大超时时间的长时间运行的任务。您使用 Cloud Functions 执行非常繁重的计算操作的唯一真正机会是找到一种方法将工作拆分为多个函数调用,然后将所有工作的结果加入最终产品。对于像视频转码这样的事情,这听起来是一项非常困难的任务。

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

关于javascript - Firebase 的云功能 : completing long processes without touching maximum timeout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44469537/

相关文章:

javascript - 如何使用 javascript 将小时转换为 12 HRS/24 HRS 格式

javascript - 创建 Firebase 用户并同时将数据添加到数据库中

javascript - 用于在 enter() 上附加分组元素的 d3 习惯用法?

java - 时间戳方法 'SimpleDateFormat' 未从数据库检索

android - 如何使用 ionic 3 和 Firebase Cloud Messaging 从通知栏打开特定页面?

php - 对单页 Web 应用程序架构的愚蠢但明智的关注

reactjs - Firebase v9 React JS IndexOf 不是函数

batch-file - 有没有更有效的方法可以通过 ffmpeg 批量添加水印和加入视频?

ffmpeg - 压缩算法能否同时无损和有损?

video - 使用 ffmpeg 从按数字顺序排序的图像创建视频