Skip to content

Commit

Permalink
Never use .apply and instead give FfmpegCommand as parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Spruce (Felix Fichte) committed Jun 30, 2013
1 parent c933414 commit 4a97fff
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 93 deletions.
3 changes: 1 addition & 2 deletions examples/metadata.js
@@ -1,7 +1,6 @@
var Metalib = require('../index').Metadata;

// make sure you set the correct path to your video file
var metaObject = new Metalib('/path/to/your_movie.avi');
metaObject.get(function(metadata, err) {
Metalib('/path/to/your_movie.avi',function(metadata, err) {
console.log(require('util').inspect(metadata, false, null));
});
9 changes: 5 additions & 4 deletions lib/calculate.js
@@ -1,6 +1,6 @@

exports = module.exports = function Calculate(command) {
this._aspectIsEqual = function(ar1, ar2) {
command.prototype._aspectIsEqual = function(ar1, ar2) {
var p1 = this.toAspectRatio(ar1);
var p2 = this.toAspectRatio(ar2);
if (p1 === undefined || p2 === undefined) {
Expand All @@ -10,7 +10,7 @@ exports = module.exports = function Calculate(command) {
}
};

this._calculatePadding = function(data) {
command.prototype._calculatePadding = function(data) {
if (data.video.aspect) {
var newaspect, padAmount;
// check if the aspect ratio has changed
Expand Down Expand Up @@ -74,7 +74,7 @@ exports = module.exports = function Calculate(command) {
}
};

this._calculateDimensions = function(data) {
command.prototype._calculateDimensions = function(data) {
// load metadata and prepare size calculations
var fixedWidth = /([0-9]+)x\?/.exec(this.options.video.size);
var fixedHeight = /\?x([0-9]+)/.exec(this.options.video.size);
Expand Down Expand Up @@ -158,5 +158,6 @@ exports = module.exports = function Calculate(command) {
this.options.video.height = h;

};
exports.calculateDimensions = this._calculateDimensions;

command.prototype.calculateDimensions = command.prototype._calculateDimensions;
};
4 changes: 2 additions & 2 deletions lib/debug.js
@@ -1,5 +1,5 @@
exports = module.exports = function Debug(command) {
this.getCommand = function(outputmethod, callback) {
command.prototype.getCommand = function(outputmethod, callback) {
var self = this;
this._prepare(function(err, meta) {
if (err) {
Expand All @@ -22,7 +22,7 @@ exports = module.exports = function Debug(command) {
return this;
};

this.getArgs = function(callback) {
command.prototype.getArgs = function(callback) {
if (callback) {
var self = this;
this._prepare(function(err, meta) {
Expand Down
18 changes: 9 additions & 9 deletions lib/extensions.js
@@ -1,20 +1,20 @@

exports = module.exports = function Extensions() {
exports = module.exports = function Extensions(command) {

this.ffmpegPath = process.env.FFMPEG_PATH || 'ffmpeg';
command.prototype.ffmpegPath = process.env.FFMPEG_PATH || 'ffmpeg';

this.setFfmpegPath = function(path) {
command.prototype.setFfmpegPath = function(path) {
this.ffmpegPath = path;
};

this.determineFfmpegPath = function() {
command.prototype.determineFfmpegPath = function() {
if (this.ffmpegPath) {
return this.ffmpegPath;
}
return 'ffmpeg';
};

this.gcd = function(a, b) {
command.prototype.gcd = function(a, b) {
if (!a && !b) {
return 0;
}
Expand All @@ -26,7 +26,7 @@ exports = module.exports = function Extensions() {
return b;
};

this.toAspectRatio = function(ar) {
command.prototype.toAspectRatio = function(ar) {
var p = ar.split(':');
if (p.length !== 2) {
return undefined;
Expand All @@ -38,7 +38,7 @@ exports = module.exports = function Extensions() {
}
};

this.ffmpegTimemarkToSeconds = function(timemark) {
command.prototype.ffmpegTimemarkToSeconds = function(timemark) {
var parts = timemark.split(':');
var secs = 0;

Expand All @@ -56,7 +56,7 @@ exports = module.exports = function Extensions() {
return secs;
};

this.parseVersionString = function(versionstr) {
command.prototype.parseVersionString = function(versionstr) {
if (typeof versionstr != 'string' || versionstr.indexOf('.') == -1) {
return false;
}
Expand All @@ -72,7 +72,7 @@ exports = module.exports = function Extensions() {
};
};

this.atLeastVersion = function(actualVersion, minVersion) {
command.prototype.atLeastVersion = function(actualVersion, minVersion) {
var minimum = this.parseVersionString(minVersion);
var running = this.parseVersionString(actualVersion);

Expand Down
14 changes: 7 additions & 7 deletions lib/fluent-ffmpeg.js
Expand Up @@ -311,19 +311,19 @@ function FfmpegCommand(args) {
}

// add module methods
require('./extensions').apply(FfmpegCommand.prototype);
require('./metadata').apply(FfmpegCommand.prototype);
require('./processor').apply(FfmpegCommand.prototype);
require('./calculate').apply(FfmpegCommand.prototype);
require('./debug').apply(FfmpegCommand.prototype);

require('./extensions')(FfmpegCommand);
var metaDataLib = require('./metadata')(FfmpegCommand);
require('./processor')(FfmpegCommand);
require('./calculate')(FfmpegCommand);
require('./debug')(FfmpegCommand);
// module exports
exports = module.exports = function(args) {
return new FfmpegCommand(args);
};

// export meta data discovery
exports.Metadata = require('./metadata');

exports.Metadata = metaDataLib;
exports.Calculate = require('./calculate');

exports.CONSTANT_BITRATE = 1;
Expand Down
26 changes: 13 additions & 13 deletions lib/metadata.js
Expand Up @@ -2,8 +2,9 @@ var exec = require('child_process').exec,
path = require('path'),
os = require('os').platform();

exports = module.exports = function Metadata(inputfile) {
this.escapedPath = function(path, enclose) {
exports = module.exports = function Metadata(command) {

command.prototype.escapedPath = function(path, enclose) {
if(/http/.exec(path)) {
path = path.replace(' ', '%20');
} else {
Expand All @@ -22,27 +23,20 @@ exports = module.exports = function Metadata(inputfile) {
}
return path;
};

this.inputfile = inputfile?path.normalize(inputfile):'';

this.setFfmpegPath = function(path) {
this.ffmpegPath = path;
};

// for internal use
this.getMetadata = function(inputfile, callback) {
command.prototype.getMetadata = function(inputfile, callback) {
this.inputfile = path.normalize(inputfile);
this._loadDataInternal(callback);
};

// for external use
this.get = function(callback) {
command.prototype.get = function(callback) {
// import extensions for external call
require('./extensions').apply(Metadata.prototype);
this.inputfile = path.normalize(inputfile);
this._loadDataInternal(callback);
};

this._loadDataInternal = function(callback) {
command.prototype._loadDataInternal = function(callback) {
if (this.metaData){
return callback(this.metaData);
}
Expand Down Expand Up @@ -173,4 +167,10 @@ exports = module.exports = function Metadata(inputfile) {
callback(ret);
});
};

return function(filename,callback){
var cmd = new command({}).getMetadata(filename, callback);
}

};

25 changes: 13 additions & 12 deletions lib/processor.js
Expand Up @@ -8,10 +8,10 @@ var fs = require('fs'),

exports = module.exports = function Processor(command) {
// constant for timeout checks
this.E_PROCESSTIMEOUT = -99;
this._codecDataAlreadySent = false;
command.prototype.E_PROCESSTIMEOUT = -99;
command.prototype._codecDataAlreadySent = false;

this.saveToFile = function(targetfile, callback) {
command.prototype.saveToFile = function(targetfile, callback) {

callback = callback || function() {};

Expand Down Expand Up @@ -106,7 +106,7 @@ exports = module.exports = function Processor(command) {
});
};

this.mergeToFile = function(targetfile,callback){
command.prototype.mergeToFile = function(targetfile,callback){
this.options.outputfile = path.normalize(targetfile);
var self = this;
var options = this.options;
Expand Down Expand Up @@ -214,7 +214,7 @@ exports = module.exports = function Processor(command) {

}

this.writeToStream = function(stream, callback) {
command.prototype.writeToStream = function(stream, callback) {

callback = callback || function(){};

Expand Down Expand Up @@ -321,7 +321,7 @@ exports = module.exports = function Processor(command) {
});
};

this.takeScreenshots = function(config, folder, callback) {
command.prototype.takeScreenshots = function(config, folder, callback) {

callback = callback || function(){};

Expand Down Expand Up @@ -493,7 +493,7 @@ exports = module.exports = function Processor(command) {
});
};

this._getProgressFromStdErr = function(stderrString, totalDurationSec) {
command.prototype._getProgressFromStdErr = function(stderrString, totalDurationSec) {
// get last stderr line
var lastLine = stderrString.split(/\r\n|\r|\n/g);
var ll = lastLine[lastLine.length - 2];
Expand All @@ -520,7 +520,7 @@ exports = module.exports = function Processor(command) {
}
};

this._checkStdErrForCodec = function(stderrString) {
command.prototype._checkStdErrForCodec = function(stderrString) {
var format= /Input #[0-9]+, ([^ ]+),/.exec(stderrString);
var dur = /Duration\: ([^,]+)/.exec(stderrString);
var audio = /Audio\: (.*)/.exec(stderrString);
Expand Down Expand Up @@ -553,7 +553,7 @@ exports = module.exports = function Processor(command) {
}
};

this._spawnProcess = function(args, options) {
command.prototype._spawnProcess = function(args, options) {
var retProc = spawn(this.ffmpegPath, args, options);
// only re-nice if running on a non-windows platform
if (this.options.hasOwnProperty('_nice.level') && !os.match(/win(32|64)/)) {
Expand All @@ -580,7 +580,7 @@ exports = module.exports = function Processor(command) {
return retProc;
};

this.buildFfmpegArgs = function(overrideOutputCheck) {
command.prototype.buildFfmpegArgs = function(overrideOutputCheck) {
var args = [];

// add startoffset and duration
Expand Down Expand Up @@ -752,8 +752,7 @@ exports = module.exports = function Processor(command) {
return args;
};


this.requiresMetaData = function() {
command.prototype.requiresMetaData = function() {

if (this.options.video.pad && !this.options.video.skip)
{
Expand All @@ -772,4 +771,6 @@ exports = module.exports = function Processor(command) {

return false;
};

return command;
};
90 changes: 46 additions & 44 deletions test/extensions.test.js
@@ -1,49 +1,51 @@
var extlib = process.env.FLUENTFFMPEG_COV ? require('../lib-cov/extensions') : require('../lib/extensions'),
var Ffmpeg = require('../index'),
path = require('path'),
assert = require('assert');

// kinda nasty...
var ext = new extlib();

describe('Extensions.toAspectRatio', function() {
it('should convert an aspect ratio string to a proper object', function() {
var ret = ext.toAspectRatio('16:9');
ret.x.should.equal(16);
ret.y.should.equal(9);
});
it('should return undefined when an invalid aspect ratio is passed', function() {
assert.ok(!ext.toAspectRatio('15.929'));
});
});

describe('Extensions.parseVersionString', function() {
it('should parse the major/minor/patch version correctly', function() {
var ret = ext.parseVersionString('4.5.123');
ret.should.have.property('major').with.valueOf(4);
ret.should.have.property('minor').with.valueOf(5);
ret.should.have.property('patch').with.valueOf(123);
});
});

describe('Extensions.atLeastVersion', function() {
it('should correctly compare a full version number', function() {
ext.atLeastVersion('2.3.4532', '2.3.4531').should.be.true;
});
it('should correctly compare a version number without patch version', function() {
ext.atLeastVersion('2.3', '2.2.32').should.be.true;
});
it('should correctly compare a major version number', function() {
ext.atLeastVersion('3', '2.9.912').should.be.true;
});
it('should correctly compare an exact version match', function() {
ext.atLeastVersion('1.2.34', '1.2.34').should.be.true;
});
});

describe('Extensions.ffmpegTimemarkToSeconds', function() {
it('should correctly convert a simple timestamp', function() {
ext.ffmpegTimemarkToSeconds('00:02:00.00').should.be.equal(120);
});
it('should correctly convert a complex timestamp', function() {
ext.ffmpegTimemarkToSeconds('00:08:09.10').should.be.equal(489);
var ext = new Ffmpeg(path.join(__dirname, 'assets', 'testvideo-43.avi'));
describe('Extensions', function() {
describe('toAspectRatio', function() {
it('should convert an aspect ratio string to a proper object', function() {
var ret = ext.toAspectRatio('16:9');
ret.x.should.equal(16);
ret.y.should.equal(9);
});
it('should return undefined when an invalid aspect ratio is passed', function() {
assert.ok(!ext.toAspectRatio('15.929'));
});
});

describe('parseVersionString', function() {
it('should parse the major/minor/patch version correctly', function() {
var ret = ext.parseVersionString('4.5.123');
ret.should.have.property('major').with.valueOf(4);
ret.should.have.property('minor').with.valueOf(5);
ret.should.have.property('patch').with.valueOf(123);
});
});

describe('atLeastVersion', function() {
it('should correctly compare a full version number', function() {
ext.atLeastVersion('2.3.4532', '2.3.4531').should.be.true;
});
it('should correctly compare a version number without patch version', function() {
ext.atLeastVersion('2.3', '2.2.32').should.be.true;
});
it('should correctly compare a major version number', function() {
ext.atLeastVersion('3', '2.9.912').should.be.true;
});
it('should correctly compare an exact version match', function() {
ext.atLeastVersion('1.2.34', '1.2.34').should.be.true;
});
});

describe('ffmpegTimemarkToSeconds', function() {
it('should correctly convert a simple timestamp', function() {
ext.ffmpegTimemarkToSeconds('00:02:00.00').should.be.equal(120);
});
it('should correctly convert a complex timestamp', function() {
ext.ffmpegTimemarkToSeconds('00:08:09.10').should.be.equal(489);
});
});
});

0 comments on commit 4a97fff

Please sign in to comment.