diff --git a/bin/omarchy-cmd-screenrecord b/bin/omarchy-cmd-screenrecord index d9dbdcc7..072cfab1 100755 --- a/bin/omarchy-cmd-screenrecord +++ b/bin/omarchy-cmd-screenrecord @@ -2,6 +2,8 @@ # Start and stop a screenrecording, which will be saved to ~/Videos by default. # Alternative location can be set via OMARCHY_SCREENRECORD_DIR or XDG_VIDEOS_DIR ENVs. +# Resolution is capped to 4K for monitors above 4K, native otherwise. +# Override via --resolution= (e.g. --resolution=1920x1080, --resolution=0x0 for native). [[ -f ~/.config/user-dirs.dirs ]] && source ~/.config/user-dirs.dirs OUTPUT_DIR="${OMARCHY_SCREENRECORD_DIR:-${XDG_VIDEOS_DIR:-$HOME/Videos}}" @@ -15,18 +17,35 @@ DESKTOP_AUDIO="false" MICROPHONE_AUDIO="false" WEBCAM="false" WEBCAM_DEVICE="" +RESOLUTION="" STOP_RECORDING="false" +RECORDING_FILE="/tmp/omarchy-screenrecord-filename" for arg in "$@"; do case "$arg" in - --with-desktop-audio) DESKTOP_AUDIO="true" ;; - --with-microphone-audio) MICROPHONE_AUDIO="true" ;; - --with-webcam) WEBCAM="true" ;; - --webcam-device=*) WEBCAM_DEVICE="${arg#*=}" ;; - --stop-recording) STOP_RECORDING="true" + --with-desktop-audio) DESKTOP_AUDIO="true" ;; + --with-microphone-audio) MICROPHONE_AUDIO="true" ;; + --with-webcam) WEBCAM="true" ;; + --webcam-device=*) WEBCAM_DEVICE="${arg#*=}" ;; + --resolution=*) RESOLUTION="${arg#*=}" ;; + --stop-recording) STOP_RECORDING="true" ;; esac done +default_resolution() { + local width height + read -r width height < <(hyprctl monitors -j | jq -r '.[] | select(.focused == true) | "\(.width) \(.height)"') + if ((width > 3840 || height > 2160)); then + echo "3840x2160" + else + echo "0x0" + fi +} + +toggle_screenrecording_indicator() { + pkill -RTMIN+8 waybar +} + cleanup_webcam() { pkill -f "WebcamOverlay" 2>/dev/null } @@ -36,7 +55,7 @@ start_webcam_overlay() { # Auto-detect first available webcam if none specified if [[ -z $WEBCAM_DEVICE ]]; then - WEBCAM_DEVICE=$(v4l2-ctl --list-devices 2>/dev/null | grep -m1 "^\s*/dev/video" | tr -d '\t') + WEBCAM_DEVICE=$(v4l2-ctl --list-devices 2>/dev/null | grep -m1 "^[[:space:]]*/dev/video" | tr -d '\t') if [[ -z $WEBCAM_DEVICE ]]; then notify-send "No webcam devices found" -u critical -t 3000 return 1 @@ -74,7 +93,7 @@ start_webcam_overlay() { start_screenrecording() { local filename="$OUTPUT_DIR/screenrecording-$(date +'%Y-%m-%d_%H-%M-%S').mp4" local audio_devices="" - local audio_args="" + local audio_args=() [[ $DESKTOP_AUDIO == "true" ]] && audio_devices+="default_output" @@ -84,15 +103,57 @@ start_screenrecording() { audio_devices+="default_input" fi - [[ -n $audio_devices ]] && audio_args+="-a $audio_devices" + [[ -n $audio_devices ]] && audio_args+=(-a "$audio_devices" -ac aac) + + local resolution="${RESOLUTION:-$(default_resolution)}" + + gpu-screen-recorder -w portal -k auto -s "$resolution" -f 60 -fallback-cpu-encoding yes -o "$filename" "${audio_args[@]}" & + local pid=$! + + # Wait for recording to actually start (file appears after portal selection) + while kill -0 $pid 2>/dev/null && [[ ! -f $filename ]]; do + sleep 0.2 + done + + if kill -0 $pid 2>/dev/null; then + echo "$filename" >"$RECORDING_FILE" + toggle_screenrecording_indicator + fi +} + +stop_screenrecording() { + pkill -SIGINT -f "^gpu-screen-recorder" # SIGINT required to save video properly + + # Wait a maximum of 5 seconds to finish before hard killing + local count=0 + while pgrep -f "^gpu-screen-recorder" >/dev/null && ((count < 50)); do + sleep 0.1 + count=$((count + 1)) + done - gpu-screen-recorder -w portal -k h264 -f 60 -fallback-cpu-encoding yes -o "$filename" $audio_args -ac aac & toggle_screenrecording_indicator + cleanup_webcam + + if pgrep -f "^gpu-screen-recorder" >/dev/null; then + pkill -9 -f "^gpu-screen-recorder" + notify-send "Screen recording error" "Recording process had to be force-killed. Video may be corrupted." -u critical -t 5000 + else + trim_first_frame + notify-send "Screen recording saved to $OUTPUT_DIR" -t 2000 + fi + + rm -f "$RECORDING_FILE" +} + +screenrecording_active() { + pgrep -f "^gpu-screen-recorder" >/dev/null } trim_first_frame() { - local latest=$(ls -t "$OUTPUT_DIR"/screenrecording-*.mp4 2>/dev/null | head -1) - if [[ -n $latest ]]; then + local latest + latest=$(cat "$RECORDING_FILE" 2>/dev/null) + + if [[ -n $latest && -f $latest ]]; then local trimmed="${latest%.mp4}-trimmed.mp4" if ffmpeg -y -ss 0.1 -i "$latest" -c copy "$trimmed" -loglevel quiet 2>/dev/null; then mv "$trimmed" "$latest" @@ -102,46 +163,12 @@ trim_first_frame() { fi } -stop_screenrecording() { - pkill -SIGINT -f "^gpu-screen-recorder" # SIGINT required to save video properly - - # Wait a maximum of 5 seconds to finish before hard killing - local count=0 - while pgrep -f "^gpu-screen-recorder" >/dev/null && (( count < 50 )); do - sleep 0.1 - count=$((count + 1)) - done - - if pgrep -f "^gpu-screen-recorder" >/dev/null; then - pkill -9 -f "^gpu-screen-recorder" - cleanup_webcam - notify-send "Screen recording error" "Recording process had to be force-killed. Video may be corrupted." -u critical -t 5000 - else - cleanup_webcam - trim_first_frame - notify-send "Screen recording saved to $OUTPUT_DIR" -t 2000 - fi - toggle_screenrecording_indicator -} - -toggle_screenrecording_indicator() { - pkill -RTMIN+8 waybar -} - -screenrecording_active() { - pgrep -f "^gpu-screen-recorder" >/dev/null || pgrep -f "WebcamOverlay" >/dev/null -} - if screenrecording_active; then - if pgrep -f "WebcamOverlay" >/dev/null && ! pgrep -f "^gpu-screen-recorder" >/dev/null; then - cleanup_webcam - else - stop_screenrecording - fi -elif [[ $STOP_RECORDING == "false" ]]; then + stop_screenrecording +elif [[ $STOP_RECORDING == "true" ]]; then + exit 1 +else [[ $WEBCAM == "true" ]] && start_webcam_overlay start_screenrecording || cleanup_webcam -else - exit 1 fi