// instrument by jscoverage, do not modifly this file
(function () {
  var BASE;
  if (typeof global === 'object') {
    BASE = global;
  } else if (typeof window === 'object') {
    BASE = window;
  } else {
    throw new Error('[jscoverage] unknow ENV!');
  }
  if (!BASE._$jscoverage) {
    BASE._$jscoverage = {};
    BASE._$jscoverage_cond = {};
    BASE._$jscoverage_done = function (file, line, express) {
      if (arguments.length === 2) {
        BASE._$jscoverage[file][line] ++;
      } else {
        BASE._$jscoverage_cond[file][line] ++;
        return express;
      }
    };
    BASE._$jscoverage_init = function (base, file, lines) {
      var tmp = [];
      for (var i = 0; i < lines.length; i ++) {
        tmp[lines[i]] = 0;
      }
      base[file] = tmp;
    };
  }
})();
_$jscoverage_init(_$jscoverage, "lib/ffprobe.js",[4,7,8,11,36,37,39,40,42,43,46,48,50,51,55,56,60,61,62,63,64,67,68,69,70,73,80,81,85,87,88,91,92,93,94,97,101,103,104,106,110,111,113,114,116,117,118,122,124,125,127,128,129,134,139,140,141,143,144,145,146,148,153,154,157,158,159,162,163,166,167,168]);
_$jscoverage_init(_$jscoverage_cond, "lib/ffprobe.js",[39,42,50,55,61,63,87,91,91,91,92,93,113,124,143,145]);
_$jscoverage["lib/ffprobe.js"].source = ["/*jshint node:true, laxcomma:true*/","'use strict';","","var spawn = require('child_process').spawn;","","","function legacyTag(key) { return key.match(/^TAG:/); }","function legacyDisposition(key) { return key.match(/^DISPOSITION:/); }","","","module.exports = function(proto) {","  /**","   * A callback passed to the {@link FfmpegCommand#ffprobe} method.","   *","   * @callback FfmpegCommand~ffprobeCallback","   *","   * @param {Error|null} err error object or null if no error happened","   * @param {Object} ffprobeData ffprobe output data; this object","   *   has the same format as what the following command returns:","   *","   *     `ffprobe -print_format json -show_streams -show_format INPUTFILE`","   * @param {Array} ffprobeData.streams stream information","   * @param {Object} ffprobeData.format format information","   */","","  /**","   * Run ffprobe on last specified input","   *","   * @method FfmpegCommand#ffprobe","   * @category Metadata","   *","   * @param {Number} [index] 0-based index of input to probe (defaults to last input)","   * @param {FfmpegCommand~ffprobeCallback} callback callback function","   *","   */","  proto.ffprobe = function(index, callback) {","    var input;","","    if (typeof callback === 'undefined') {","      callback = index;","","      if (!this._currentInput) {","        return callback(new Error('No input specified'));","      }","","      input = this._currentInput;","    } else {","      input = this._inputs[index];","","      if (!input) {","        return callback(new Error('Invalid input index'));","      }","    }","","    if (!input.isFile) {","      return callback(new Error('Cannot run ffprobe on non-file input'));","    }","","    // Find ffprobe","    this._getFfprobePath(function(err, path) {","      if (err) {","        return callback(err);","      } else if (!path) {","        return callback(new Error('Cannot find ffprobe'));","      }","","      var stdout = '';","      var stdoutClosed = false;","      var stderr = '';","      var stderrClosed = false;","","      // Spawn ffprobe","      var ffprobe = spawn(path, [","        '-print_format', 'json',","        '-show_streams',","        '-show_format',","        input.source","      ]);","","      ffprobe.on('error', function(err) {","        callback(err);","      });","","      // Ensure we wait for captured streams to end before calling callback","      var exitError = null;","      function handleExit(err) {","        if (err) {","          exitError = err;","        }","","        if (processExited && stdoutClosed && stderrClosed) {","          if (exitError) {","            if (stderr) {","              exitError.message += '\\n' + stderr;","            }","","            return callback(exitError);","          }","","          // Process output","          var data;","","          try {","            data = JSON.parse(stdout);","          } catch(e) {","            return callback(e);","          }","","          // Handle legacy output with \"TAG:x\" and \"DISPOSITION:x\" keys","          [data.format].concat(data.streams).forEach(function(target) {","            var legacyTagKeys = Object.keys(target).filter(legacyTag);","","            if (legacyTagKeys.length) {","              target.tags = target.tags || {};","","              legacyTagKeys.forEach(function(tagKey) {","                target.tags[tagKey.substr(4)] = target[tagKey];","                delete target[tagKey];","              });","            }","","            var legacyDispositionKeys = Object.keys(target).filter(legacyDisposition);","","            if (legacyDispositionKeys.length) {","              target.disposition = target.disposition || {};","","              legacyDispositionKeys.forEach(function(dispositionKey) {","                target.disposition[dispositionKey.substr(12)] = target[dispositionKey];","                delete target[dispositionKey];","              });","            }","          });","","          callback(null, data);","        }","      }","","      // Handle ffprobe exit","      var processExited = false;","      ffprobe.on('exit', function(code, signal) {","        processExited = true;","","        if (code) {","          handleExit(new Error('ffprobe exited with code ' + code));","        } else if (signal) {","          handleExit(new Error('ffprobe was killed with signal ' + signal));","        } else {","          handleExit();","        }","      });","","      // Handle stdout/stderr streams","      ffprobe.stdout.on('data', function(data) {","        stdout += data;","      });","","      ffprobe.stdout.on('close', function() {","        stdoutClosed = true;","        handleExit();","      });","","      ffprobe.stderr.on('data', function(data) {","        stderr += data;","      });","","      ffprobe.stderr.on('close', function() {","        stderrClosed = true;","        handleExit();","      });","    });","  };","};","",""];
"use strict";

_$jscoverage_done("lib/ffprobe.js", 4);
var spawn = require("child_process").spawn;

function legacyTag(key) {
    _$jscoverage_done("lib/ffprobe.js", 7);
    return key.match(/^TAG:/);
}

function legacyDisposition(key) {
    _$jscoverage_done("lib/ffprobe.js", 8);
    return key.match(/^DISPOSITION:/);
}

_$jscoverage_done("lib/ffprobe.js", 11);
module.exports = function(proto) {
    _$jscoverage_done("lib/ffprobe.js", 36);
    proto.ffprobe = function(index, callback) {
        _$jscoverage_done("lib/ffprobe.js", 37);
        var input;
        _$jscoverage_done("lib/ffprobe.js", 39);
        if (_$jscoverage_done("lib/ffprobe.js", 39, typeof callback === "undefined")) {
            _$jscoverage_done("lib/ffprobe.js", 40);
            callback = index;
            _$jscoverage_done("lib/ffprobe.js", 42);
            if (_$jscoverage_done("lib/ffprobe.js", 42, !this._currentInput)) {
                _$jscoverage_done("lib/ffprobe.js", 43);
                return callback(new Error("No input specified"));
            }
            _$jscoverage_done("lib/ffprobe.js", 46);
            input = this._currentInput;
        } else {
            _$jscoverage_done("lib/ffprobe.js", 48);
            input = this._inputs[index];
            _$jscoverage_done("lib/ffprobe.js", 50);
            if (_$jscoverage_done("lib/ffprobe.js", 50, !input)) {
                _$jscoverage_done("lib/ffprobe.js", 51);
                return callback(new Error("Invalid input index"));
            }
        }
        _$jscoverage_done("lib/ffprobe.js", 55);
        if (_$jscoverage_done("lib/ffprobe.js", 55, !input.isFile)) {
            _$jscoverage_done("lib/ffprobe.js", 56);
            return callback(new Error("Cannot run ffprobe on non-file input"));
        }
        _$jscoverage_done("lib/ffprobe.js", 60);
        this._getFfprobePath(function(err, path) {
            _$jscoverage_done("lib/ffprobe.js", 61);
            if (_$jscoverage_done("lib/ffprobe.js", 61, err)) {
                _$jscoverage_done("lib/ffprobe.js", 62);
                return callback(err);
            } else {
                _$jscoverage_done("lib/ffprobe.js", 63);
                if (_$jscoverage_done("lib/ffprobe.js", 63, !path)) {
                    _$jscoverage_done("lib/ffprobe.js", 64);
                    return callback(new Error("Cannot find ffprobe"));
                }
            }
            _$jscoverage_done("lib/ffprobe.js", 67);
            var stdout = "";
            _$jscoverage_done("lib/ffprobe.js", 68);
            var stdoutClosed = false;
            _$jscoverage_done("lib/ffprobe.js", 69);
            var stderr = "";
            _$jscoverage_done("lib/ffprobe.js", 70);
            var stderrClosed = false;
            _$jscoverage_done("lib/ffprobe.js", 73);
            var ffprobe = spawn(path, [ "-print_format", "json", "-show_streams", "-show_format", input.source ]);
            _$jscoverage_done("lib/ffprobe.js", 80);
            ffprobe.on("error", function(err) {
                _$jscoverage_done("lib/ffprobe.js", 81);
                callback(err);
            });
            _$jscoverage_done("lib/ffprobe.js", 85);
            var exitError = null;
            function handleExit(err) {
                _$jscoverage_done("lib/ffprobe.js", 87);
                if (_$jscoverage_done("lib/ffprobe.js", 87, err)) {
                    _$jscoverage_done("lib/ffprobe.js", 88);
                    exitError = err;
                }
                _$jscoverage_done("lib/ffprobe.js", 91);
                if (_$jscoverage_done("lib/ffprobe.js", 91, processExited) && _$jscoverage_done("lib/ffprobe.js", 91, stdoutClosed) && _$jscoverage_done("lib/ffprobe.js", 91, stderrClosed)) {
                    _$jscoverage_done("lib/ffprobe.js", 92);
                    if (_$jscoverage_done("lib/ffprobe.js", 92, exitError)) {
                        _$jscoverage_done("lib/ffprobe.js", 93);
                        if (_$jscoverage_done("lib/ffprobe.js", 93, stderr)) {
                            _$jscoverage_done("lib/ffprobe.js", 94);
                            exitError.message += "\n" + stderr;
                        }
                        _$jscoverage_done("lib/ffprobe.js", 97);
                        return callback(exitError);
                    }
                    _$jscoverage_done("lib/ffprobe.js", 101);
                    var data;
                    _$jscoverage_done("lib/ffprobe.js", 103);
                    try {
                        _$jscoverage_done("lib/ffprobe.js", 104);
                        data = JSON.parse(stdout);
                    } catch (e) {
                        _$jscoverage_done("lib/ffprobe.js", 106);
                        return callback(e);
                    }
                    _$jscoverage_done("lib/ffprobe.js", 110);
                    [ data.format ].concat(data.streams).forEach(function(target) {
                        _$jscoverage_done("lib/ffprobe.js", 111);
                        var legacyTagKeys = Object.keys(target).filter(legacyTag);
                        _$jscoverage_done("lib/ffprobe.js", 113);
                        if (_$jscoverage_done("lib/ffprobe.js", 113, legacyTagKeys.length)) {
                            _$jscoverage_done("lib/ffprobe.js", 114);
                            target.tags = target.tags || {};
                            _$jscoverage_done("lib/ffprobe.js", 116);
                            legacyTagKeys.forEach(function(tagKey) {
                                _$jscoverage_done("lib/ffprobe.js", 117);
                                target.tags[tagKey.substr(4)] = target[tagKey];
                                _$jscoverage_done("lib/ffprobe.js", 118);
                                delete target[tagKey];
                            });
                        }
                        _$jscoverage_done("lib/ffprobe.js", 122);
                        var legacyDispositionKeys = Object.keys(target).filter(legacyDisposition);
                        _$jscoverage_done("lib/ffprobe.js", 124);
                        if (_$jscoverage_done("lib/ffprobe.js", 124, legacyDispositionKeys.length)) {
                            _$jscoverage_done("lib/ffprobe.js", 125);
                            target.disposition = target.disposition || {};
                            _$jscoverage_done("lib/ffprobe.js", 127);
                            legacyDispositionKeys.forEach(function(dispositionKey) {
                                _$jscoverage_done("lib/ffprobe.js", 128);
                                target.disposition[dispositionKey.substr(12)] = target[dispositionKey];
                                _$jscoverage_done("lib/ffprobe.js", 129);
                                delete target[dispositionKey];
                            });
                        }
                    });
                    _$jscoverage_done("lib/ffprobe.js", 134);
                    callback(null, data);
                }
            }
            _$jscoverage_done("lib/ffprobe.js", 139);
            var processExited = false;
            _$jscoverage_done("lib/ffprobe.js", 140);
            ffprobe.on("exit", function(code, signal) {
                _$jscoverage_done("lib/ffprobe.js", 141);
                processExited = true;
                _$jscoverage_done("lib/ffprobe.js", 143);
                if (_$jscoverage_done("lib/ffprobe.js", 143, code)) {
                    _$jscoverage_done("lib/ffprobe.js", 144);
                    handleExit(new Error("ffprobe exited with code " + code));
                } else {
                    _$jscoverage_done("lib/ffprobe.js", 145);
                    if (_$jscoverage_done("lib/ffprobe.js", 145, signal)) {
                        _$jscoverage_done("lib/ffprobe.js", 146);
                        handleExit(new Error("ffprobe was killed with signal " + signal));
                    } else {
                        _$jscoverage_done("lib/ffprobe.js", 148);
                        handleExit();
                    }
                }
            });
            _$jscoverage_done("lib/ffprobe.js", 153);
            ffprobe.stdout.on("data", function(data) {
                _$jscoverage_done("lib/ffprobe.js", 154);
                stdout += data;
            });
            _$jscoverage_done("lib/ffprobe.js", 157);
            ffprobe.stdout.on("close", function() {
                _$jscoverage_done("lib/ffprobe.js", 158);
                stdoutClosed = true;
                _$jscoverage_done("lib/ffprobe.js", 159);
                handleExit();
            });
            _$jscoverage_done("lib/ffprobe.js", 162);
            ffprobe.stderr.on("data", function(data) {
                _$jscoverage_done("lib/ffprobe.js", 163);
                stderr += data;
            });
            _$jscoverage_done("lib/ffprobe.js", 166);
            ffprobe.stderr.on("close", function() {
                _$jscoverage_done("lib/ffprobe.js", 167);
                stderrClosed = true;
                _$jscoverage_done("lib/ffprobe.js", 168);
                handleExit();
            });
        });
    };
};