我正在尝试使用firebase函数和ffmpeg将水印添加到视频中,但我知道正确的代码。我知道ffmpen现在可以在googlecloudenv中使用,所以这种方法应该是可行的。
感谢您的帮助。
const functions = require('firebase-functions');
const { Storage } = require('@google-cloud/storage');
const projectId = 'video-demo-a57fa';
let gcs = new Storage ({
projectId
});
const os =require('os');
const path =require('path');
const spawn = require('child-process-promise').spawn;
exports.addLogo = functions.storage.object().onFinalize(event =>{
const bucket =event.bucket;
const contentType =event.contentType;
const filePath =event.name;
console.log(path.dirname(filePath));
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 };
return destBucket.file(filePath).download({
destination: tmpFilePath
}).then(()=>{
return spawn('ffmpeg', ['-i', tmpFilePath, '-i', './watermark.png', '-filter_complex', '"overlay=x=(main_w-overlay_w)/2:y=(main_h-overlay_h)/2"', tmpFilePath]);
}).then(() => {
console.log(tmpFilePath);
return destBucket.upload(tmpFilePath, {
destination: path.dirname(filePath)+'/resized-' + path.basename(filePath),
metadata: metadata
})
}).catch(err=>{
console.log(err);
});
})