File size: 19,908 Bytes
811126d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 |
/*jshint node:true*/
'use strict';
var fs = require('fs');
var path = require('path');
var async = require('async');
var utils = require('./utils');
/*
*! Capability helpers
*/
var avCodecRegexp = /^\s*([D ])([E ])([VAS])([S ])([D ])([T ]) ([^ ]+) +(.*)$/;
var ffCodecRegexp = /^\s*([D\.])([E\.])([VAS])([I\.])([L\.])([S\.]) ([^ ]+) +(.*)$/;
var ffEncodersRegexp = /\(encoders:([^\)]+)\)/;
var ffDecodersRegexp = /\(decoders:([^\)]+)\)/;
var encodersRegexp = /^\s*([VAS\.])([F\.])([S\.])([X\.])([B\.])([D\.]) ([^ ]+) +(.*)$/;
var formatRegexp = /^\s*([D ])([E ])\s+([^ ]+)\s+(.*)$/;
var lineBreakRegexp = /\r\n|\r|\n/;
var filterRegexp = /^(?: [T\.][S\.][C\.] )?([^ ]+) +(AA?|VV?|\|)->(AA?|VV?|\|) +(.*)$/;
var cache = {};
module.exports = function(proto) {
/**
* Manually define the ffmpeg binary full path.
*
* @method FfmpegCommand#setFfmpegPath
*
* @param {String} ffmpegPath The full path to the ffmpeg binary.
* @return FfmpegCommand
*/
proto.setFfmpegPath = function(ffmpegPath) {
cache.ffmpegPath = ffmpegPath;
return this;
};
/**
* Manually define the ffprobe binary full path.
*
* @method FfmpegCommand#setFfprobePath
*
* @param {String} ffprobePath The full path to the ffprobe binary.
* @return FfmpegCommand
*/
proto.setFfprobePath = function(ffprobePath) {
cache.ffprobePath = ffprobePath;
return this;
};
/**
* Manually define the flvtool2/flvmeta binary full path.
*
* @method FfmpegCommand#setFlvtoolPath
*
* @param {String} flvtool The full path to the flvtool2 or flvmeta binary.
* @return FfmpegCommand
*/
proto.setFlvtoolPath = function(flvtool) {
cache.flvtoolPath = flvtool;
return this;
};
/**
* Forget executable paths
*
* (only used for testing purposes)
*
* @method FfmpegCommand#_forgetPaths
* @private
*/
proto._forgetPaths = function() {
delete cache.ffmpegPath;
delete cache.ffprobePath;
delete cache.flvtoolPath;
};
/**
* Check for ffmpeg availability
*
* If the FFMPEG_PATH environment variable is set, try to use it.
* If it is unset or incorrect, try to find ffmpeg in the PATH instead.
*
* @method FfmpegCommand#_getFfmpegPath
* @param {Function} callback callback with signature (err, path)
* @private
*/
proto._getFfmpegPath = function(callback) {
if ('ffmpegPath' in cache) {
return callback(null, cache.ffmpegPath);
}
async.waterfall([
// Try FFMPEG_PATH
function(cb) {
if (process.env.FFMPEG_PATH) {
fs.exists(process.env.FFMPEG_PATH, function(exists) {
if (exists) {
cb(null, process.env.FFMPEG_PATH);
} else {
cb(null, '');
}
});
} else {
cb(null, '');
}
},
// Search in the PATH
function(ffmpeg, cb) {
if (ffmpeg.length) {
return cb(null, ffmpeg);
}
utils.which('ffmpeg', function(err, ffmpeg) {
cb(err, ffmpeg);
});
}
], function(err, ffmpeg) {
if (err) {
callback(err);
} else {
callback(null, cache.ffmpegPath = (ffmpeg || ''));
}
});
};
/**
* Check for ffprobe availability
*
* If the FFPROBE_PATH environment variable is set, try to use it.
* If it is unset or incorrect, try to find ffprobe in the PATH instead.
* If this still fails, try to find ffprobe in the same directory as ffmpeg.
*
* @method FfmpegCommand#_getFfprobePath
* @param {Function} callback callback with signature (err, path)
* @private
*/
proto._getFfprobePath = function(callback) {
var self = this;
if ('ffprobePath' in cache) {
return callback(null, cache.ffprobePath);
}
async.waterfall([
// Try FFPROBE_PATH
function(cb) {
if (process.env.FFPROBE_PATH) {
fs.exists(process.env.FFPROBE_PATH, function(exists) {
cb(null, exists ? process.env.FFPROBE_PATH : '');
});
} else {
cb(null, '');
}
},
// Search in the PATH
function(ffprobe, cb) {
if (ffprobe.length) {
return cb(null, ffprobe);
}
utils.which('ffprobe', function(err, ffprobe) {
cb(err, ffprobe);
});
},
// Search in the same directory as ffmpeg
function(ffprobe, cb) {
if (ffprobe.length) {
return cb(null, ffprobe);
}
self._getFfmpegPath(function(err, ffmpeg) {
if (err) {
cb(err);
} else if (ffmpeg.length) {
var name = utils.isWindows ? 'ffprobe.exe' : 'ffprobe';
var ffprobe = path.join(path.dirname(ffmpeg), name);
fs.exists(ffprobe, function(exists) {
cb(null, exists ? ffprobe : '');
});
} else {
cb(null, '');
}
});
}
], function(err, ffprobe) {
if (err) {
callback(err);
} else {
callback(null, cache.ffprobePath = (ffprobe || ''));
}
});
};
/**
* Check for flvtool2/flvmeta availability
*
* If the FLVTOOL2_PATH or FLVMETA_PATH environment variable are set, try to use them.
* If both are either unset or incorrect, try to find flvtool2 or flvmeta in the PATH instead.
*
* @method FfmpegCommand#_getFlvtoolPath
* @param {Function} callback callback with signature (err, path)
* @private
*/
proto._getFlvtoolPath = function(callback) {
if ('flvtoolPath' in cache) {
return callback(null, cache.flvtoolPath);
}
async.waterfall([
// Try FLVMETA_PATH
function(cb) {
if (process.env.FLVMETA_PATH) {
fs.exists(process.env.FLVMETA_PATH, function(exists) {
cb(null, exists ? process.env.FLVMETA_PATH : '');
});
} else {
cb(null, '');
}
},
// Try FLVTOOL2_PATH
function(flvtool, cb) {
if (flvtool.length) {
return cb(null, flvtool);
}
if (process.env.FLVTOOL2_PATH) {
fs.exists(process.env.FLVTOOL2_PATH, function(exists) {
cb(null, exists ? process.env.FLVTOOL2_PATH : '');
});
} else {
cb(null, '');
}
},
// Search for flvmeta in the PATH
function(flvtool, cb) {
if (flvtool.length) {
return cb(null, flvtool);
}
utils.which('flvmeta', function(err, flvmeta) {
cb(err, flvmeta);
});
},
// Search for flvtool2 in the PATH
function(flvtool, cb) {
if (flvtool.length) {
return cb(null, flvtool);
}
utils.which('flvtool2', function(err, flvtool2) {
cb(err, flvtool2);
});
},
], function(err, flvtool) {
if (err) {
callback(err);
} else {
callback(null, cache.flvtoolPath = (flvtool || ''));
}
});
};
/**
* A callback passed to {@link FfmpegCommand#availableFilters}.
*
* @callback FfmpegCommand~filterCallback
* @param {Error|null} err error object or null if no error happened
* @param {Object} filters filter object with filter names as keys and the following
* properties for each filter:
* @param {String} filters.description filter description
* @param {String} filters.input input type, one of 'audio', 'video' and 'none'
* @param {Boolean} filters.multipleInputs whether the filter supports multiple inputs
* @param {String} filters.output output type, one of 'audio', 'video' and 'none'
* @param {Boolean} filters.multipleOutputs whether the filter supports multiple outputs
*/
/**
* Query ffmpeg for available filters
*
* @method FfmpegCommand#availableFilters
* @category Capabilities
* @aliases getAvailableFilters
*
* @param {FfmpegCommand~filterCallback} callback callback function
*/
proto.availableFilters =
proto.getAvailableFilters = function(callback) {
if ('filters' in cache) {
return callback(null, cache.filters);
}
this._spawnFfmpeg(['-filters'], { captureStdout: true, stdoutLines: 0 }, function (err, stdoutRing) {
if (err) {
return callback(err);
}
var stdout = stdoutRing.get();
var lines = stdout.split('\n');
var data = {};
var types = { A: 'audio', V: 'video', '|': 'none' };
lines.forEach(function(line) {
var match = line.match(filterRegexp);
if (match) {
data[match[1]] = {
description: match[4],
input: types[match[2].charAt(0)],
multipleInputs: match[2].length > 1,
output: types[match[3].charAt(0)],
multipleOutputs: match[3].length > 1
};
}
});
callback(null, cache.filters = data);
});
};
/**
* A callback passed to {@link FfmpegCommand#availableCodecs}.
*
* @callback FfmpegCommand~codecCallback
* @param {Error|null} err error object or null if no error happened
* @param {Object} codecs codec object with codec names as keys and the following
* properties for each codec (more properties may be available depending on the
* ffmpeg version used):
* @param {String} codecs.description codec description
* @param {Boolean} codecs.canDecode whether the codec is able to decode streams
* @param {Boolean} codecs.canEncode whether the codec is able to encode streams
*/
/**
* Query ffmpeg for available codecs
*
* @method FfmpegCommand#availableCodecs
* @category Capabilities
* @aliases getAvailableCodecs
*
* @param {FfmpegCommand~codecCallback} callback callback function
*/
proto.availableCodecs =
proto.getAvailableCodecs = function(callback) {
if ('codecs' in cache) {
return callback(null, cache.codecs);
}
this._spawnFfmpeg(['-codecs'], { captureStdout: true, stdoutLines: 0 }, function(err, stdoutRing) {
if (err) {
return callback(err);
}
var stdout = stdoutRing.get();
var lines = stdout.split(lineBreakRegexp);
var data = {};
lines.forEach(function(line) {
var match = line.match(avCodecRegexp);
if (match && match[7] !== '=') {
data[match[7]] = {
type: { 'V': 'video', 'A': 'audio', 'S': 'subtitle' }[match[3]],
description: match[8],
canDecode: match[1] === 'D',
canEncode: match[2] === 'E',
drawHorizBand: match[4] === 'S',
directRendering: match[5] === 'D',
weirdFrameTruncation: match[6] === 'T'
};
}
match = line.match(ffCodecRegexp);
if (match && match[7] !== '=') {
var codecData = data[match[7]] = {
type: { 'V': 'video', 'A': 'audio', 'S': 'subtitle' }[match[3]],
description: match[8],
canDecode: match[1] === 'D',
canEncode: match[2] === 'E',
intraFrameOnly: match[4] === 'I',
isLossy: match[5] === 'L',
isLossless: match[6] === 'S'
};
var encoders = codecData.description.match(ffEncodersRegexp);
encoders = encoders ? encoders[1].trim().split(' ') : [];
var decoders = codecData.description.match(ffDecodersRegexp);
decoders = decoders ? decoders[1].trim().split(' ') : [];
if (encoders.length || decoders.length) {
var coderData = {};
utils.copy(codecData, coderData);
delete coderData.canEncode;
delete coderData.canDecode;
encoders.forEach(function(name) {
data[name] = {};
utils.copy(coderData, data[name]);
data[name].canEncode = true;
});
decoders.forEach(function(name) {
if (name in data) {
data[name].canDecode = true;
} else {
data[name] = {};
utils.copy(coderData, data[name]);
data[name].canDecode = true;
}
});
}
}
});
callback(null, cache.codecs = data);
});
};
/**
* A callback passed to {@link FfmpegCommand#availableEncoders}.
*
* @callback FfmpegCommand~encodersCallback
* @param {Error|null} err error object or null if no error happened
* @param {Object} encoders encoders object with encoder names as keys and the following
* properties for each encoder:
* @param {String} encoders.description codec description
* @param {Boolean} encoders.type "audio", "video" or "subtitle"
* @param {Boolean} encoders.frameMT whether the encoder is able to do frame-level multithreading
* @param {Boolean} encoders.sliceMT whether the encoder is able to do slice-level multithreading
* @param {Boolean} encoders.experimental whether the encoder is experimental
* @param {Boolean} encoders.drawHorizBand whether the encoder supports draw_horiz_band
* @param {Boolean} encoders.directRendering whether the encoder supports direct encoding method 1
*/
/**
* Query ffmpeg for available encoders
*
* @method FfmpegCommand#availableEncoders
* @category Capabilities
* @aliases getAvailableEncoders
*
* @param {FfmpegCommand~encodersCallback} callback callback function
*/
proto.availableEncoders =
proto.getAvailableEncoders = function(callback) {
if ('encoders' in cache) {
return callback(null, cache.encoders);
}
this._spawnFfmpeg(['-encoders'], { captureStdout: true, stdoutLines: 0 }, function(err, stdoutRing) {
if (err) {
return callback(err);
}
var stdout = stdoutRing.get();
var lines = stdout.split(lineBreakRegexp);
var data = {};
lines.forEach(function(line) {
var match = line.match(encodersRegexp);
if (match && match[7] !== '=') {
data[match[7]] = {
type: { 'V': 'video', 'A': 'audio', 'S': 'subtitle' }[match[1]],
description: match[8],
frameMT: match[2] === 'F',
sliceMT: match[3] === 'S',
experimental: match[4] === 'X',
drawHorizBand: match[5] === 'B',
directRendering: match[6] === 'D'
};
}
});
callback(null, cache.encoders = data);
});
};
/**
* A callback passed to {@link FfmpegCommand#availableFormats}.
*
* @callback FfmpegCommand~formatCallback
* @param {Error|null} err error object or null if no error happened
* @param {Object} formats format object with format names as keys and the following
* properties for each format:
* @param {String} formats.description format description
* @param {Boolean} formats.canDemux whether the format is able to demux streams from an input file
* @param {Boolean} formats.canMux whether the format is able to mux streams into an output file
*/
/**
* Query ffmpeg for available formats
*
* @method FfmpegCommand#availableFormats
* @category Capabilities
* @aliases getAvailableFormats
*
* @param {FfmpegCommand~formatCallback} callback callback function
*/
proto.availableFormats =
proto.getAvailableFormats = function(callback) {
if ('formats' in cache) {
return callback(null, cache.formats);
}
// Run ffmpeg -formats
this._spawnFfmpeg(['-formats'], { captureStdout: true, stdoutLines: 0 }, function (err, stdoutRing) {
if (err) {
return callback(err);
}
// Parse output
var stdout = stdoutRing.get();
var lines = stdout.split(lineBreakRegexp);
var data = {};
lines.forEach(function(line) {
var match = line.match(formatRegexp);
if (match) {
match[3].split(',').forEach(function(format) {
if (!(format in data)) {
data[format] = {
description: match[4],
canDemux: false,
canMux: false
};
}
if (match[1] === 'D') {
data[format].canDemux = true;
}
if (match[2] === 'E') {
data[format].canMux = true;
}
});
}
});
callback(null, cache.formats = data);
});
};
/**
* Check capabilities before executing a command
*
* Checks whether all used codecs and formats are indeed available
*
* @method FfmpegCommand#_checkCapabilities
* @param {Function} callback callback with signature (err)
* @private
*/
proto._checkCapabilities = function(callback) {
var self = this;
async.waterfall([
// Get available formats
function(cb) {
self.availableFormats(cb);
},
// Check whether specified formats are available
function(formats, cb) {
var unavailable;
// Output format(s)
unavailable = self._outputs
.reduce(function(fmts, output) {
var format = output.options.find('-f', 1);
if (format) {
if (!(format[0] in formats) || !(formats[format[0]].canMux)) {
fmts.push(format);
}
}
return fmts;
}, []);
if (unavailable.length === 1) {
return cb(new Error('Output format ' + unavailable[0] + ' is not available'));
} else if (unavailable.length > 1) {
return cb(new Error('Output formats ' + unavailable.join(', ') + ' are not available'));
}
// Input format(s)
unavailable = self._inputs
.reduce(function(fmts, input) {
var format = input.options.find('-f', 1);
if (format) {
if (!(format[0] in formats) || !(formats[format[0]].canDemux)) {
fmts.push(format[0]);
}
}
return fmts;
}, []);
if (unavailable.length === 1) {
return cb(new Error('Input format ' + unavailable[0] + ' is not available'));
} else if (unavailable.length > 1) {
return cb(new Error('Input formats ' + unavailable.join(', ') + ' are not available'));
}
cb();
},
// Get available codecs
function(cb) {
self.availableEncoders(cb);
},
// Check whether specified codecs are available and add strict experimental options if needed
function(encoders, cb) {
var unavailable;
// Audio codec(s)
unavailable = self._outputs.reduce(function(cdcs, output) {
var acodec = output.audio.find('-acodec', 1);
if (acodec && acodec[0] !== 'copy') {
if (!(acodec[0] in encoders) || encoders[acodec[0]].type !== 'audio') {
cdcs.push(acodec[0]);
}
}
return cdcs;
}, []);
if (unavailable.length === 1) {
return cb(new Error('Audio codec ' + unavailable[0] + ' is not available'));
} else if (unavailable.length > 1) {
return cb(new Error('Audio codecs ' + unavailable.join(', ') + ' are not available'));
}
// Video codec(s)
unavailable = self._outputs.reduce(function(cdcs, output) {
var vcodec = output.video.find('-vcodec', 1);
if (vcodec && vcodec[0] !== 'copy') {
if (!(vcodec[0] in encoders) || encoders[vcodec[0]].type !== 'video') {
cdcs.push(vcodec[0]);
}
}
return cdcs;
}, []);
if (unavailable.length === 1) {
return cb(new Error('Video codec ' + unavailable[0] + ' is not available'));
} else if (unavailable.length > 1) {
return cb(new Error('Video codecs ' + unavailable.join(', ') + ' are not available'));
}
cb();
}
], callback);
};
};
|