<commit_before>var fs = require('fs'),
  path = require('path'),
  async = require('../support/async.min.js'),
  os = require('os').platform(),
  exec = require('child_process').exec,
  spawn = require('child_process').spawn,
  Registry = require('./registry'),

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

  this.saveToFile = function(targetfile, callback) {

    this.options.outputfile = targetfile;
    var self = this;

    // parse options to command
    this._prepare(function(err, meta) {
      if (err) {
        callback(null, null, err);
      } else {
        var args = self.buildFfmpegArgs(false, meta);

        // kinda hacky, have to make sure the returned object is no array
        if (args.length === undefined) {
          // we got an error object, trigger error callback
          callback (null, null, args);
        } else {
          // start conversion of file using spawn
          var ffmpegProc = self._spawnProcess(args);
          if (self.options.inputstream) {
              // pump input stream to stdin
              self.options.inputstream.resume();
              self.options.inputstream.pipe(ffmpegProc.stdin);
          }

          //handle timeout if set
          var processTimer;
          if (self.options.timeout) {
            processTimer = setTimeout(function() {
              ffmpegProc.removeAllListeners('exit');
              ffmpegProc.kill('SIGKILL');
              self.options.logger.warn('process ran into a timeout (' + self.options.timeout + 's)');
              callback(self.E_PROCESSTIMEOUT, 'timeout');
            }, self.options.timeout * 1000);
          }

          var stdout = '';
          var stderr = '';
          ffmpegProc.on('exit', function(code) {
            if (processTimer) {
              clearTimeout(processTimer);
            }
            // check if we have to run flvtool2 to update flash video meta data
            if (self.options._updateFlvMetadata === true) {
              // make sure we didn't try to determine this capability before
              if (!Registry.instance.get('capabilityFlvTool2')) {
                // check if flvtool2 is installed
                exec('which flvtool2', function(whichErr, whichStdOut, whichStdErr) {
                  if (whichStdOut !== '') {
                    Registry.instance.set('capabilityFlvTool2', true);
                    // update metadata in flash video
                    exec('flvtool2 -U ' + self.options.outputfile, function(flvtoolErr, flvtoolStdout, flvtoolStderr) {
                      callback(stdout, stderr, null);
                    });
                  } else {
                    // flvtool2 is not installed, skip further checks
                    Registry.instance.set('capabilityFlvTool2', false);
                    callback(stdout, stderr, null);
                  }
                });
              } else if (!Registry.instance.get('capabilityFlvTool2')) {
                // flvtool2 capability was checked before, execute update
                exec('flvtool2 -U ' + self.options.outputfile, function(flvtoolErr, flvtoolStdout, flvtoolStderr) {
                  callback(stdout, stderr, null);
                });
              } else {
                // flvtool2 not installed, skip update
                callback(stdout, stderr, null);
              }
            } else {
              callback(stdout, stderr, null);
            }
          });
          ffmpegProc.stdout.on('data', function (data) {
            stdout += data;
          });

          ffmpegProc.stderr.on('data', function (data) {
            stderr += data;
            if (self.options.onCodecData) {
              self._checkStdErrForCodec(stderr);
            }
            if (self.options.onProgress) {
              self._getProgressFromStdErr(stderr, meta.durationsec);
            }
          });
        }
      }
    });
  };

  this.writeToStream = function(stream, callback) {
    if (!this.options._isStreamable) {
      this.options.logger.error('selected output format is not streamable');
      callback(null, new Error('selected output format is not streamable'));
    } else {
      var self = this;
      // parse options to command
      this._prepare(function(err, meta) {
        if (err) {
          callback(null, err);
        } else {
          var args = self.buildFfmpegArgs(true, meta);
          // kinda hacky, have to make sure the returned object is no array
          if (args.length === undefined) {
            callback(null, args);
          } else {
            // write data to stdout
            args.push('pipe:1');

            // start conversion of file using spawn
            var ffmpegProc = self._spawnProcess(args);

            if (self.options.inputstream) {
              // pump input stream to stdin
              self.options.inputstream.resume();
              self.options.inputstream.pipe(ffmpegProc.stdin);
            }

            //handle timeout if set
            var processTimer;
            if (self.options.timeout) {
              processTimer = setTimeout(function() {
                ffmpegProc.removeAllListeners('exit');
                ffmpegProc.kill('SIGKILL');
                self.options.logger.warn('process ran into a timeout (' + self.options.timeout + 's)');
                callback(self.E_PROCESSTIMEOUT, 'timeout');
              }, self.options.timeout * 1000);
            }

            var stderr = '';

            ffmpegProc.stderr.on('data', function(data) {
              stderr += data;
              if (self.options.onCodecData) {
                self._checkStdErrForCodec(stderr);
              }
              if (self.options.onProgress) {
                self._getProgressFromStdErr(stderr, meta.durationsec);
              }
            });

            ffmpegProc.stdout.on('data', function(chunk) {
              stream.write(chunk);
            });

            ffmpegProc.on('exit', function(code, signal) {
              if (processTimer) {
                clearTimeout(processTimer);
              }
              // close file descriptor on outstream
              if(/^[a-z]+:\/\//.exec(self.options.inputfile)) {
                callback(code, stderr);
              } else {
                var cb_ = function() {
                  if (self.options.inputstream) {
                    fs.close(self.options.inputstream.fd, function() {
                      callback(code, stderr);
                    });
                  }
                  else {
                    callback(code, stderr);
                  }
                };
                if (!stream.fd) {
                  if (stream.end) {
                    stream.end();
                  } else {
                    callback(code, "stream will not be closed");
                  }
                  cb_();
                } else {
                  fs.close(stream.fd, cb_);
                }
              }
            });
          }
        }
      });
    }
  };

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

    function _zeroPad(number, len) {
      return new Array(len-String(number).length+2).join('0')+number;
    }
    
    function _renderOutputName(j, offset) {
      var result = filename;
      if(/%0*i/.test(result)) {
        var numlen = String(result.match(/%(0*)i/)[1]).length;
        result = result.replace(/%0*i/, _zeroPad(j, numlen));
      }
      result = result.replace('%s', offset);
      result = result.replace('%w', self.options.video.width);
      result = result.replace('%h', self.options.video.height);
      result = result.replace('%r', self.options.video.width+'x'+self.options.video.height);
      result = result.replace('%f', self.options.inputfile);
      result = result.replace('%b', self.options.inputfile.substr(0,self.options.inputfile.lastIndexOf('.')));
      return result;
    }
    
    function _screenShotInternal(callback) {
      // get correct dimensions<commit_msg>Cleanup<commit_after>var fs = require('fs'),
  path = require('path'),
  async = require('../support/async.min.js'),
  os = require('os').platform(),
  exec = require('child_process').exec,
  spawn = require('child_process').spawn,
  Registry = require('./registry'),

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

  this.saveToFile = function(targetfile, callback) {

    this.options.outputfile = targetfile;
    var self = this;

    // parse options to command
    this._prepare(function(err, meta) {
      if (err) {
        callback(null, null, err);
      } else {
        var args = self.buildFfmpegArgs(false, meta);

        // kinda hacky, have to make sure the returned object is no array
        if (args.length === undefined) {
          // we got an error object, trigger error callback
          callback (null, null, args);
        } else {
          // start conversion of file using spawn
          var ffmpegProc = self._spawnProcess(args);
          if (self.options.inputstream) {
              // pump input stream to stdin
              self.options.inputstream.resume();
              self.options.inputstream.pipe(ffmpegProc.stdin);
          }

          //handle timeout if set
          var processTimer;
          if (self.options.timeout) {
            processTimer = setTimeout(function() {
              ffmpegProc.removeAllListeners('exit');
              ffmpegProc.kill('SIGKILL');
              self.options.logger.warn('process ran into a timeout (' + self.options.timeout + 's)');
              callback(self.E_PROCESSTIMEOUT, 'timeout');
            }, self.options.timeout * 1000);
          }

          var stdout = '';
          var stderr = '';
          ffmpegProc.on('exit', function(code) {
            if (processTimer) {
              clearTimeout(processTimer);
            }
            // check if we have to run flvtool2 to update flash video meta data
            if (self.options._updateFlvMetadata === true) {
              // make sure we didn't try to determine this capability before
              if (!Registry.instance.get('capabilityFlvTool2')) {
                // check if flvtool2 is installed
                exec('which flvtool2', function(whichErr, whichStdOut, whichStdErr) {
                  if (whichStdOut !== '') {
                    Registry.instance.set('capabilityFlvTool2', true);
                    // update metadata in flash video
                    exec('flvtool2 -U ' + self.options.outputfile, function(flvtoolErr, flvtoolStdout, flvtoolStderr) {
                      callback(stdout, stderr, null);
                    });
                  } else {
                    // flvtool2 is not installed, skip further checks
                    Registry.instance.set('capabilityFlvTool2', false);
                    callback(stdout, stderr, null);
                  }
                });
              } else if (!Registry.instance.get('capabilityFlvTool2')) {
                // flvtool2 capability was checked before, execute update
                exec('flvtool2 -U ' + self.options.outputfile, function(flvtoolErr, flvtoolStdout, flvtoolStderr) {
                  callback(stdout, stderr, null);
                });
              } else {
                // flvtool2 not installed, skip update
                callback(stdout, stderr, null);
              }
            } else {
              callback(stdout, stderr, null);
            }
          });
          ffmpegProc.stdout.on('data', function (data) {
            stdout += data;
          });

          ffmpegProc.stderr.on('data', function (data) {
            stderr += data;
            if (self.options.onCodecData) {
              self._checkStdErrForCodec(stderr);
            }
            if (self.options.onProgress) {
              self._getProgressFromStdErr(stderr, meta.durationsec);
            }
          });
        }
      }
    });
  };

  this.writeToStream = function(stream, callback) {
    if (!this.options._isStreamable) {
      this.options.logger.error('selected output format is not streamable');
      return callback(null, new Error('selected output format is not streamable'));
    }

    var self    = this;
    var options = this.options;
    // parse options to command
    this._prepare(function(err, meta) {
      if (err) {
        return callback(null, err);
      }
      var args = self.buildFfmpegArgs(true, meta);

      if (!args instanceof Array) {
        return callback(null, args);
      }
      // write data to stdout
      args.push('pipe:1');

      // start conversion of file using spawn
      var ffmpegProc = self._spawnProcess(args);

      if (options.inputstream) {
        // pump input stream to stdin
        options.inputstream.resume();
        options.inputstream.pipe(ffmpegProc.stdin);
      }

      //handle timeout if set
      var processTimer;
      if (options.timeout) {
        processTimer = setTimeout(function() {
          ffmpegProc.removeAllListeners('exit');
          ffmpegProc.kill('SIGKILL');
          options.logger.warn('process ran into a timeout (' + options.timeout + 's)');
          callback(self.E_PROCESSTIMEOUT, 'timeout');
        }, options.timeout * 1000);
      }

      var stderr = '';

      ffmpegProc.stderr.on('data', function(data) {
        stderr += data;
        if (options.onCodecData) {
          self._checkStdErrForCodec(stderr);
        }
        if (options.onProgress) {
          self._getProgressFromStdErr(stderr, meta.durationsec);
        }
      });

      ffmpegProc.stdout.on('data', function(chunk) {
        stream.write(chunk);
      });

      ffmpegProc.on('exit', function(code, signal) {
        if (processTimer) {
          clearTimeout(processTimer);
        }
        // close file descriptor on outstream
        if(/^[a-z]+:\/\//.exec(options.inputfile)) {
          return callback(code, stderr);
        }

        var cb_ = function() {
          if (!options.inputstream) {
            return callback(code, stderr);
          };
          fs.close(options.inputstream.fd, function() {
            callback(code, stderr);
          });
        };

        if (stream.fd) {
          return fs.close(stream.fd, cb_)
        }else {
          if (stream.end) {
            stream.end();
          } else {
            callback(code, "stream will not be closed");
          }
          cb_();
        }

      });
    });

  }

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

    function _zeroPad(number, len) {
      return new Array(len-String(number).length+2).join('0')+number;
    }
    
    function _renderOutputName(j, offset) {
      var result = filename;
      if(/%0*i/.test(result)) {
        var numlen = String(result.match(/%(0*)i/)[1]).length;
        result = result.replace(/%0*i/, _zeroPad(j, numlen));
      }
      result = result.replace('%s', offset);
      result = result.replace('%w', self.options.video.width);
      result = result.replace('%h', self.options.video.height);
      result = result.replace('%r', self.options.video.width+'x'+self.options.video.height);
      result = result.replace('%f', self.options.inputfile);
      result = result.replace('%b', self.options.inputfile.substr(0,self.options.inputfile.lastIndexOf('.')));
      return result;
    }
    
    function _screenShotInternal(callback) {
      // get correct dimensions
{'fraction_non_alphanumeric': 0.12100237133884509, 'fraction_numerical': 0.003845414343395501, 'mean_word_length': 1.7198884434373365, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3899605', 'n_tokens_mistral': 4280, 'n_tokens_neox': 4153, 'n_words': 906}