Skip to content
Browse files

Merge pull request #58 from tagedieb/master

Added option to set screenshot filenames and timemarks to take percent values (partially solves #57)
  • Loading branch information...
2 parents aecd566 + c5e7191 commit af96947f80e0b87d325dc444a2c2df9a2a4717cf @schaermu schaermu committed
Showing with 61 additions and 6 deletions.
  1. +25 −1 README.md
  2. +36 −5 lib/processor.js
View
26 README.md
@@ -99,7 +99,7 @@ One pretty neat feature is the ability of fluent-ffmpeg to generate any amount o
console.log('screenshots were saved')
});
-For more control, you can also set the timemarks for taking screenshots yourself (timemarks are always in seconds):
+For more control, you can also set the timemarks for taking screenshots yourself. Timemarks are percent `50%` or otherwise seconds.
var ffmpeg = require('fluent-ffmpeg');
@@ -111,6 +111,30 @@ For more control, you can also set the timemarks for taking screenshots yourself
}, '/path/to/thumbnail/folder', function(err) {
console.log('screenshots were saved')
});
+
+You can set the screenshots filename dynamically using following format characters:
+
+* `%s` - offset in seconds
+* `%w` - screenshot width
+* `%h` - screenshot height
+* `%r` - screenshot resolution ( widthxheight )
+* `%f` - input filename
+* `%b` - input basename ( filename w/o extension )
+* `%i` - number of screenshot in timemark array ( can be zeropadded by using it like `%000i` )
+
+If multiple timemarks are given and no `%i` format character is found in filename `_%i` will be added to the end of the given filename.
+
+ var ffmpeg = require('fluent-ffmpeg');
+
+ var proc = new ffmpeg({ source: '/path/to/your_movie.avi' })
+ .withSize('150x100')
+ .takeScreenshots({
+ count: 2,
+ timemarks: [ '50%', '75%' ],
+ filename: '%b_screenshot_%w_%i'
+ }, '/path/to/thumbnail/folder', function(err) {
+ console.log('screenshots were saved')
+ });
### Reading video metadata
Using a seperate object, you are able to access various metadata of your video file.
View
41 lib/processor.js
@@ -194,6 +194,26 @@ exports = module.exports = function Processor(command) {
};
this.takeScreenshots = function(config, folder, callback) {
+
+ function _zeroPad(number, len) {
+ return new Array(len-String(number).length+2).join('0')+number;
+ }
+
+ function _renderOutputName(j, offset) {
+ var result = filename;
+ if( /%0*i/.test(result) ) {
+ var numlen = String(result.match(/%(0*)i/)[1]).length;
+ result = result.replace(/%0*i/, _zeroPad(j, numlen));
+ }
+ result = result.replace('%s', offset);
+ result = result.replace('%w', self.options.video.width);
+ result = result.replace('%h', self.options.video.height);
+ result = result.replace('%r', self.options.video.width+'x'+self.options.video.height);
+ result = result.replace('%f', self.options.inputfile);
+ result = result.replace('%b', self.options.inputfile.substr(0,self.options.inputfile.lastIndexOf('.')));
+ return result;
+ }
+
function _screenShotInternal(callback) {
// get correct dimensions
self._prepare(function(err, meta) {
@@ -201,6 +221,10 @@ exports = module.exports = function Processor(command) {
// check if all timemarks are inside duration
if (timemarks !== null) {
for (var i = 0; i < timemarks.length; i++) {
+ /* convert percentage to seconds */
+ if( timemarks[i].indexOf('%') > 0 ) {
+ timemarks[i] = (parseInt(timemarks[i], 10) / 100) * meta.durationsec;
+ }
if (parseInt(timemarks[i], 10) > (meta.durationsec * 0.9)) {
// remove timemark from array
timemarks.splice(i, 1);
@@ -233,10 +257,9 @@ exports = module.exports = function Processor(command) {
} else {
offset = secondOffset * j;
}
- var target = self.escapedPath(folder + '/tn_' + offset + 's.jpg');
+ var target = self.escapedPath(folder + '/' + _renderOutputName(j, offset) + '.jpg');
var input = self.escapedPath(self.options.inputfile);
-
-
+
// build screenshot command
var tnArgs = [
'-ss', offset,
@@ -270,7 +293,7 @@ exports = module.exports = function Processor(command) {
});
}
- var timemarks, screenshotcount;
+ var timemarks, screenshotcount, filename;
if (typeof config === 'object') {
// use json object as config
if (config.count) {
@@ -288,7 +311,15 @@ exports = module.exports = function Processor(command) {
this.options.logger.warn("set size of thumbnails using 'withSize' method");
callback(new Error("set size of thumbnails using 'withSize' method"));
}
-
+
+ filename = config.filename || 'tn_%ss';
+ if( filename.indexOf('%i') === -1 && timemarks.length > 1 ) {
+ // if there are multiple timemarks but no %i in filename add one
+ // so we won't overwrite the same thumbnail with each timemark
+ filename += '_%i';
+ }
+ folder = folder || '.';
+
var self = this;
// WORKAROUND: exists will be moved from path to fs with node v0.7

0 comments on commit af96947

Please sign in to comment.
Something went wrong with that request. Please try again.