{ config, lib, pkgs, ... }:
with lib;
let

  cfg = config.programs.custom.ffmpeg;

  ffmpegTemplate = name:
    { profile, preset, tune ? null }:
    pkgs.writeShellScriptBin "ffmpeg-${name}" ''

      if [ $# -eq 0 ]
      then
        cat <<EOF
      ffmpeg-<profile>-<preset>-<tunes>-720p <input> <output>

      profiles =
        "baseline" - Primarily for low-cost applications that require additional data loss robustness
        "main"     - This profile is used for standard-definition digital TV broadcasts that use the MPEG-4 format as defined in the DVB standard.
        "high"     - The primary profile for broadcast and disc storage applications, particularly for high-definition television applications

      presets =
        "ultrafast"
        "superfast"
        "veryfast"
        "faster"
        "fast"
        "medium"
        "slow"
        "slower"
        "veryslow"

      tunes = (optional)
        "film"       - use for high quality movie content; lowers deblockin
        "animation"  - good for cartoons; uses higher deblocking and more reference frames
        "grain"      - preserves the grain structure in old, grainy film material
        "stillimage" - good for slideshow-like content
        "fastdecode" - allows faster decoding by disabling certain filters

      EOF
        exit 0
      fi

      if [ $# -ne 2 ]
      then
        echo "ffmpeg-${name} <input> <output>"
        exit 1
      fi

      input=$1
      output=$2

      if [ ! -f "$input" ]
      then
        echo "input does not exist $input"
        exit 1
      fi

      exec ${pkgs.ffmpeg}/bin/ffmpeg \
        -i "$input" \
        -filter:v scale=h='min(720\,ih)':w='min(1280\,iw)' \
        -vcodec libx264 \
        -preset ${preset} \
        -profile:v ${profile} \
        ${optionalString (tune != null) "-tune ${tune}"} \
        -acodec aac \
        "$output" \
        -hide_banner
    '';

  ffmpegDescriptive = profile: preset:
    ffmpegTemplate "${profile}-${preset}-720p" { inherit profile preset; };

  ffmpegDescriptiveTune = profile: preset: tune:
    ffmpegTemplate "${profile}-${preset}-${tune}-720p" {
      inherit profile preset tune;
    };

  # https://en.wikipedia.org/wiki/H.264/MPEG-4_AVC#Profiles
  profiles = [
    "baseline"
    "main"
    "high"
    #"high10"
    #"high422"
    #"high44"
  ];
  presets = [
    #"ultrafast"
    #"superfast"
    #"veryfast"
    #"faster"
    "fast"
    "medium"
    "slow"
    #"slower"
    "veryslow"
  ];
  tunes = [ "film" "animation" "grain" "stillimage" "fastdecode" ];

  ffmpegs = lib.crossLists ffmpegDescriptive [ profiles presets ];

  ffmpegsTune = lib.crossLists ffmpegDescriptiveTune [ profiles presets tunes ];

in {

  options.programs.custom.ffmpeg = {
    enable = mkEnableOption "enable programs.custom.ffmpeg";
  };

  config =
    mkIf cfg.enable { environment.systemPackages = ffmpegs ++ ffmpegsTune; };
}