#!/bin/bash

# Video Quality
# The range of the CRF scale is 0–51, where 0 is lossless, 23 is the default,
# and 51 is worst quality possible. A lower value generally leads to higher
# quality, and a subjectively sane range is 17–28
QUALITY=28

if ! command -v slop &> /dev/null
then
    echo "slop command not found. Please install slop."
    exit 1
fi

if [ -z "$1" ]; then
    echo "Usage: $0 <output.mp4>"
    exit 1
fi

filename="$1"

echo "Select the desktop area to record."
coords=$(slop -f "%x %y %w %h" 2>/dev/null)

x=$(echo $coords | awk '{print $1}')
y=$(echo $coords | awk '{print $2}')
width=$(echo $coords | awk '{print $3}')
height=$(echo $coords | awk '{print $4}')

width=$((width / 2 * 2))
height=$((height / 2 * 2))

echo "Recording started. Press 'q' to stop recording."

    #\#-f alsa -i pulse -c:v libx264 -preset ultrafast -crf ${QUALITY} \
    #-c:v libx264  -preset ultrafast -crf ${QUALITY} \
    #-tune zerolatency -pix_fmt yuv420p -f mp4 $filename
ffmpeg -f x11grab -loglevel quiet -s ${width}x${height} -i ${DISPLAY}+$x,$y \
    -c:v libx264 -pix_fmt yuv420p -crf ${QUALITY} -preset ultrafast -f mp4 $filename

echo "Recording stopped."
