#!/bin/sh
set -e

_vidfile="/home/sdk/rec-screen.mkv"
_outfile="/home/sdk/rec-encoded-$(date "+%Y%m%d_%H%M%S").mp4"

_res="1920x1080"
_fps="24"

_vid_in="-framerate ${_fps} -f x11grab -i :0.0"
_vid_proc="-c:v libx264rgb -crf 0 -qp 0 -framerate ${_fps} -sws_flags neighbor -preset ultrafast -tune zerolatency"

_ffmpeg_param="${_vid_in} ${_vid_proc} ${_mic_proc}"

printf 'Press q to stop.\n'
set -x
ffmpeg -y -loglevel error -hide_banner ${_ffmpeg_param} "${_vidfile}"
set +x

printf 'Encoding...\n'
set -x
ffmpeg -hide_banner -y \
    -i "${_vidfile}" \
    -c:v libx264 \
    -crf 21 \
    -s ${_res} \
    -pix_fmt yuv420p \
    -preset fast \
    -movflags faststart \
    -threads 8 \
    "${_outfile}"
set +x
printf '%s\n' "${_outfile}"
