Permalink
Browse files

added async.min.js (caolan/async), fixed bug in screenshot generation

  • Loading branch information...
schaermu committed Apr 19, 2011
1 parent 0d0375d commit e9f433f891db539542bf0d7e125ceda086e57e33
Showing with 46 additions and 32 deletions.
  1. +43 −30 lib/fluent-ffmpeg.js
  2. +1 −0 support/async.min.js
  3. +2 −2 test/conversion.test.js
View
@@ -1,16 +1,21 @@
var fs = require('fs'),
path = require('path'),
+ async = require('../support/async.min.js'),
exec = require('child_process').exec,
spawn = require('child_process').spawn;
function FfmpegProcessor(source) {
// check if argument is a stream
var srcstream, srcfile;
if (typeof source === 'object') {
- // streaming mode
- source.pause();
- srcstream = source;
- srcfile = source.path;
+ if (source.readable) {
+ // streaming mode
+ source.pause();
+ srcstream = source;
+ srcfile = source.path;
+ } else {
+ throw new Error('Source is not a ReadableStream')
+ }
} else {
// file mode
srcfile = source;
@@ -153,33 +158,41 @@ function FfmpegProcessor(source) {
self._calculateDimensions(function(err) {
// get positions for screenshots (using duration of file minus 10% to remove fade-in/fade-out)
var secondOffset = Math.round((meta.durationsec * 0.9) / screenshotcount);
+ var donecount = 0;
+ var series = [];
+
+ var i = 1;
- for (var i = 1; i <= screenshotcount; i++) {
- var offset = secondOffset * i;
- var target = folder + '/tn_' + offset + 's.jpg';
-
- // build screenshot command
- var tnArgs = [
- '-ss', offset,
- '-i', options.inputfile,
- '-vcodec', 'mjpeg',
- '-vframes', '1',
- '-an',
- '-f', 'rawvideo',
- '-s', options.video.size,
- target
- ];
-
- // start thumbnail generation
- var error = '';
- var ffmpegProc = exec('ffmpeg ' + tnArgs.join(' '), function(err, stdout, stderr) {
- if (error !== null) {
- error += err;
- }
- });
- }
- callback(error);
- });
+ // use async helper function to generate all screenshots and
+ // fire callback just once after work is done
+ async.until(
+ function() {
+ return i > screenshotcount;
+ },
+ function(taskcallback) {
+ var offset = secondOffset * i;
+ var target = folder + '/tn_' + offset + 's.jpg';
+
+ // build screenshot command
+ var tnArgs = [
+ '-ss', offset,
+ '-i', options.inputfile,
+ '-vcodec', 'mjpeg',
+ '-vframes', '1',
+ '-an',
+ '-f', 'rawvideo',
+ '-s', options.video.size,
+ target
+ ];
+
+ i++;
+ exec('ffmpeg ' + tnArgs.join(' '), taskcallback);
+ },
+ function(err) {
+ callback(err);
+ }
+ );
+ });
} else {
throw new Error("meta data contains no duration, aborting screenshot creation")
}
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -98,7 +98,7 @@ module.exports = testCase({
var args = new ffmpeg(self.testfile)
.withSize('150x?')
.takeScreenshots(2, testFolder, function(err) {
- test.ok(!err);
+ test.ok(err == null);
fs.readdir(testFolder, function(err, files) {
var tnCount = 0;
files.forEach(function(file) {
@@ -107,7 +107,7 @@ module.exports = testCase({
fs.unlinkSync(testFolder + '/' + file);
}
});
- test.ok(tnCount == 2);
+ test.ok(tnCount === 2);
// remove folder
fs.rmdirSync(testFolder);
test.done();

0 comments on commit e9f433f

Please sign in to comment.