【问题标题】:Firebase function always timeout on large files?Firebase 函数总是在大文件上超时?
【发布时间】:2021-01-10 22:41:14
【问题描述】:

我创建了一个 firebase 函数,当视频上传到 firebase 存储时触发该函数,并通过使用 ffmpeg 为其添加水印,它适用于小视频尺寸,但它总是在大视频尺寸下超时。知道如何克服这些限制

const functions = require('firebase-functions');
const { Storage, Bucket } = require('@google-cloud/storage');
const projectId = 'video-sharing-a57fa';
const admin = require('firebase-admin');
admin.initializeApp();

let gcs = new Storage({
    projectId
});
const os = require('os');
const path = require('path');
const spawn = require('child-process-promise').spawn;


exports.addLogo = functions.runWith({ memory: '4GB', timeoutSeconds: 540 }).storage.object().onFinalize(async event => {

const bucket = event.bucket;
const contentType = event.contentType;
const filePath = event.name;
console.log('File change detected, function execution started');
if (path.basename(filePath).startsWith('resized-')) {
    console.log('We already renamed that file!');
    return;
}
const destBucket = gcs.bucket(bucket);
const tmpFilePath = path.join(os.tmpdir(), path.basename(filePath));
const metadata = { contentType: contentType };
const tmpLogoPath = path.join(os.tmpdir(), 'watermark.png');
await destBucket.file('watermark.png').download({
    destination: tmpLogoPath
})

const newPath = path.join(os.tmpdir(), 'output.mp4')

return destBucket.file(filePath).download({
        destination: tmpFilePath
    }).then(() => {
        console.log('entered spawn');
        var str = "overlay=10:10"
        return spawn('ffmpeg', ['-i', tmpFilePath, '-i', tmpLogoPath, '-filter_complex', str, newPath]);
    }).then(() => {
        console.log('chaning the name');
        return destBucket.upload(newPath, {
            destination: path.dirname(filePath) + '/resized-' + path.basename(filePath),
            metadata: metadata
        })
    });

})

【问题讨论】:

    标签: node.js firebase google-app-engine google-cloud-functions google-cloud-storage


    【解决方案1】:

    云函数的执行时间有限,限制为 最多 9 分钟。更多信息here。最有可能的问题是ffmpeg 没有及时添加水印。你的行动应该是:

    1. 检查函数的日志,确认这正是错误firebase functions:log --only <FUNCTION_NAME>
    2. 考虑使用不同的架构选项来处理非常大的文件:
      一种。限制ffmpeg进程的数据量,例如与-ss 50 -t 10。在这种情况下,将有以下架构:a)一个读取文件并将它们放入队列的函数,b)一个读取文件大小并将数据放入另一个队列的函数,例如{name: "file1.mp4", start: 10, duration: 15}
      湾。使用按需容器,例如 Cloud Run
      C。如果您经常处理某些文件,请使用 App Engine

    【讨论】:

      猜你喜欢
      • 2019-05-01
      • 2017-07-31
      • 2020-06-09
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 2021-03-09
      • 2011-11-13
      • 1970-01-01
      相关资源
      最近更新 更多