Created
March 12, 2015 21:39
-
-
Save gabrielstuff/905259147cfa32eaddc2 to your computer and use it in GitHub Desktop.
FSCollection.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function generatePreview(fileObj, readStream, writeStream) { | |
fileObj.name('preview.jpg', { | |
store: 'thumbs' | |
}); | |
fileObj.extension('jpg', { | |
store: 'thumbs' | |
}); | |
fileObj.type('image/jpg', { | |
store: 'thumbs' | |
}); | |
fileObj.updatedAt(new Date, { | |
store: 'thumbs' | |
}); | |
mkdirp.sync(pathHelper.join(process.env.PWD, 'temp')); | |
var shortId = shortner.generate(), | |
filename = pathHelper.join(process.env.PWD, 'temp', 'temp' + shortId + '.mp4'), | |
tempfile = fs.createWriteStream(filename); | |
tempfile.on('error', function(err) { | |
console.log('error on tempfile: ', err); | |
}); | |
readStream.pipe(tempfile); | |
tempfile.on('close', function() { | |
console.log('begin thumbnails generation...'); | |
var ffmpeg = child_process.spawn("ffmpeg", [ | |
'-i', filename, // path | |
'-vf', 'scale=640:360', // bitrate to 64k | |
'-r', '1', | |
'-frames:v', '1', | |
'-ss', Meteor.settings.video.previewtime, | |
'-f', 'mjpeg', | |
'pipe:1' | |
]); | |
ffmpeg.stderr.on('data', function(data) { | |
//Uncomment to see what happen ! | |
//console.log('videoCollection, ffmpeg, stderr: ' + data); | |
}) | |
ffmpeg.on('close', function(code) { | |
if (code == 0) { | |
console.log('Preview generated.'); | |
} else { | |
console.log('An error occured while generating the preview.'); | |
} | |
fs.unlinkSync(filename); | |
}); | |
writeStream.on('error', function(err) { | |
console.log('videoCollection, stream err: ', err); | |
}); | |
ffmpeg.stdout.pipe(writeStream); | |
}); | |
} | |
new FS.Store.FileSystem("thumbs", { | |
beforeWrite: function(fileObj) { | |
if (fileObj.original.type.indexOf('video/') < 0) { | |
// We return an object, which will change the | |
// filename extension and type for this store only. | |
return { | |
extension: 'png', | |
type: 'image/png' | |
}; | |
} | |
}, | |
transformWrite: function(fileObj, readStream, writeStream) { | |
if (fileObj.original.type.indexOf('video/') < 0) { | |
generateThumb(fileObj, readStream, writeStream); | |
} else if (fileObj.original.type.indexOf('video/') >= 0) { | |
generatePreview(fileObj, readStream, writeStream); | |
} | |
}, | |
maxTries: 1 | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment