Upon uploading videos to firebase storage, I am faced with the task of transcoding webm files to mp4 format. While I have a functional code demo available here, the issue arises when dealing with large video files. The conversion process often exceeds the timeout limit set by firebase functions, resulting in an incomplete transcode. Increasing the timeout limit is an option, but it feels like a temporary fix since there's no guarantee that the process will always finish within the extended timeframe.
Is there a way to prevent firebase from timing out without simply extending the maximum timeout limit?
If not, how can I efficiently handle time-consuming tasks such as video conversion while utilizing firebase function triggers?
In the event that completing demanding processes through firebase functions is not viable, is there a method to accelerate fluent-ffmpeg conversion speeds without significantly compromising quality? (I understand this might be a challenging request, and I'm willing to adjust quality levels if necessary, especially for compatibility with IOS devices)
For easier reference, here is a key excerpt from the mentioned demo. Although the complete code can be viewed here, the following section highlights the Promise responsible for ensuring successful video transcoding. The full script spans around 70 lines, making it relatively manageable for review if needed.
const functions = require('firebase-functions');
const mkdirp = require('mkdirp-promise');
const gcs = require('@google-cloud/storage')();
const Promise = require('bluebird');
const ffmpeg = require('fluent-ffmpeg');
const ffmpeg_static = require('ffmpeg-static');
(This is followed by text parsing routines and the below onChange event snippet)
function promisifyCommand (command) {
return new Promise( (cb) => {
command
.on( 'end', () => { cb(null) } )
.on( 'error', (error) => { cb(error) } )
.run();
})
}
return mkdirp(tempLocalDir).then(() => {
console.log('Directory Created')
//Download item from bucket
const bucket = gcs.bucket(object.bucket);
return bucket.file(filePath).download({destination: tempLocalFile}).then(() => {
console.log('file downloaded to convert. Location:', tempLocalFile)
cmd = ffmpeg({source:tempLocalFile})
.setFfmpegPath(ffmpeg_static.path)
.inputFormat(fileExtension)
.output(tempLocalMP4File)
cmd = promisifyCommand(cmd)
return cmd.then(() => {
//The delay occurs here due to the lengthy video transcoding process!
console.log('mp4 created at ', tempLocalMP4File)
return bucket.upload(tempLocalMP4File, {
destination: MP4FilePath
}).then(() => {
console.log('mp4 uploaded at', filePath);
});
})
});
});