snippets/shell/bash/avt/avt
Ali Furkan Yıldız f19427da23 cleanup
2023-01-11 21:35:31 +03:00

134 lines
3.6 KiB
Bash
Executable file

#!/usr/bin/env bash
# avt: ali's (h264) vid tool
# https://gitlab.com/alifurkany/snippets/-/tree/main/shell/bash/avt
print_help() {
cat <<EOF
avt: ali's (h264) video tool
Usage: $0 rec|record
$0 nvid|normalize
$0 help
Environment variables:
avt_cl: enable opencl [set to enable]
Commands:
$0 rec / record
Record screen and audio in very high quality
Usage: $0 rec
Enviroment variables:
rec_out: set output file (default: $HOME/Desktop/$(date +%F_%H-%M-%S).mkv)
rec_fps: set framerate (default: 60)
rec_res: set resolution (default: 1920x1080)
rec_preset: set preset (default: ultrafast)
rec_vrate: replace -crf options
rec_crf: set crf, use 0 for lossless (default: 10)
rec_adev: space separated pulseaudio source devices (default: default and first running monitor)
rec_ar: set audio samplerate (default: 96000)
rec_normalize: immediately start nvid after recording ends [set to enable]
$0 nvid / normalize
Normalize video & audio for backwards compatibility and lower file size
Usage: $0 nvid <file> [outfile]
Environment variables:
nvid_preset: set preset (default: veryslow)
nvid_vrate: replace -crf options
nvid_crf: set crf value (default: 19)
nvid_rgb: use libx264rgb [set to enable]
nvid_pixfmt: set pixel format (default: yuv420p)
nvid_profile: set profile (default: high)
nvid_ac: set audio codec (default: libopus)
nvid_ba: set audio bitrate (default: 160k)
nvid_ar: set audio samplerate (default: 48000)
EOF
}
record() {
# shellcheck disable=SC2206
devices=(${rec_adev:-"default" "$(pactl list sinks | perl -e '
my $in = "";
while (<>) { $in .= $_; }
my @sinks = split("\n\n", $in);
foreach (@sinks) {
if (/State: RUNNING/) {
/Monitor Source: ([^\n]*)/;
print $1 . "\n";
exit;
}
}
')"})
# shellcheck disable=SC2206
# remove empty elements from array
devices=(${devices[@]})
pulse_inputs=""
audio_maps=""
audio_filter=""
i=0
for device in "${devices[@]}"; do
i=$(("$i" + 1))
pulse_inputs="$pulse_inputs -f pulse -i $device"
audio_maps="$audio_maps -map $i:a"
audio_filter="$audio_filter""[$i]"
done
audio_filter="$audio_filter amix=inputs=$i [merged]"
OUTPUTFILE="${1:-$HOME/Desktop/$(date +%F_%H-%M-%S).mkv}"
echo "Saving to $OUTPUTFILE"
# shellcheck disable=SC2086
ffmpeg -y -thread_queue_size 64 \
-f x11grab -r "${rec_fps:-60}" -s "${rec_res:-1920x1080}" -i "${rec_display:-$DISPLAY}" \
$pulse_inputs \
-filter_complex "$audio_filter" \
-c:a pcm_s16le -ar "${rec_ar:-96000}" \
-c:v libx264rgb ${avt_cl+-x264opts opencl} -profile:v high444 -preset "${rec_preset:-ultrafast}" ${rec_vrate:--crf "${rec_crf:-10}"} -tune zerolatency -r ${rec_fps:-60} \
-map 0:v $audio_maps -map '[merged]:a' \
"$OUTPUTFILE"
EXITCODE=$?
if { [[ "$EXITCODE" == 0 ]] || [[ "$EXITCODE" == 255 ]]; } && [[ "${rec_normalize+x}" == "x" ]]; then
normalize "$OUTPUTFILE"
return $?
else
return $EXITCODE
fi
}
normalize() {
OUTPUTFILE="${2:-$1_n.mp4}"
echo "Saving to $OUTPUTFILE"
# shellcheck disable=SC2086
ffmpeg -y \
${nvid_cl+-hwaccel opencl} -i "$1" \
${nvid_cl+-init_hw_device opencl=ocl -filter_hw_device ocl} \
-c:v libx264${nvid_rgb+rgb} ${avt_cl+-x264opts opencl} -preset "${nvid_preset:-veryslow}" ${nvid_vrate:--crf "${nvid_crf:-19}"} \
-pix_fmt "${nvid_pixfmt:-yuv420p}" -profile:v "${nvid_profile:-high}" \
-c:a "${nvid_ac:-libopus}" -b:a "${nvid_ba:-160k}" -ar "${nvid_ar:-48000}" \
-map 0 \
"$OUTPUTFILE"
return $?
}
case "$1" in
rec | record)
# shellcheck disable=SC2068
record ${@:2}
;;
nvid | normalize)
# shellcheck disable=SC2068
normalize ${@:2}
;;
help)
print_help
;;
*)
echo "$0: Error: No command specified. See \`$0 help\` for more details."
false
;;
esac
exit $?