nix/home/config/sway/module/Screenshot.nix

108 lines
3.2 KiB
Nix
Raw Normal View History

2024-09-07 05:36:55 +03:00
{ config, pkgs, lib, ... }: let
2024-08-22 19:54:20 +03:00
codec = "libsvtav1";
2024-08-22 19:13:40 +03:00
color = config.style.color;
container = "mp4";
2024-08-22 19:54:20 +03:00
format = "%Y-%m-%d_%H-%M-%S";
2024-08-28 03:09:03 +03:00
framerate = 10;
2024-08-22 19:13:40 +03:00
opacity = "26";
selection = "slurp -d -b ${color.bg.light}${opacity} -c ${color.fg.light} -w 0 -s 00000000";
2024-08-22 20:53:39 +03:00
pixfmt = "yuv420p10le";
2024-08-22 19:13:40 +03:00
in {
2024-09-07 05:36:55 +03:00
text = let
fullscr = pkgs.writeShellScriptBin "FullscreenScreenshot" ''
scrFile="''${XDG_PICTURES_DIR[0]}/$(date +${format}).png"
grim \
-o $(swaymsg -t get_outputs | jq -r ".[] | select(.focused) | .name") - \
| tee "''${scrFile}" \
| wl-copy -t image/png
'';
selectscr = pkgs.writeShellScriptBin "SelectScreenshot" ''
scrSelection=$(${selection})
[[ -n "''${scrSelection}" ]] || exit
scrFile="''${XDG_PICTURES_DIR[0]}/$(date +${format}).png"
grim \
-g "''${scrSelection}" - \
| tee "''${scrFile}" \
| wl-copy -t image/png
'';
selectrec = pkgs.writeShellScriptBin "SelectRecording" ''
pkill -SIGINT wf-recorder \
|| {
scrSelection=$(${selection})
[[ -n "''${scrSelection}" ]] || exit
2024-09-07 05:41:03 +03:00
scrFile="''${XDG_VIDEOS_DIR[0]}/$(date +${format}).${container}"
2024-09-07 05:36:55 +03:00
scrTransform="$(swaymsg -t get_outputs | jq -r '.[] | select(.focused) | .transform')"
[[ "''${scrTransform}" = "normal" ]] && scrTransform=""
wf-recorder \
--geometry "''${scrSelection}" \
--codec ${codec} \
2024-09-07 05:41:03 +03:00
--file "''${scrFile}" \
2024-09-07 05:36:55 +03:00
--framerate ${toString framerate} \
--pixel-format ${pixfmt} \
&& ffmpeg \
-f lavfi \
-i anullsrc=channel_layout=stereo:sample_rate=44100 \
2024-09-07 05:41:03 +03:00
-i "''${scrFile}" \
2024-09-07 05:36:55 +03:00
-c:v copy \
-c:a libopus \
-shortest \
2024-09-07 05:41:03 +03:00
-f ${container} "''${scrFile}_" \
&& mv "''${scrFile}_" "''${scrFile}" \
2024-09-07 05:36:55 +03:00
&& [[ -n "''${scrTransform}" ]] \
2024-09-07 05:41:03 +03:00
&& ffmpeg -display_rotation ''${scrTransform} -i ''${scrFile} -c copy -f ${container} ''${scrFile}_ \
&& mv ''${scrFile}_ ''${scrFile} \
|| rm ''${scrFile}_
2024-09-07 05:36:55 +03:00
};
'';
fullrec = pkgs.writeShellScriptBin "FullscreenRecording" ''
pkill -SIGINT wf-recorder \
|| {
2024-09-07 05:41:03 +03:00
scrFile="''${XDG_VIDEOS_DIR[0]}/$(date +${format}).${container}"
2024-09-07 05:36:55 +03:00
scrTransform="$(swaymsg -t get_outputs | jq -r '.[] | select(.focused) | .transform')"
[[ "''${scrTransform}" = "normal" ]] && scrTransform=""
wf-recorder \
-o $(swaymsg -t get_outputs | jq -r ".[] | select(.focused) | .name") - \
--codec ${codec} \
2024-09-07 05:41:03 +03:00
--file "''${scrFile}" \
2024-09-07 05:36:55 +03:00
--framerate ${toString framerate} \
--pixel-format ${pixfmt} \
&& ffmpeg \
-f lavfi \
-i anullsrc=channel_layout=stereo:sample_rate=44100 \
2024-09-07 05:41:03 +03:00
-i "''${scrFile}" \
2024-09-07 05:36:55 +03:00
-c:v copy \
-c:a libopus \
-shortest \
2024-09-07 05:41:03 +03:00
-f ${container} "''${scrFile}_" \
&& mv "''${scrFile}_" "''${scrFile}" \
2024-09-07 05:36:55 +03:00
&& [[ -n "''${scrTransform}" ]] \
2024-09-07 05:41:03 +03:00
&& ffmpeg -display_rotation ''${scrTransform} -i ''${scrFile} -c copy -f ${container} ''${scrFile}_ \
&& mv ''${scrFile}_ ''${scrFile} \
|| rm ''${scrFile}_
2024-09-07 05:36:55 +03:00
};
'';
in ''
2024-04-06 03:03:58 +03:00
# Fullscreen screenshot.
2024-09-07 05:36:55 +03:00
bindsym --to-code $mod+y exec ${lib.getExe fullscr}
# Fullscreen recording.
bindsym --to-code $mod+shift+y exec ${lib.getExe fullrec}
2024-04-06 03:03:58 +03:00
# Select screenshot.
2024-09-07 05:36:55 +03:00
bindsym --to-code $mod+v exec ${lib.getExe selectscr}
2024-08-22 19:13:40 +03:00
# Select recording.
2024-09-07 05:36:55 +03:00
bindsym --to-code $mod+shift+v exec ${lib.getExe selectrec}
2024-04-06 03:03:58 +03:00
'';
}