Permalink
Browse files

added conversion tests, fixed some minor bugs

  • Loading branch information...
1 parent c9727f9 commit 0d0375d2c591502ebabbae27a808f3ad90b20a13 @schaermu schaermu committed Apr 19, 2011
Showing with 168 additions and 39 deletions.
  1. +48 −37 lib/fluent-ffmpeg.js
  2. +1 −1 lib/presets/podcast.js
  3. +1 −1 test/args.test.js
  4. +118 −0 test/conversion.test.js
View
@@ -133,47 +133,58 @@ function FfmpegProcessor(source) {
// check target folder
if (!path.existsSync(folder)) {
- fs.mkdirSync(folder, '0755');
+ fs.mkdir(folder, '0755', function(err) {
+ if (!err) {
+ callback(err);
+ } else {
+ _screenShotInternal(callback);
+ }
+ });
+ } else {
+ _screenShotInternal(callback);
}
// read metadata from file
var self = this;
- exports.Metadata.get(options.inputfile, function(meta, err) {
- if (meta.durationsec) {
- // get correct dimensions
- 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);
- 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
- ];
+ function _screenShotInternal(callback) {
+ exports.Metadata.get(options.inputfile, function(meta, err) {
+ if (meta.durationsec) {
+ // get correct dimensions
+ 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);
- // start thumbnail generation
- var error = '';
- var ffmpegProc = exec('ffmpeg ' + tnArgs.join(' '), function(err, stdout, stderr) {
- if (error !== null) {
- error += err;
- }
- });
- }
- callback(error);
- });
- } else {
- throw new Error("meta data contains no duration, aborting screenshot creation")
- }
- });
+ 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);
+ });
+ } else {
+ throw new Error("meta data contains no duration, aborting screenshot creation")
+ }
+ });
+ }
} catch (err) {
callback(err);
}
@@ -189,7 +200,7 @@ function FfmpegProcessor(source) {
this._calculateDimensions(function(err) {
if (err) {
callback(null, null, err);
- } else {
+ } else {
var args = self._buildFfmpegArgs(false);
// start conversion of file using spawn
View
@@ -8,7 +8,7 @@ exports.load = function(ffmpeg) {
.withAudioCodec('libfaac')
.withAudioChannels(1)
.addOptions(['-flags', '+loop', '-cmp', '+chroma', '-partitions','+parti4x4+partp8x8+partb8x8', '-flags2',
- '+mixed_refs', '-me umh', '-subq 5', '-bufsize 2M', '-rc_eq \'blurCplx^(1-qComp)\'',
+ '+mixed_refs', '-me_method umh', '-subq 5', '-bufsize 2M', '-rc_eq \'blurCplx^(1-qComp)\'',
'-qcomp 0.6', '-qmin 10', '-qmax 51', '-qdiff 4', '-level 13' ]);
return ffmpeg;
};
View
@@ -4,7 +4,7 @@ var ffmpeg = require('../lib/fluent-ffmpeg'),
exec = require('child_process').exec;
module.exports = testCase({
- setUp: function (callback) {
+ setUp: function(callback) {
// check for ffmpeg installation
this.testfile = __dirname + '/assets/testvideo.avi';
var self = this;
View
@@ -0,0 +1,118 @@
+var ffmpeg = require('../lib/fluent-ffmpeg'),
+ path = require('path'),
+ fs = require('fs'),
+ testCase = require('nodeunit').testCase,
+ exec = require('child_process').exec;
+
+module.exports = testCase({
+ setUp: function(callback) {
+ // check for ffmpeg installation
+ this.testfile = __dirname + '/assets/testvideo.avi';
+ var self = this;
+ exec('which ffmpeg', function(err, stdout, stderr) {
+ if (stdout != '') {
+ // check if file exists
+ path.exists(self.testfile, function(exists) {
+ if (exists) {
+ callback();
+ } else {
+ callback(new Error('test video file does not exist, check path (' + self.testfile + ')'));
+ }
+ });
+ } else {
+ callback(new Error('cannot run test without ffmpeg installed, aborting test...'));
+ }
+ });
+ },
+ testConvertToFile: function(test) {
+ test.expect(5);
+ var testFile = __dirname + '/assets/testConvertToFile.flv';
+ var args = new ffmpeg(this.testfile)
+ .usingPreset('flashvideo')
+ .saveToFile(testFile, function(stdout, stderr, err) {
+ test.ok(!err);
+ path.exists(testFile, function(exist) {
+ test.ok(exist);
+ // check filesize to make sure conversion actually worked
+ fs.stat(testFile, function(err, stats) {
+ test.ok(!err);
+ test.ok(stats.size > 0);
+ test.ok(stats.isFile());
+ // unlink file
+ fs.unlinkSync(testFile);
+ test.done();
+ });
+ })
+ });
+ },
+ testConvertToStream: function(test) {
+ test.expect(4);
+ var testFile = __dirname + '/assets/testConvertToStream.flv';
+ var outstream = fs.createWriteStream(testFile);
+ var args = new ffmpeg(this.testfile)
+ .usingPreset('flashvideo')
+ .writeToStream(outstream, function(code, stderr) {
+ path.exists(testFile, function(exist) {
+ test.ok(exist);
+ // check filesize to make sure conversion actually worked
+ fs.stat(testFile, function(err, stats) {
+ test.ok(!err);
+ test.ok(stats.size > 0);
+ test.ok(stats.isFile());
+ // unlink file
+ fs.unlinkSync(testFile);
+ test.done();
+ });
+ })
+ });
+ },
+ testConvertFromStream: function(test) {
+ var instream = fs.createReadStream(this.testfile);
+ var testFile = __dirname + '/assets/testConvertFromStream.flv';
+ test.expect(3);
+ var args = new ffmpeg(instream)
+ .usingPreset('flashvideo')
+ .saveToFile(testFile, function(stderr, stdout, err) {
+ path.exists(testFile, function(exist) {
+ // check filesize to make sure conversion actually worked
+ if (exist) {
+ fs.stat(testFile, function(err, stats) {
+ test.ok(!err);
+ test.ok(stats.size > 0);
+ test.ok(stats.isFile());
+ // unlink file
+ fs.unlinkSync(testFile);
+ test.done();
+ });
+ } else {
+ test.done();
+ }
+ })
+ });
+ },
+ testTakeScreenshots: function(test) {
+ test.expect(2);
+ var testFolder = __dirname + '/assets/tntest';
+ var self = this;
+ fs.mkdir(testFolder, '0755', function(err) {
+ var args = new ffmpeg(self.testfile)
+ .withSize('150x?')
+ .takeScreenshots(2, testFolder, function(err) {
+ test.ok(!err);
+ fs.readdir(testFolder, function(err, files) {
+ var tnCount = 0;
+ files.forEach(function(file) {
+ if (file.indexOf('.jpg') > -1) {
+ tnCount++;
+ fs.unlinkSync(testFolder + '/' + file);
+ }
+ });
+ test.ok(tnCount == 2);
+ // remove folder
+ fs.rmdirSync(testFolder);
+ test.done();
+ });
+ });
+ });
+ }
+});

0 comments on commit 0d0375d

Please sign in to comment.