Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

node.js - How to add a watermark to video on firebase storage using firebase functions and ffmpeg

I am trying to add a watermark to a video using firebase functions and ffmpeg, but I do know the right code for it. I know is that ffmpen is now available in google cloud env so this approach should work. But I think that the problem is that ffmpeg is not able to locate watermark.png, where should I put this image file? Any help is appreciated.

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);
    });
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...