const ConverterEngine = require('./ConverterEngine');
const ChildProcess = require('child_process');

class FfmpegConverterEngine extends ConverterEngine {
  constructor(player) {
    super(player);
    this.command = chooseCommand();
  }

  handleError(encoder, err) {
    if (encoder.destroy) encoder.destroy();
    this.emit('error', err);
  }

  createConvertStream(seek = 0) {
    super.createConvertStream();
    const encoder = ChildProcess.spawn(this.command, [
      '-analyzeduration', '0',
      '-loglevel', '0',
      '-i', '-',
      '-f', 's16le',
      '-ar', '48000',
      '-ss', String(seek),
      'pipe:1',
    ], { stdio: ['pipe', 'pipe', 'ignore'] });
    encoder.on('error', e => this.handleError(encoder, e));
    encoder.stdin.on('error', e => this.handleError(encoder, e));
    encoder.stdout.on('error', e => this.handleError(encoder, e));
    return encoder;
  }
}

function chooseCommand() {
  for (const cmd of ['ffmpeg', 'avconv', './ffmpeg', './avconv']) {
    if (!ChildProcess.spawnSync(cmd, ['-h']).error) return cmd;
  }
  return null;
}

module.exports = FfmpegConverterEngine;

@@ -1057,19 +1057,160 @@
 %7D%0A  
-return null
+throw new Error(%0A    'FFMPEG was not found on your system, so audio cannot be played.' +%0A    'Please make sure FFMPEG is installed and in your PATH'%0A  )
 ;%0A%7D%0A

add a message, because WHY THE HELL DID NO ONE EVER DO THIS (#690)