From c0d8763d0b1b85e01516b1786f73192f8221b6c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ali=20Furkan=20Y=C4=B1ld=C4=B1z?= Date: Wed, 11 Jan 2023 19:44:42 +0300 Subject: [PATCH] add avt --- README.md | 4 +- shell/bash/avt/README.md | 36 +++++++++++ shell/bash/avt/avt | 132 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 shell/bash/avt/README.md create mode 100755 shell/bash/avt/avt diff --git a/README.md b/README.md index 3821481..05fb494 100644 --- a/README.md +++ b/README.md @@ -8,5 +8,5 @@ All code is licensed under GPL-3.0 unless specified otherwise. | C | Bash | JS | | ---------------------- | :-------------------------: | --- | -| [unlink.c](c/unlink.c) | [arshit](shell/bash/arshit) | | -| [cecho.c](c/cecho.c) | | | +| [unlink.c](c/unlink.c) | [avt](shell/bash/avt) | | +| [cecho.c](c/cecho.c) | [arshit](shell/bash/arshit) | | diff --git a/shell/bash/avt/README.md b/shell/bash/avt/README.md new file mode 100644 index 0000000..c8bd406 --- /dev/null +++ b/shell/bash/avt/README.md @@ -0,0 +1,36 @@ +# avt: ali's (h264) video tool + + avt: ali's (h264) video tool + Usage: ./avt rec|record + ./avt nvid|normalize + ./avt help + Environment variables: + avt_cl: enable opencl [set to enable] + + Commands: + + ./avt rec / record + Record screen and audio in very high quality + Usage: ./avt rec [output directory] + Enviroment variables: + rec_fps: set framerate (default: 60) + rec_res: set resolution (default: 1920x1080) + rec_crf: set crf, use 0 for lossless (default: 6) + rec_adev: set pulseaudio source devices (default: default and first running monitor) [array] + rec_ar: set audio samplerate (default: 192000) + rec_normalize: immediately start nvid after recording ends [set to enable] + + ./avt nvid / normalize + Normalize and compress video & audio + Usage: ./avt nvid [outfile] + Environment variables: + nvid_preset: set preset (default: veryslow) + nvid_vrate: replace -crf options + nvid_crf: set crf value (default: 21) + 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: aac) + nvid_ba: set audio bitrate (default: 160k) + nvid_ar: set audio samplerate (default: 48000) + \ No newline at end of file diff --git a/shell/bash/avt/avt b/shell/bash/avt/avt new file mode 100755 index 0000000..a927af0 --- /dev/null +++ b/shell/bash/avt/avt @@ -0,0 +1,132 @@ +#!/usr/bin/env bash + +# avt: ali's (h264) vid tool +# Licensed under GPL-3.0, see the end of file for details +# https://gitlab.com/alifurkany/snippets/-/blob/main/shell/bash/avt + +print_help() { + cat < [outfile] +Environment variables: +nvid_preset: set preset (default: veryslow) +nvid_vrate: replace -crf options +nvid_crf: set crf value (default: 21) +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: aac) +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 :0 \ + $pulse_inputs \ + -filter_complex "$audio_filter" \ + -c:a pcm_s16le -ar "${rec_ar:-192000}" \ + -c:v libx264rgb -x264opts opencl -profile:v high444 -crf "${rec_crf:-4}" -tune film -preset ultrafast -r 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:-n_$(grep -oP '^.+(?=\..+)' <<<"$1").mp4}" + echo "Saving to $OUTPUTFILE" + + 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} ${nvid_cl+-x264opts opencl} -preset "${nvid_preset:-veryslow}" ${nvid_vrate:--crf "${nvid_crf:-21}"} \ + -pix_fmt "${nvid_pixfmt:-yuv420p}" -profile:v "${nvid_profile:-high}" \ + -c:a "${nvid_ac:-aac}" -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 $?