misc-scripts/ffrec

44 lines
1.2 KiB
Bash
Executable File

#!/bin/sh
set -eu
OUTFILE="$HOME/pics/screenshots/$(date +"%Y-%m-%d_%H-%M-%S").mp4"
LOCKFILE="/tmp/ffrec.pid"
[ -s "$LOCKFILE" ] && xargs kill < "$LOCKFILE" && notify-send "Recording saved to $OUTFILE" && rm -f "$LOCKFILE" && exit 0
errecho() {
>&2 echo "$@"
}
usage() {
errecho "usage: ${0##*/} [-h] [-a SOURCE] [-r FPS]"
errecho
errecho " -h: show this message and exit"
errecho " -a: record audio from SOURCE (only with PA / pipewire-pulse) (set to \"\" for alsa output)"
errecho " -r: set recording framerate to FPS (default: 60)"
}
while getopts :ha:r: OPT; do
case "$OPT" in
h) usage; exit 0;;
a) AUDIO="-f pulse -i ${OPTARG:-alsa_output.pci-0000_30_00.6.analog-stereo.monitor}" ;;
r) FPS="$OPTARG" ;;
*) errecho "ffrec: invalid option: -$OPTARG"; exit 1 ;;
esac
done
IFS=" " read -r SIZE OFFSET <<-EOF
$(hacksaw -f "%wx%h %x,%y")
EOF
[ -z "$SIZE" ] && exit 1 # if slop was cancelled don't record
(
flock -n 9
set -f
ffmpeg -thread_queue_size 512 -f x11grab -show_region 1 -framerate "${FPS:-60}" -s "$SIZE" -i "$DISPLAY.0+$OFFSET" ${AUDIO:-} -c:v libx264 -crf 21 -preset:v fast "$OUTFILE" &
set +f
echo "$!" > "$LOCKFILE"
notify-send "Recording Started!"
wait
) 9>"$LOCKFILE"