Can you call out to FFMPEG in a Firebase Cloud Function

前端 未结 6 1587
感动是毒
感动是毒 2020-11-27 16:12

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

Is i

6条回答
  •  星月不相逢
    2020-11-27 16:45

    ffmpeg is not preinstalled (pretty much just ImageMagick); to see exactly what's installed check out the Dockerfile here: https://github.com/GoogleCloudPlatform/nodejs-docker/blob/master/runtime-image/Dockerfile.

    However, you can upload arbitrary binaries when you upload your code using gcloud beta functions deploy because everything in the current directory (except node_modules) is uploaded.

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

    Option 1: use ffmpeg-static npm module

    ffmpeg-static is an npm module that builds the correct ffmpeg binary based on the current system during npm install. Since Cloud Functions builds your code in the cloud, it'll build the correct ffmpeg binary.

    https://github.com/eugeneware/ffmpeg-static

    You can see it in action in the Cloud Functions for Firebase examples repo.

    const ffmpeg = require('fluent-ffmpeg');
    const ffmpeg_static = require('ffmpeg-static');
    
    var cmd = ffmpeg('/tmp/video.avi')
      .setFfmpegPath(ffmpeg_static.path)
      .videoBitrate(1024)
      .videoCodec('divx')
      .format('avi')
      .on('end', () => {
        // ...
      })
      .on('error', err => {
        console.error(err);
      })
      .save('/tmp/file-out.avi');

    (Thanks Daniel Lessa for pointing out this module in his answer.)

    Option 2: upload your own binary

    You could include an ffmpeg binary as part of the upload and then run a shell command using something like child_process.exec. You'll need the ffmpeg binary that's compiled for the target platform (Debian/jessie).

    File listing with pre-compiled ffmpeg binary

    ./
    ../
    index.js
    ffmpeg
    

    Then run for example gcloud beta functions deploy myFunc --trigger-http

    index.js

    var exec = require('child_process').exec;
    var cmd = 'ffmpeg -i /tmp/myvideo.mp4 /tmp/image-%d.jpg';
    
    exec(cmd, function(error, stdout, stderr) {
      // command output is in stdout
    });
    

提交回复
热议问题