From 596cc3569010e3d4b92a72636d21134c2efdb465 Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 19 Aug 2024 15:45:27 +0200 Subject: [PATCH] feat: add gpu-screen-recorder service --- users/common/programs/gpu-screen-recorder.nix | 128 ++++++++++++++++++ users/patrick/default.nix | 1 + 2 files changed, 129 insertions(+) create mode 100644 users/common/programs/gpu-screen-recorder.nix diff --git a/users/common/programs/gpu-screen-recorder.nix b/users/common/programs/gpu-screen-recorder.nix new file mode 100644 index 0000000..3413e30 --- /dev/null +++ b/users/common/programs/gpu-screen-recorder.nix @@ -0,0 +1,128 @@ +{ + lib, + pkgs, + nixosConfig, + ... +}: +let + save-replay = pkgs.writeShellApplication { + name = "gpu-screen-recorder-save-replay"; + runtimeInputs = [ + pkgs.libnotify + pkgs.systemd + ]; + text = '' + # sytemctl itself may return 0 if the service is not running / unit doesn't exist + MAIN_PID=$(systemctl --user show --property MainPID --value gpu-screen-recorder.service || echo 0) + if [[ "$MAIN_PID" -gt 0 ]]; then + kill -USR1 "$MAIN_PID" + + notify-send '🎥 GPU Screen Recorder' '💾 Replay saved!' \ + -t 3000 \ + -i com.dec05eba.gpu_screen_recorder \ + -a 'GPU Screen Recorder' + + # Remember active window + hyprctl activewindow -j > "$HOME/Videos/Replay_$(date +"%Y-%m-%d_%H-%M-%S.window.json")" + else + notify-send '🎥 GPU Screen Recorder' '❌ Cannot save replay, service not running' \ + -t 5000 \ + -i com.dec05eba.gpu_screen_recorder \ + -a 'GPU Screen Recorder' + fi + ''; + }; + + on-stop-service = pkgs.writeShellApplication { + name = "gpu-screen-recorder-stop-service"; + runtimeInputs = [ pkgs.libnotify ]; + text = '' + if [[ "$SERVICE_RESULT" == "success" ]]; then + notify-send '🎥 GPU Screen Recorder' '🔴 Replay service stopped!' \ + -t 5000 -u low \ + -i com.dec05eba.gpu_screen_recorder \ + -a 'GPU Screen Recorder' + else + notify-send '🎥 GPU Screen Recorder' '❌ Replay service failed: '"$EXIT_STATUS (code $EXIT_CODE)"'!' \ + -t 5000 \ + -i com.dec05eba.gpu_screen_recorder \ + -a 'GPU Screen Recorder' + fi + ''; + }; + + start-service = pkgs.writeShellApplication { + name = "gpu-screen-recorder-start-service"; + runtimeInputs = [ + pkgs.pulseaudio + pkgs.libnotify + ]; + text = '' + AUDIO_SINK="$(pactl get-default-sink).monitor" + AUDIO_SOURCE="$(pactl get-default-source)" + + # Always search in sources since we added .monitor to any sinks already + AUDIO_SINK_DESC="$(pactl --format json list sources | jq -r '(.[] | select(.name == $NAME)).description' --arg NAME "$AUDIO_SINK")" + AUDIO_SOURCE_DESC="$(pactl --format json list sources | jq -r '(.[] | select(.name == $NAME)).description' --arg NAME "$AUDIO_SOURCE")" + + notify-send '🎥 GPU Screen Recorder' '🟢 Replay started'$'\n'$'\n'"→ $AUDIO_SINK_DESC"$'\n'"→ $AUDIO_SOURCE_DESC" \ + -t 5000 -u low \ + -i com.dec05eba.gpu_screen_recorder \ + -a 'GPU Screen Recorder' + + exec /run/wrappers/bin/gpu-screen-recorder \ + -w "$GSR_WINDOW" \ + -c "$GSR_CONTAINER" \ + -q "$GSR_QUALITY" \ + -f "$GSR_FRAMERATE" \ + -fm "$GSR_MODE" \ + -k "$GSR_CODEC" \ + -ac "$GSR_AUDIO_CODEC" \ + -r "$GSR_REPLAYDURATION" \ + -v "$GSR_FPSPPS" \ + -mf "$GSR_MAKEFOLDERS" \ + -a "audio-out/$AUDIO_SINK" \ + -a "microphone/$AUDIO_SOURCE" \ + -o "$GSR_OUTPUTDIR" + ''; + }; +in +{ + lib.gpu-screen-recorder = { + inherit save-replay; + }; + + systemd.user.services.gpu-screen-recorder = { + #Install.WantedBy = ["graphical-session.target"]; + Unit = { + Description = "GPU Screen Recorder Service"; + #PartOf = ["graphical-session.target"]; + }; + Service = { + Environment = + [ + "GSR_CONTAINER=mkv" + "GSR_QUALITY=ultra" + "GSR_FRAMERATE=144" + "GSR_MODE=cfr" + "GSR_CODEC=auto" + "GSR_AUDIO_CODEC=opus" + "GSR_REPLAYDURATION=30" + "GSR_OUTPUTDIR=%h/Videos" + "GSR_MAKEFOLDERS=no" + "GSR_COLOR_RANGE=full" + "GSR_FPSPPS=no" + ] + ++ lib.optionals (nixosConfig.node.name == "kroma") [ + "GSR_WINDOW=DP-2" # Primary monitor + ]; + + ExecStart = lib.getExe start-service; + ExecStopPost = lib.getExe on-stop-service; + KillSignal = "SIGINT"; + Restart = "on-failure"; + RestartSec = "10"; + Type = "simple"; + }; + }; +} diff --git a/users/patrick/default.nix b/users/patrick/default.nix index a55dfc9..511cd30 100644 --- a/users/patrick/default.nix +++ b/users/patrick/default.nix @@ -78,6 +78,7 @@ lib.optionalAttrs (!minimal) { ../common/programs/poe.nix ../common/programs/spicetify.nix ../common/programs/thunderbird.nix + ../common/programs/gpu-screen-recorder.nix ../common/shells/pager.nix ] ++ {