2020-07-25 23:39:09 +00:00
#!/usr/bin/env bash
2020-06-10 15:23:38 +00:00
if which tput >/dev/null 2>&1; then
2020-07-26 17:58:29 +00:00
ncolors=$(tput colors)
2020-06-10 15:23:38 +00:00
fi
if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
2020-07-26 17:58:29 +00:00
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BLUE="$(tput setaf 4)"
MAGENTA="$(tput setaf 5)"
CYAN="$(tput setaf 6)"
BOLD="$(tput bold)"
NORMAL="$(tput sgr0)"
2020-06-10 15:23:38 +00:00
else
2020-07-26 17:58:29 +00:00
RED=""
GREEN=""
YELLOW=""
BLUE=""
MAGENTA=""
CYAN=""
BOLD=""
NORMAL=""
2020-06-10 15:23:38 +00:00
fi
if [[ $1 == "" ]]; then
printf "\n${BOLD}Usage: $0 <filename> <optional crf value> <optional output name>\n - If you want to encode a range of CRF values then use -1 as the crf value.${NORMAL}\n"
exit 1
fi
filename=$(basename -- "$1")
extension="${filename##*.}"
filename="${filename%.*}"
use_crf=$2
default_crf=25 # Programming vids have pretty crisp text @ crf 25.
output_name="$3"
2020-12-16 15:56:06 +00:00
use_cpu=$4
2020-06-10 15:23:38 +00:00
function encode() {
crf=$1
if [[ $output_name == "" ]]; then
output="${filename}_REDUCED_CRF-${crf}.$extension"
else
output="${output_name}.$extension"
fi
2020-11-23 19:14:03 +00:00
printf "\n${YELLOW}${BOLD}Encoding '$filename.$extension' with CRF $crf | output: $output${NORMAL}\n"
2020-12-16 15:56:06 +00:00
if [[ $use_cpu -eq 1 ]]; then
ffmpeg -i "$filename.$extension" -c:v libx264 -crf $crf -preset veryfast -profile:v baseline -level 3.0 -strict -2 "$output"
else
# File will be slightly larger than CPU encoding, but it's much faster to transcode and doesn't max out the CPU cores.
2021-05-27 21:37:29 +00:00
ffmpeg -y -vsync 0 -hwaccel cuvid -c:v h264_cuvid -i "$filename.$extension" -c:a copy -c:v h264_nvenc -profile:v high -rc:v vbr_hq -cq:v $crf -b:v 5M -maxrate 5M -max_muxing_queue_size 9999 "$output"
2020-12-16 15:56:06 +00:00
fi
2020-11-23 19:14:03 +00:00
printf "\n${GREEN}${BOLD}Done encoding '$filename.$extension' with CRF $crf${NORMAL}\n\n"
2020-06-10 15:23:38 +00:00
}
if [[ $use_crf == "" ]]; then
use_crf=$default_crf
fi
if [[ $use_crf == -1 ]]; then
2020-11-23 19:14:03 +00:00
printf "\n${YELLOW}${BOLD}Encoding using a range of CRF values.${NORMAL}\n"
2020-06-10 15:23:38 +00:00
# Bigger crf values == bigger compression.
for crf in {25..28}
do
encode $crf
done
else
encode $use_crf
fi