你能在 Firebase 云函数中调用 FFMPEG 吗 [英] Can you call out to FFMPEG in a Firebase Cloud Function

查看:28
本文介绍了你能在 Firebase 云函数中调用 FFMPEG 吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Firebase 云函数文档,您可以在云函数中利用 ImageMagick:https://firebase.google.com/docs/functions/use-cases

Per the Firebase Cloud Functions documentation, you can leverage ImageMagick from within a cloud function: https://firebase.google.com/docs/functions/use-cases

是否可以做类似的事情,但调用 FFMPEG 而不是 ImageMagick?虽然缩略图很棒,但我还希望能够将传入的图像附加到存储在 Firebase 存储中的视频文件中.

Is it possible do something similar but call out to FFMPEG instead of ImageMagick? While thumbnailing images is great, I'd also like the capability to append incoming images to a video file stored out on Firebase Storage.

推荐答案

更新:ffmpeg 现在已预安装在 Cloud Functions 环境中.如需预装软件包的完整列表,请查看 https://cloud.google.com/functions/docs/reference/system-packages.

Update: ffmpeg is now preinstalled in the Cloud Functions environment. For a complete list of preinstalled packages, check out https://cloud.google.com/functions/docs/reference/system-packages.

注意:您只有 /tmp/ 的磁盘访问权限.

Note: you only have disk write access at /tmp/.

该模块通过易于使用的 Node.js 模块抽象出 ffmpeg 命令行选项.

This module abstracts the ffmpeg command line options with an easy to use Node.js module.

const ffmpeg = require('fluent-ffmpeg');

let cmd = ffmpeg('example.mp4')
    .clone()
    .size('300x300')
    .save('/tmp/smaller-file.mp4')
    .on('end', () => {
      // Finished processing the video.
      console.log('Done');

      // E.g. return the resized video:
      res.sendFile('/tmp/smaller-file.mp4');
    });

GitHub 上的完整代码

因为 ffmpeg 已经安装,您可以通过 shell 进程调用二进制文件及其命令行选项.

Because ffmpeg is already installed, you can invoke the binary and its command line options via a shell process.

const { exec } = require("child_process");

exec("ffmpeg -i example.mp4", (error, stdout, stderr) => {
  //ffmpeg logs to stderr, but typically output is in stdout.
  console.log(stderr);
});

GitHub 上的完整代码

如果您需要特定版本的 ffmpeg,您可以将 ffmpeg 二进制文件作为上传的一部分,然后使用 child_process.exec.您需要一个为目标平台 (Ubuntu) 编译的 ffmpeg 二进制文件.

If you need a specific version of ffmpeg, you can include an ffmpeg binary as part of the upload and then run a shell command using something like child_process.exec. You'll need an ffmpeg binary that's compiled for the target platform (Ubuntu).

./
../
index.js
ffmpeg

index.js

const { exec } = require("child_process");

exec("ffmpeg -i example.mp4", (error, stdout, stderr) => {
  //ffmpeg logs to stderr, but typically output is in stdout.
  console.log(stderr);
});

我在 GitHub 上包含了两个完整的工作示例.这些示例适用于 Google Cloud Functions(并非专门用于 Firebase 的 Cloud Functions).

I've included two full working examples on GitHub. The examples are for Google Cloud Functions (not specifically Cloud Functions for Firebase).

这篇关于你能在 Firebase 云函数中调用 FFMPEG 吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆