import ffmpeg from "fluent-ffmpeg";
import path from "path";

export const description = "Generates a thumbnail of a specific size from a video."

export const options = [
  ["--input <file>", "input file video"],
  ["--start <time>", "starting position", 600],
  ["--output <file>", "output filename, ext will determine type"],
  ["--debug <level>", "debug level for ffmpeg, 1=command, 2=all"]
];

export const main = (opts) => {
  const { dir, name } = path.parse(opts.input);

  const encode = ffmpeg(opts.input)
    .inputOptions([
      "-ss", opts.start
    ])
    .outputOptions([
      "-frames:v", 1,
      "-vsync", "vfr"
    ])
    .on("end", () => process.exit(0));

  if(opts.output) {
    encode.output(opts.output);
  } else {
    encode.output(`${dir}/${name}.jpg`);
  }

  if(opts.debug === 1) {
    encode.on("start", line => console.log(line))
  } else if(opts.debug === 2) {
    encode.on("stderr", line => console.log(line))
  }

  encode.run();
}
