Skip to content

Instantly share code, notes, and snippets.

@casajarm
Last active February 24, 2021 22:27
Show Gist options
  • Save casajarm/a33d5f003665fd2cb036e24a18a3ab2f to your computer and use it in GitHub Desktop.
Save casajarm/a33d5f003665fd2cb036e24a18a3ab2f to your computer and use it in GitHub Desktop.
Using FFMPEG to concatenate files on Google Cloud Functions via NodeJS .. send it array of filenames and the extension type and it returns a promise that resolves to the final file with full path
const os = require("os");
const ffmpegPath = require("@ffmpeg-installer/ffmpeg").path;
const fluent_ffmpeg = require("fluent-ffmpeg");
const ffprobePath = require('@ffprobe-installer/ffprobe').path;
const path = require('path');
fluent_ffmpeg.setFfprobePath(ffprobePath); //this was key for me on GCF
fluent_ffmpeg.setFfmpegPath(ffmpegPath);
/**
*
* @param {Array} filearr array of file names with path
* @param {String} fileType file extension
*/
function concatenate(filearr, fileType) {
const tmpdir = os.tmpdir();
let mpeg = new fluent_ffmpeg();
let outFileName = path.join(tmpdir, 'finalFile.' + fileType);
filearr.forEach(function (fileName) {
mpeg.addInput(fileName);
});
const promise = new Promise((resolve, reject) => {
mpeg.mergeToFile(outFileName, tmpdir)
.on('error', function (err, stdout, stderr) {
console.log(err.message);
console.log("stdout:\n" + stdout);
console.log("stderr:\n" + stderr);
reject;
})
.on('end', function () {
console.log('Finished concatenating!');
resolve(outFileName);
});
});
return promise;
}
exports.concatenate = concatenate;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment