From c889afb92bba464aca101bee9584ddabf5c63911 Mon Sep 17 00:00:00 2001 From: nnisarggada Date: Fri, 22 Nov 2024 16:59:02 +0530 Subject: [PATCH] feat(app): Added flags to enable audio and to use only one script The script can now be run with optional flag to enable audio in the screen recording. The flag can be passed to check if the recording is on (for the waybar module). This allows only one script to be used for everything. --- README.md | 90 ++++++++++++++++++++++++++++++++++++-------------- record-screen | 56 ++++++++++++++++++++++++++----- record-screend | 32 ------------------ 3 files changed, 113 insertions(+), 65 deletions(-) delete mode 100755 record-screend diff --git a/README.md b/README.md index 8b2d05c..6242737 100644 --- a/README.md +++ b/README.md @@ -1,44 +1,84 @@ # `wayland-recorder` -Scripts to use with `wf-recorder` and `waybar` for screen recording + +Scripts to use with `wf-recorder` and `waybar` for screen recording. ## Dependencies -* [`slurp`](https://github.com/emersion/slurp) -* [`wf-recorder`](https://github.com/ammen99/wf-recorder) +- [`slurp`](https://github.com/emersion/slurp) +- [`wf-recorder`](https://github.com/ammen99/wf-recorder) -[`waybar`](https://github.com/Alexays/Waybar) can optionally be used to show -that a recording is in progress, but is not strictly necessary. +[`waybar`](https://github.com/Alexays/Waybar) can optionally be used to show that a recording is in progress, but is not strictly necessary. ## Setup -[`record-screen`](record-screen) is the main script. It is intended to be -bound to a hot key. Upon the first invocation, it runs `slurp` to let you -select an area of the screen to record, and then runs `wf-recorder` in the -background. By default, it records to `~/Videos/Recordings`, but this can be -changed by editing the script. On a second invocation, it stops the -recording. +[`record-screen`](record-screen) is the main script. It is intended to be bound to a hotkey. Upon the first invocation, it runs `slurp` to let you select an area of the screen to record, and then runs `wf-recorder` in the background. By default, it records to `~/vids/recordings`, but this can be changed by editing the script. On a second invocation, it stops the recording. -To use it, you can, for example, configure `sway` as such: +### New Flags: - bindsym $mod+print exec exec +- `-c` or `--check`: Checks if a recording is in progress and prints the status. +- `-a` or `--audio`: Records audio only (no screen recording). +- Default behavior (without flags) starts a screen recording after selecting the area via `slurp`. + +### Usage: + +- **To start a screen recording**: + Simply run the script or bind it to a hotkey. The first time you run it, it will prompt you to select the screen area and start the recording. + + ```bash + ./record-screen + ``` + +- **To stop an ongoing recording**: + Running the script again will stop the recording. + + ```bash + ./record-screen + ``` + +- **To start an audio-only recording**: + Pass the `-a` or `--audio` flag to the script to start recording audio only (no screen capture). + + ```bash + ./record-screen --audio + ``` + +- **To check if a recording is currently in progress**: + Use the `-c` or `--check` flag to see if a recording is currently active. + ```bash + ./record-screen --check + ``` + +### Example Keybinding for `sway`: + +You can configure the script to be executed via a hotkey, for example with the following line in your `sway` configuration: + +```bash +bindsym $mod+print exec exec +``` + +This will start/stop the screen recording upon each key press. ## Waybar integration -[`record-screend`](record-screend) can be used to create a custom waybar -widget which shows if a recording is in progress. +[`record-screen -c`](record-screen) can be used to create a custom waybar widget that shows if a recording is in progress. -This is an example configuration: +Here's an example configuration for `waybar`: ```json - "custom/recording": { - "exec": "exec ", - "exec-on-event": false, - "on-click": "pkill -INT -P \"$(pgrep -xo record-screen)\" wf-recorder" - }, +"custom/recording": { + "exec": "~/.local/bin/record-screen -c", + "interval": 1, + "tooltip-format": "Recording ON!", + "on-click": "~/.local/bin/record-screen" +} ``` -This configuration also allows you to terminate the current recording by -simply clicking the message. +### Explanation: -Note that the script attempts to display an icon with Font Awesome. If this -is not desired, the script could be edited to remove it. +- `"exec"` runs the script with the `-c` flag every second to check if a recording is in progress. +- `"on-click"` will execute the script without flags, which will stop the current recording when clicked. +- `"interval": 1` updates the status every second. + +This configuration also allows you to terminate the current recording by simply clicking the widget. + +Note that the script attempts to display an icon with Font Awesome. If this is not desired, the script could be edited to remove the icon. diff --git a/record-screen b/record-screen index 27d3114..7294f37 100755 --- a/record-screen +++ b/record-screen @@ -1,10 +1,50 @@ #!/bin/bash -if ! pkill -INT -P "$(pgrep -xo record-screen)" wf-recorder 2>/dev/null; then - geometry="$(slurp -d)" - if [ -n "$geometry" ]; then - pkill -USR1 -x record-screend - mkdir -p ~/Videos/Recordings - wf-recorder -f ~/Videos/Recordings/"screen-record-$(date +%Y-%m-%d-%H-%M-%S).mp4" -g "$geometry" - pkill -USR2 -x record-screend - fi + +# Get the process ID of the running wf-recorder +recording_pid=$(pgrep -xo wf-recorder) + +# Check if -a or --audio is passed for audio-only recording +audio_only=false +if [[ "$1" == "-a" || "$1" == "--audio" ]]; then + audio_only=true +fi + +# Function to handle errors gracefully +error_exit() { + notify-send "Screen Recording" "$1" + exit 1 +} + +# Handle the --check (-c) flag +if [[ "$1" == "-c" || "$1" == "--check" ]]; then + if [[ -n "$recording_pid" ]]; then + echo " Recording" + fi + exit 0 +fi + +# Stop recording if already running +if [[ -n "$recording_pid" ]]; then + kill "$recording_pid" && notify-send "Screen Recording" "Recording stopped" || error_exit "Failed to stop recording :/" + exit 0 +fi + +# Get the screen geometry using `slurp` +geometry="$(slurp -d)" || error_exit "Failed to capture screen geometry :/" + +if [[ -n "$geometry" ]]; then + # Create the recordings directory if it doesn't exist + recordings_dir=~/vids/recordings + mkdir -p "$recordings_dir" || error_exit "Failed to create directory: $recordings_dir :/" + + # Start recording using wf-recorder + if [[ "$audio_only" == true ]]; then + wf-recorder -a -f "$recordings_dir/screen-record-$(date +%Y-%m-%d-%H-%M-%S).mp4" -g "$geometry" || \ + error_exit "Failed to start screen recording :/" + else + wf-recorder -f "$recordings_dir/screen-record-$(date +%Y-%m-%d-%H-%M-%S).mp4" -g "$geometry" || \ + error_exit "Failed to start screen recording :/" + fi +else + error_exit "No geometry selected :/" fi diff --git a/record-screend b/record-screend deleted file mode 100755 index 7c584e3..0000000 --- a/record-screend +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/bash - -recordings=0 - -update() { - if [ "$recordings" -gt 0 ]; then - echo " Recording" - else - echo - fi -} - -begin_record() { - recordings=$((recordings + 1)) - update -} - -end_record() { - recordings=$((recordings - 1)) - update -} - -exec sleep infinity & - -pid="$!" -trap begin_record SIGUSR1 -trap end_record SIGUSR2 -trap "kill $pid" EXIT - -while :; do - wait "$pid" -done