Skip to content

Instantly share code, notes, and snippets.

@dylan-sessler
Created September 19, 2022 02:24
Show Gist options
  • Save dylan-sessler/1deb5fa3ff0ba34f5edf37cc21631b19 to your computer and use it in GitHub Desktop.
Save dylan-sessler/1deb5fa3ff0ba34f5edf37cc21631b19 to your computer and use it in GitHub Desktop.
screen record
#!/bin/bash
less_noisy='-hide_banner -y -loglevel error'
screengrab_cmd='x11grab'
framerate=10
screen_resolution=$(xdpyinfo | awk '/dimensions:/ { print $2 }')
monitor_location=':0.0'
audio_input='default'
color_format='yuv420p'
quality_level='33' # default 23, higher worse, lower better. Common range is 18-33
compression_speed='veryslow' # slower compression means a smaller output file at the same quality
tmp_file='/tmp/raw.mkv'
if [[ $# == 0 ]]; then
now=$(date "+%Y%m%d%H%M%S")
output_file="${HOME}/Videos/${now}.mkv"
else
output_file="$1"
fi
function main {
validate_input "$@"
start_recording_noise &
record "$@"
end_recording_noise &
compress "$@"
}
function test {
record
compress
review
}
function record {
ffmpeg $less_noisy \
-f $screengrab_cmd \
-framerate $framerate \
-s $screen_resolution \
-i $monitor_location \
-f alsa \
-i $audio_input \
-c:v rawvideo \
-c:a pcm_s16le \
-pix_fmt $color_format \
$tmp_file
}
function compress {
# -crf 33 cuts the size by about half
# -preset veryslow cuts the size by about 20%
# -tune stillimage actually increase size by a bit, not sure if it has an effect on quality?
ffmpeg $less_noisy \
-i $tmp_file \
-crf $quality_level \
-preset $compression_speed \
-tune stillimage \
$output_file
}
function review {
xdg-open $output_file
ffprobe -hide_banner $output_file
ls -hl $output_file
}
function validate_input {
if [[ $# > 1 ]]; then
echo "This script takes zero or one parameter, not $#"
exit 1
fi
if [[ $# == 1 && "$1" != *@(mkv|mp4) ]]; then
echo "File suffix doesn't match one of the allowed types. They are: .mkv, .mp4"
exit 2
fi
}
function start_recording_noise {
export XDG_RUNTIME_DIR="/run/user/1000"
path_to_sound="$(dirname ${0})/sounds/click.mp3"
mplayer $path_to_sound -volume 100 &> /dev/null
}
function end_recording_noise {
export XDG_RUNTIME_DIR="/run/user/1000"
path_to_sound="$(dirname ${0})/sounds/camera-click.wav"
mplayer $path_to_sound -volume 100 &> /dev/null
}
main "$@"
# test
# maybe useful stuff
# -thread_queue_size 4096 \
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment