Skip to content

Commit

Permalink
Report codecData for multiple inputs
Browse files Browse the repository at this point in the history
Also supports reporting codecData for stream inputs.
  • Loading branch information
jdp committed Apr 16, 2016
1 parent 058d5b1 commit 4be81e9
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 29 deletions.
62 changes: 33 additions & 29 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,35 +270,39 @@ var utils = module.exports = {
* @private
*/
extractCodecData: function(command, stderr) {
var format= /Input #[0-9]+, ([^ ]+),/.exec(stderr);
var dur = /Duration\: ([^,]+)/.exec(stderr);
var audio = /Audio\: (.*)/.exec(stderr);
var video = /Video\: (.*)/.exec(stderr);
var codecObject = { format: '', audio: '', video: '', duration: '' };

if (format && format.length > 1) {
codecObject.format = format[1];
}

if (dur && dur.length > 1) {
codecObject.duration = dur[1];
}

if (audio && audio.length > 1) {
audio = audio[1].split(', ');
codecObject.audio = audio[0];
codecObject.audio_details = audio;
}
if (video && video.length > 1) {
video = video[1].split(', ');
codecObject.video = video[0];
codecObject.video_details = video;
}

var codecInfoPassed = /Press (\[q\]|ctrl-c) to stop/.test(stderr);
if (codecInfoPassed) {
command.emit('codecData', codecObject);
command._codecDataSent = true;
var inputPattern = /Input #[0-9]+, ([^ ]+),/;
var durPattern = /Duration\: ([^,]+)/;
var audioPattern = /Audio\: (.*)/;
var videoPattern = /Video\: (.*)/;

var inputStack = [], inputIndex = -1, inInput = false;
var lines = stderr.split('\n'), format, dur, audio, video;
for (var i = 0, line; line = lines[i++]; line !== undefined) {
if (format = line.match(inputPattern)) {
inInput = true;
inputStack[++inputIndex] = { format: format[1], audio: '', video: '', duration: '' };
}
else if (inInput && (dur = line.match(durPattern))) {
inputStack[inputIndex].duration = dur[1];
}
else if (inInput && (audio = line.match(audioPattern))) {
audio = audio[1].split(', ');
inputStack[inputIndex].audio = audio[0];
inputStack[inputIndex].audio_details = audio;
}
else if (inInput && (video = line.match(videoPattern))) {
video = video[1].split(', ');
inputStack[inputIndex].video = video[0];
inputStack[inputIndex].video_details = video;
}
else if (/Output #\d+/.test(line)) {
inInput = false;
}
else if (/Stream mapping:|Press (\[q\]|ctrl-c) to stop/.test(line)) {
command.emit('codecData', inputStack.length === 1 ? inputStack[0] : inputStack);
command._codecDataSent = true;
break;
}
}
},

Expand Down
47 changes: 47 additions & 0 deletions test/processor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,53 @@ describe('Processor', function() {
.saveToFile(testFile);
});

it('should report codec data through \'codecData\' event on piped inputs', function(done) {
this.timeout(60000);

var testFile = path.join(__dirname, 'assets', 'testOnCodecData.avi')
this.files.push(testFile);

this.getCommand({ source: fs.createReadStream(this.testfilebig), logger: testhelper.logger })
.on('codecData', function(data) {
data.should.have.property('audio');
data.should.have.property('video');
})
.usingPreset('divx')
.on('error', function(err, stdout, stderr) {
testhelper.logError(err, stdout, stderr);
assert.ok(!err);
})
.on('end', function() {
done();
})
.saveToFile(testFile);
});

it('should report codec data through \'codecData\' for multiple inputs', function(done) {
this.timeout(60000);

var testFile = path.join(__dirname, 'assets', 'testOnCodecData.wav')
this.files.push(testFile);

this.getCommand({ logger: testhelper.logger })
.input(this.testfileaudio1)
.input(this.testfileaudio2)
.on('codecData', function(codecs) {
codecs.length.should.equal(2);
codecs.forEach(function(data) {
data.should.have.property('audio');
});
})
.on('error', function(err, stdout, stderr) {
testhelper.logError(err, stdout, stderr);
assert.ok(!err);
})
.on('end', function() {
done();
})
.mergeToFile(testFile);
});

it('should report progress through \'progress\' event', function(done) {
this.timeout(60000);

Expand Down

0 comments on commit 4be81e9

Please sign in to comment.