Skip to content

Commit

Permalink
switched progress determination to duration calculation and ditched t…
Browse files Browse the repository at this point in the history
…otalframecount
  • Loading branch information
schaermu committed Mar 30, 2012
1 parent 35c8240 commit 5359611
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
18 changes: 18 additions & 0 deletions lib/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ String.prototype.toAspectRatio = function() {
}
}

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

// add hours
secs += parseInt(parts[0], 10) * 3600;
// add minutes
secs += parseInt(parts[1], 10) * 60;

// split sec/msec part
var secParts = parts[2].split('.');

// add seconds
secs += parseInt(secParts[0], 10);

return secs;
}

String.prototype.parseVersionString = function() {
var x = this.split('.');
// parse from string or default to 0 if can't parse
Expand Down
15 changes: 1 addition & 14 deletions lib/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,7 @@ var meta = module.exports = {

// calculate duration in seconds
if (duration && duration.length > 1) {
var parts = duration[1].split(':');
var secs = 0;
// add hours
secs += parseInt(parts[0]) * 3600;
// add minutes
secs += parseInt(parts[1]) * 60;
// add seconds
secs += parseInt(parts[2]);
ret.durationsec = secs;
}

// try to determine framecount
if (ret.durationsec > 0 && ret.video.fps > 0) {
ret.video.framecount = (ret.durationsec * ret.video.fps);
ret.durationsec = duration[1].ffmpegTimemarkToSeconds();
}

if (channels && channels.length > 1) {
Expand Down
13 changes: 7 additions & 6 deletions lib/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports = module.exports = function Processor(command) {
ffmpegProc.stderr.on('data', function (data) {
stderr += data;
if (self.options.onCodecData) self._checkStdErrForCodec(stderr);
if (self.options.onProgress) self._getProgressFromStdErr(stderr, meta.video.framecount);
if (self.options.onProgress) self._getProgressFromStdErr(stderr, meta.durationsec);
});
}
}
Expand Down Expand Up @@ -140,7 +140,7 @@ exports = module.exports = function Processor(command) {
ffmpegProc.stderr.on('data', function(data) {
stderr += data;
if (self.options.onCodecData) self._checkStdErrForCodec(stderr);
if (self.options.onProgress) self._getProgressFromStdErr(stderr, meta.video.framecount);
if (self.options.onProgress) self._getProgressFromStdErr(stderr, meta.durationsec);
});

ffmpegProc.stdout.on('data', function(chunk) {
Expand Down Expand Up @@ -318,7 +318,7 @@ exports = module.exports = function Processor(command) {
}
};

this._getProgressFromStdErr = function(stderrString, totalFrameCount) {
this._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 @@ -333,9 +333,10 @@ exports = module.exports = function Processor(command) {
timemark: progress[6]
};

// calculate percent progress using frame counts
if (totalFrameCount && totalFrameCount > 0) {
ret.percent = (ret.frames / totalFrameCount) * 100;
console.log('progress in sec: ' + ret.timemark.ffmpegTimemarkToSeconds());
// calculate percent progress using duration
if (totalDurationSec && totalDurationSec > 0) {
ret.percent = (ret.timemark.ffmpegTimemarkToSeconds() / totalDurationSec) * 100;
}

this.options.onProgress(ret);
Expand Down
6 changes: 6 additions & 0 deletions test/extensions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ module.exports = testCase({
test.ok('1.0.10'.atLeastVersion('1.0.4'));
test.ok('2.3.4532'.atLeastVersion('2.3.4531'));
test.done();
},
testFfmpegTimemarkToSeconds: function(test) {
console.log('00:08:09.10'.ffmpegTimemarkToSeconds());
test.ok('00:02:00.00'.ffmpegTimemarkToSeconds() == 120);
test.ok('00:08:09.10'.ffmpegTimemarkToSeconds() == 489);
test.done();
}
});

0 comments on commit 5359611

Please sign in to comment.