Snippets
Created by
Kees van Bemmel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | /*
* Author: Incentro Cloud
*/
'use strict';
const gcs = require('@google-cloud/storage')();
const path = require('path');
const fs = require('fs');
const ffmpeg = require('fluent-ffmpeg');
const ffmpeg_static = require('ffmpeg-static');
const ffprobe_static = require('ffprobe-static');
// Set the ffmpeg path to use the deployed binaries
const binPath = path.resolve(__dirname, 'ffmpeg');
ffmpeg.setFfmpegPath(ffmpeg_static.path);
ffmpeg.setFfprobePath(ffprobe_static.path);
exports.videoGenerateThumbnail = function videoGenerateThumbnail(event) {
const pubsubMessage = event.data;
const messageData = Buffer.from(pubsubMessage.data, 'base64').toString();
const dataObject = JSON.parse(messageData);
const bucket = dataObject.file.bucket;
const filename = dataObject.file.name;
const assetid = dataObject.assetid;
const file = gcs.bucket(bucket).file(filename);
console.log('Processing file: ' + filename + ' for assetid ' + assetid);
var justFile = path.basename(filename);
var destinationPath = '/tmp/' + justFile;
return Promise.resolve()
.then(() => {
console.log('downloading video file...')
return downloadFile(file, filename, destinationPath);
}).then(() => {
console.log('extracting thumbnail from video')
return generateThumbnail(destinationPath, bucket, filename);
}).catch((err) => {
return Promise.reject(err);
});
};
function generateThumbnail(fileName, thumbnailBucketName, gcsfilename) {
return new Promise((resolve, reject) => {
ffmpeg(fileName)
.on('end', function () {
console.log('extracted thumbnail');
resolve();
})
.screenshots({
timestamps: ['50%'],
filename: 'thumbnail.png',
size: '500x?',
folder: '/tmp/'
});
}).then(() => {
console.log('Uploading thumbnail to GCS: gs://' + thumbnailBucketName + "/" + gcsfilename + "/thumbnail.png");
return uploadFile(gcs.bucket(thumbnailBucketName), '/tmp/thumbnail.png', gcsfilename + "/thumbnail.png");
}).then(() => {
//make file public so the front-end can serve the image
console.log("making thumbnail public");
return gcs.bucket(thumbnailBucketName).file(gcsfilename + "/thumbnail.png").makePublic();
}).then(() => {
fs.unlinkSync('/tmp/thumbnail.png');
console.log('cleanup successful!');
return Promise.resolve();
});
}
function uploadFile(bucket, source, destfilename) {
return new Promise((resolve, reject) => {
var file = bucket.file(destfilename);
fs.createReadStream(source)
.pipe(file.createWriteStream({ validation: false }))
.on('error', function(err) {
console.log("error: " + err);
reject(err);
})
.on('finish', function() {
console.log("done uploading: " + destfilename);
resolve(destfilename);// The file upload is complete.
});
});
}
function downloadFile(file, fileName, destinationPath) {
return new Promise((resolve, reject) => {
var onlyPath = path.dirname(destinationPath);
if (!fs.existsSync(onlyPath)) {
fs.mkdirSync(onlyPath);
}
file.download({
destination: destinationPath
}).then((error) => {
if (error.length > 0) {
reject(error);
} else {
resolve(destinationPath);
}
})
});
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.