Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you call out to FFMPEG in a Firebase Cloud Function

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

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.

like image 898
Dave Parks Avatar asked Mar 13 '17 21:03

Dave Parks


People also ask

Can you code in firebase?

Cloud Functions is yet another a Google Cloud product that works well with other Firebase and Cloud products. Using the Firebase SDKs for Cloud Functions, you can write and deploy code, running on Google “serverless” infrastructure, that automatically responds to events coming from other Firebase products.


2 Answers

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.

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

Option 1: use ffmpeg-fluent npm module

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

Full code on GitHub

Option 2: invoke the ffmpeg binary directly

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

Full code on GitHub

Option 3: upload your own binary

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).

File listing with pre-compiled ffmpeg binary

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

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

like image 75
Bret McGowen Avatar answered Sep 18 '22 12:09

Bret McGowen


While you technically can run FFMPEG on a Firebase Functions instance, you will quickly hit the small quota limits.

As per this answer, you can instead use Functions to trigger a request to GCP's more powerful App Engine or Compute Engine services. The App Engine process can grab the file from the same bucket, handle the transcoding, and upload the finished file back to the bucket. If you check the other answers at the link, one user posted a sample repo that does just that.

like image 23
ajabeckett Avatar answered Sep 17 '22 12:09

ajabeckett