更新:ffmpeg 现在已预安装在 Cloud Functions 环境中。如需预装软件包的完整列表,请查看https://cloud.google.com/functions/docs/reference/system-packages。
注意:您只有/tmp/ 的磁盘写入访问权限。
选项 1:使用 ffmpeg-fluent npm 模块
这个模块通过一个易于使用的 Node.js 模块抽象了 ffmpeg 命令行选项。
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');
});
Full code on GitHub
选项 2:直接调用 ffmpeg 二进制文件
由于ffmpeg 已经安装,您可以通过shell 进程调用二进制文件及其命令行选项。
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);
});
Full code on GitHub
选项 3:上传您自己的二进制文件
如果您需要特定版本的 ffmpeg,您可以将 ffmpeg 二进制文件作为上传的一部分,然后使用 child_process.exec 之类的内容运行 shell 命令。您需要为目标平台 (Ubuntu) 编译的 ffmpeg 二进制文件。
预编译的 ffmpeg 二进制文件列表
./
../
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);
});
我已经加入了two full working examples on GitHub。这些示例适用于 Google Cloud Functions(不是专门针对 Firebase 的 Cloud Functions)。