Skip to content

Instantly share code, notes, and snippets.

@josephsdavid
Created June 15, 2019 16:33
Show Gist options
  • Save josephsdavid/8c38a97cb223826794df90fb812fbe7d to your computer and use it in GitHub Desktop.
Save josephsdavid/8c38a97cb223826794df90fb812fbe7d to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# Source:
# https://gitlab.com/gaugek/dots/
#
dir="$HOME/Videos/recordings"
name="$(date '+%Y-%m-%d.%H:%M:%S')" # Needs .mp4 after this is listed in the actual recording part
re="\e[31m"
gr="\e[32m"
ye="\e[33m"
cy="\e[36m"
bl="\e[34m"
pu="\e[35m"
n="\e[0m"
b="\e[1m"
help="
Gauge Krahe's ${ye}(https://gitlab.com/gaugek)${n} ffmpeg recording script
${b}CLI Syntax: ${n}
${ye}\$${n} ${gr}record ${bl}<Offset X> <Offset Y> <Width> <Height> <Frames Per Second> <Desktop audio (yes|no)>${n}
When ran without any arguments (Or with missing arguments), it will get the (missing) options with dmenu
${b}CLI Arguments: ${n}
${b}-h / --help: ${n} Displays what you are viewing right now
${b}-d / --display: ${n} Record selected display
${b}-s / --slop: ${n} Use slop to get region to record
${b}-n / --no-nvenc:${n} Disable nvenc (for nvidia cards)
"
[[ -d $dir ]] || mkdir -p $dir
opts=`getopt -o d:smhn --long display:,slop,manual,help,no-nvenc -- "$@"`
eval set -- "$opts"
while true ; do
case "$1" in
-d|--display) [[ -n $2 ]] && DoM="Display" && display_select="$2"; shift 2;;
-s|--slop) DoM="Slop" ; shift 1;;
-m|--manual) DoM="Manual"; shift 1;;
-h|--help) echo -e "$help"; exit ;;
-n|--no-nvenc) nvenc=" "; shift 1;;
--) shift; break;;
esac
done
if [[ -z $1 ]] && [[ -z $DoM ]]; then
DoM="$(echo -e "Display\nManual\nSlop" | dmenu -i -p "Automatic dimensions from display, Manual dimensions, or select a region?")"
elif [[ -n $1 ]] && [[ -z $DoM ]]; then
DoM="Manual"
fi
if [[ ${DoM} = Display ]]; then
displays="$(xrandr -q --current | grep -iv " \|disconnect\|screen" | awk '{gsub("primary ", "");print $1 " " $3}')"
if [[ -z $display_select ]]; then
display_select="$(echo -e "$displays" | awk '{print $2}' | dmenu -i -p "Display to record")"
fi
display_res="$(echo -e "$displays" | grep -i $display_select | awk '{gsub("x", " "); gsub("+", " "); print $2 " " $3 " " $4 " " $5}' )"
width="$(echo $display_res | awk '{print $1}' )"
height="$(echo $display_res | awk '{print $2}' )"
offsetX="$(echo $display_res | awk '{print $3}' )"
offsetY="$(echo $display_res | awk '{print $4}' )"
rate="$(echo -e "30\n60" | dmenu -i -p "Framerate")"
audio="$(echo -e "no\nyes" | dmenu -i -p "Desktop audio?")"
elif [[ ${DoM} = Slop ]]; then
slop="$(slop)"
slop1="$(echo $slop | awk '{ gsub("x", " "); gsub("+", " "); print}')"
width="$(echo $slop1 | awk '{print $1}' )"
height="$(echo $slop1 | awk '{print $2}' )"
offsetX="$(echo $slop1 | awk '{print $3}' )"
offsetY="$(echo $slop1 | awk '{print $4}' )"
rate="$(echo -e "30\n60" | dmenu -i -p "Framerate")"
audio="$(echo -e "no\nyes" | dmenu -i -p "Desktop audio?")"
elif [[ ${DoM} = Manual ]]; then
[ -n "$1" ] && offsetX="$1" || offsetX="$(echo -e "1440\n0" | dmenu -i -p "Offset for the X axis")"
[ -n "$2" ] && offsetY="$2" || offsetY="$(echo -e "0" | dmenu -i -p "Offset for the Y axis")"
[ -n "$3" ] && width="$3" || width="$(echo -e "1920\n1366\n1440" | dmenu -i -p "Width")"
[ -n "$4" ] && height="$4" || height="$(echo -e "1080\n768\n900" | dmenu -i -p "Height")"
[ -n "$5" ] && rate="$5" || rate="$(echo -e "30\n60" | dmenu -i -p "Framerate")"
[ -n "$6" ] && audio="$6" || audio="$(echo -e "no\nyes" | dmenu -i -p "Desktop audio?")"
fi
if [[ $audio = yes ]]; then
audio="-f pulse -i `pacmd list-sources | grep -PB 1 "analog.*monitor>" | head -n 1 | sed 's/.* //g'`"
else
audio=""
fi
RtR="$(echo -e "No\nYes" | dmenu -i -p "Are you ready to record?")"
[[ -n $(lspci | grep -i nvidia) ]] && [[ -z $nvenc ]] && nvenc="-vcodec h264_nvenc"
if [[ $RtR = Yes ]]; then
notify-send -t 2000 "recording started"
ffmpeg -y $(printf " $audio") \
-f x11grab -draw_mouse 1 \
-s ${width}x${height} -r ${rate} -i :0.0+${offsetX},${offsetY} \
-qscale 0 $(printf " $nvenc") \
"${dir}/${name}".mp4
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment