{
  config,
  pkgs,
  lib,
  ...
}:

with lib;

let
  obs-cmd = pkgs.rustPlatform.buildRustPackage rec {
    pname = "obs-cmd";
    version = "v0.15.2";
    src = pkgs.fetchFromGitHub {
      owner = "grigio";
      repo = "obs-cmd";
      rev = version;
      sha256 = "sha256-RRkP0QLWcJLKv8oqESjMgHGW1QScANG7+fzR/rwSyDI=";
    };

    cargoSha256 = "sha256-JqR7MAt2VNEnZGbn+hExtFG6F7X0KhFM1n7GZ+QaHc0=";
    #cargoSha256 = fakeSha256;

    meta = with lib; {
      description = "a minimal obs CLI for obs-websocket v5";
      homepage = "https://github.com/grigio/obs-cmd";
      license = licenses.mit;
      maintainers = [ maintainers.mrVanDalo ];
      platforms = platforms.all;
    };
  };

  # Lassulus streaming setup
  # -------------------------
  # ffmpeg \
  #   -f pulse \
  #   -i default \
  #   -vaapi_device /dev/dri/renderD128 \
  #   -f x11grab \
  #   -video_size 1366x768 \
  #   -i :0 \
  #   -vf 'hwupload,scale_vaapi=format=nv12' \
  #   -c:v h264_vaapi \
  #   -c:a aac \
  #   -b:a 96k \
  #   -af "highpass=f=200, lowpass=f=3000" \
  #   -qp 30 \
  #   -f flv \
  #   rtmp://lassul.us:1935/stream/nixos \
  #   ./rc3-output-$(date +%d%H%M%S).mp4
  #
  # Dann abspielen mit :
  # mpv rtmp://lassul.us:1935/stream/nixos

  # show keyboard input on desktop for screencasts
  screenKey = pkgs.symlinkJoin {
    name = "screen-keys";
    paths =
      let
        screenKeyScript =
          {
            position ? "bottom",
            size ? "small",
            ...
          }:
          pkgs.writeShellScriptBin "screenkeys-${position}-${size}" # sh
            ''
              ${pkgs.screenkey}/bin/screenkey \
                                --no-detach \
                                --bg-color '#fdf6e3' \
                                --font-color '#073642' \
                                -p ${position} \
                                -s ${size} \
                                "$@"
            '';
      in
      lib.flatten (
        lib.flip map
          [
            "large"
            "small"
            "medium"
          ]
          (
            size:
            lib.flip map [
              "top"
              "center"
              "bottom"
            ] (position: screenKeyScript { inherit size position; })
          )
      );
  };

  mpvReview =
    let
      moveToDir =
        key: dir:
        pkgs.writeText "move-with-${key}.lua" ''
          tmp_dir = "${dir}"

          function move_current_track_${key}()
            track = mp.get_property("path")
            os.execute("mkdir -p '" .. tmp_dir .. "'")
            os.execute("mv '" .. track .. "' '" .. tmp_dir .. "'")
            print("moved '" .. track .. "' to " .. tmp_dir)
            mp.command("playlist-next")
          end

          mp.add_key_binding("${key}", "move_current_track_${key}", move_current_track_${key})
        '';
      delete = moveToDir "D" "./.graveyard";
      good = moveToDir "G" "./.good";
    in
    pkgs.writers.writeDashBin "mpv-review" ''
      exec ${pkgs.mpv}/bin/mpv --no-config --script=${delete} --script=${good} "$@"
    '';

  alphaSafe = pkgs.writers.writeBashBin "alpha-safe" ''
    export PATH=${pkgs.gnused}/bin:$PATH
    echo "$1" | sed 's/[^a-zA-Z0-9]/-/g' | sed 's/--/-/g' | sed 's/-$//g'
  '';

  sanitizeFolder = pkgs.writers.writeBashBin "sanitize-folder" (fileContents ./sanitize_folder.sh);

in
{

  options.components.media.video.enable = mkOption {
    type = lib.types.bool;
    default = config.components.media.enable;
  };

  config = mkIf (config.components.media.video.enable) {

    home-manager.sharedModules = [
      {
        programs.obs-studio = {
          enable = false;
          plugins = with pkgs.obs-studio-plugins; [
            obs-backgroundremoval
            obs-vaapi
          ];
        };
      }
    ];

    boot.kernelModules = [ "v4l2loopback" ];
    boot.extraModulePackages = [ pkgs.linuxPackages.v4l2loopback ];

    environment.systemPackages = with pkgs; [
      yt-dlp
      mplayer
      mpv
      mpvReview

      # sanitazion
      alphaSafe
      sanitizeFolder

      # obs studio stuff
      obs-cli
      v4l-utils
      #obs-cmd

      # to record your screen
      # ---------------------
      simplescreenrecorder
      screenKey
      #obs-studio

      # to transcode video material
      # ---------------------------
      handbrake
      ffmpeg-full

    ];
  };
}