微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Firebase 函数总是在大文件上超时?

如何解决Firebase 函数总是在大文件上超时?

我创建了一个 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
        })
    });

})

解决方法

云函数的执行时间有限,最多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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。