dmenu-record (4928B)
1 #!/bin/sh 2 # TODO: REWRITE THIS 3 dunstify "FIX THIS" "$0" 4 exit $? 5 6 # Audio server and webcam configuration. 7 AUDIO_HANDLER="${AUDIO_HANDLER:-alsa}" 8 WEBCAM_DRIVER="${WEBCAM_DRIVER:-v4l2}" 9 WEBCAM_DEVICE="${WEBCAM_DEVICE:-/dev/video0}" 10 11 # Get recorder settings. 12 SR_DIR="{SR_DIR:-$(xdg-user-dir VIDEOS)}" 13 SR_VIDEO_DIR="${SR_VIDEO_DIR:-$SR_DIR/video-$(date '+%y%m%d-%H%M-%S').mkv}" 14 SR_GIF_DIR="${SR_GIF_DIR:-$SR_DIR/gif-$(date '+%y%m%d-%H%M-%S').gif}" 15 SR_AUDIO_DIR="${SR_AUDIO_DIR:-$SR_DIR/audio-$(date '+%y%m%d-%H%M-%S').flac}" 16 SR_WEBCAM_DIR="${SR_WEBCAM_DIR:-$SR_DIR/webcam-$(date '+%y%m%d-%H%M-%S').mkv}" 17 SR_AUDIO_TAGS="${SR_AUDIO_TAGS:--f ${AUDIO_HANDLER} -i default -c:a flac}" 18 SR_PID="${SR_PID:-/tmp/recordingpid}" 19 20 # Get the options 21 while getopts ":ast:" option; do 22 case "$option" in 23 a) ENABLE_AUDIO="TRUE" ;; 24 s) ENABLE_SELECTION="TRUE" ;; 25 t) TYPE="$OPTARG" ;; 26 *) usage ;; 27 esac 28 done 29 [ "$ENABLE_AUDIO" = "TRUE" ] || SR_AUDIO_TAGS="" 30 31 select_window() { 32 if type slop >/dev/null; then 33 window="$(slop -qf "%x %y %w %h")" 34 [ "$window" ] || exit 2 35 LEFT="$(echo "$window" | awk -F' ' '{print $1}')" 36 TOP="$(echo "$window" | awk -F' ' '{print $2}')" 37 WIDTH="$(echo "$window" | awk -F' ' '{print $3}')" 38 HEIGHT="$(echo "$window" | awk -F' ' '{print $4}')" 39 else 40 window="$(xwininfo)" 41 LEFT="$(echo "$window" | grep -o "Absolute upper-left X:[[:space:]]*[0-9]*" | grep -o "[0-9]*")" 42 TOP="$(echo "$window" | grep -o "Absolute upper-left Y:[[:space:]]*[0-9]*" | grep -o "[0-9]*")" 43 WIDTH="$(echo "$window" | grep -o "Width:[[:space:]]*[0-9]*" | grep -o "[0-9]*")" 44 HEIGHT="$(echo "$window" | grep -o "Height:[[:space:]]*[0-9]*" | grep -o "[0-9]*")" 45 fi 46 } 47 48 video() { 49 [ "$ENABLE_SELECTION" = "TRUE" ] && select_window 50 ffmpeg -f x11grab \ 51 -s "$(xdpyinfo | awk '/dimensions/ {print $2;}')" \ 52 -i "$DISPLAY" \ 53 "$SR_AUDIO_TAGS" \ 54 -framerate 30 \ 55 -c:v h264 -crf 0 -preset ultrafast -c:a aac \ 56 "$SR_VIDEO_DIR" & 57 echo $! > "$SR_PID" 58 dunstify -u low -h string:x-dunst-stack-tag:recorder -t 700 -i "window" "壘 Recording screen" \ 59 "Saving to <span size='small'><u>$SR_VIDEO_DIR</u></span>..." 60 } 61 gif() { # NEEDS FIX 62 WIDTHxHEIGHT="$(xdpyinfo | awk '/dimensions/ {print $2}')" 63 WIDTH="${WIDTHxHEIGHT%x*}" ; HEIGHT="${WIDTHxHEIGHT#*x}" 64 LEFT=0 ; TOP=0 65 [ "$ENABLE_SELECTION" = "TRUE" ] && select_window 66 ffmpeg -f x11grab \ 67 -framerate 30 \ 68 -s "${WIDTH}x${HEIGHT}" \ 69 -i "$DISPLAY+$LEFT,$TOP" \ 70 "$SR_GIF_DIR" & 71 echo $! > "$SR_PID" 72 dunstify -u low -h string:x-dunst-stack-tag:recorder -t 700 -i "window" "壘 Recording screen" \ 73 "Saving to <span size='small'><u>$SR_VIDEO_DIR</u></span>..." 74 } 75 webcam() { 76 WEBCAM_WIDTHxHEIGHT=$(ffmpeg -f "$WEBCAM_DRIVER" -list_formats all -i "$WEBCAM_DEVICE" 2>&1 \ 77 | head -1 | awk -F' ' '{print $NF}') 78 ffmpeg "${SR_AUDIO_TAGS}" \ 79 -f "${WEBCAM_DRIVER}" \ 80 -i "${WEBCAM_DEVICE}" \ 81 -video_size "${WEBCAM_WIDTHxHEIGHT}" \ 82 "${SR_WEBCAM_DIR}" & 83 echo $! > "$SR_PID" 84 dunstify -u low -h string:x-dunst-stack-tag:recorder -t 700 -i "camera" "壘 Recording webcam" \ 85 "Saving to <span size='small'><u>$SR_WEBCAM_DIR</u></span>..." 86 } 87 audio() { 88 ffmpeg -f "${AUDIO_HANDLER}" \ 89 -i default \ 90 -c:a flac \ 91 "${SR_AUDIO_DIR}" & 92 echo $! > "$SR_PID" 93 dunstify -u low -h string:x-dunst-stack-tag:recorder -t 700 -i "mic-ready" "壘 Recording audio" \ 94 "Saving to <span size='small'><u>$SR_AUDIO_DIR</u></span>..." 95 } 96 endrec() { 97 PID="$(cat "$SR_PID")" 98 if ps -p "$PID" >/dev/null; then 99 COMM="$(ps -p "$PID" -o command=)" 100 NAME="$(echo "$COMM" | awk '{print $NF}')" 101 kill "$PID" 102 while kill -0 "$PID"; do sleep 1; kill "$PID"; done 103 rm -f "$SR_PID" 104 dunstify -u low -h string:x-dunst-stack-tag:recorder -t 3000 -i "record" "壘 Recording acquired!" "Saved to <span size='small'><u>$NAME</u></span>!" 105 echo "Recording stopped." 106 else 107 echo "No recording found to stop..." 108 fi 109 } 110 askendrec() { 111 [ -e "$SR_PID" ] || return 112 PID=$(cat "$SR_PID") 113 if ps -p "$PID" >/dev/null; then 114 printf "You are already recording, would you like to end the last one? [y/N] "; read -r ASK 115 case "$ASK" in 116 [Yy][Ee][Ss]|[Yy]) endrec ;; 117 [Nn][Oo]|[Nn]|*) exit ;; 118 esac 119 fi 120 } 121 122 case "$TYPE" in 123 video) askendrec ; video ; exit ;; 124 gif) askendrec ; gif ; exit ;; 125 audio) askendrec ; audio ; exit ;; 126 webcam) askendrec ; webcam ; exit ;; 127 end) endrec ; exit ;; 128 esac