Skip to content

Instantly share code, notes, and snippets.

@pedrovhb
Last active February 28, 2023 17:32
Show Gist options
  • Save pedrovhb/daa5973e8074cb711c560f0169c80852 to your computer and use it in GitHub Desktop.
Save pedrovhb/daa5973e8074cb711c560f0169c80852 to your computer and use it in GitHub Desktop.
Bash CLI util to record the screen with VAAPI hardware acceleration. Writes to two audio streams - one with the default input device (microphone), and one with the desktop audio.
#!/usr/bin/env nix-shell
#! nix-shell -i bash -p ffmpeg slop
set -e
output_file="$HOME/Videos/recording_$(date +%Y_%m_%d__%H_%M_%S).mp4"
qp=23
usage() {
echo "Usage: $(basename $0) [OPTIONS]"
echo
echo "Options:"
echo " -q QP Specify the QP value for the H.264 video codec (default: $qp)"
echo " -n NAME Specify the output file name (default: $(basename $output_file))"
echo " -d DIR Specify the output directory (default: $(dirname $output_file))"
echo " -o FILE Specify the absolute output file path"
echo " -h Show this help message"
}
while getopts "q:n:d:o:h" opt; do
case $opt in
q) qp="$OPTARG" ;;
n) file_name="$OPTARG" ;;
d) output_dir="$OPTARG" ;;
o) output_file="$OPTARG" ;;
h) usage; exit 0 ;;
*) usage; exit 1 ;;
esac
done
if [ -n "$file_name" ] && [ -n "$output_dir" ]; then
output_file="$output_dir/$file_name"
elif [ -n "$file_name" ]; then
output_file="$(dirname $output_file)/$file_name"
elif [ -n "$output_dir" ]; then
output_file="$output_dir/$(basename $output_file)"
fi
echo "Click a window or drag across an area to select the recording target."
ffmpeg -hwaccel vaapi -hwaccel_output_format vaapi \
-f x11grab $(slop -f '-video_size %wx%h -i +%x,%y') \
-f pulse -i default \
-f pulse -i alsa_output.pci-0000_00_1f.3.analog-stereo.monitor \
-c:a aac -b:a 128k \
-map 0:v -map 1:a -map 2:a \
-vcodec h264_vaapi -qp $qp \
-vf 'format=nv12,hwupload' "$output_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment