dotfiles/dotfiles/bin/trim-video-vbr

125 lines
5.1 KiB
Bash

#!/usr/bin/env bash
# Re-encodes the video using a target quality level and a variable bitrate.
# To have a mostly fixed bitrate with no variable quality, use trim-video-target-rate
# Just note that it'll result in larger files for a similar max bitrate target and the
# quality won't really be noticeably better.
if which tput >/dev/null 2>&1; then
ncolors=$(tput colors)
fi
if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
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)"
else
RED=""
GREEN=""
YELLOW=""
BLUE=""
MAGENTA=""
CYAN=""
BOLD=""
NORMAL=""
fi
filename=$(basename -- "$1")
output_name="$2"
start_time="$3"
end_time="$4"
target_crf="$5"
max_bitrate_mb="$6"
default_crf="20"
default_max_bitrate="6"
if [[ $filename == "" || $output_name == "" || $start_time == "" ]]; then
printf "${BOLD}${RED}Usage: trim-video <filename> <output name> <start time HH:MM:SS> <optional: end time HH:MM:SS, use empty string or 0 for no value> <optional: crf (quality, w/ lower = more compression) - defaults to $default_crf, use 0 for no value> <optional: max bitrate in MB - defaults to ${default_max_bitrate}M>${NORMAL}\n"
exit 1
fi
extension="${filename##*.}"
filename="${filename%.*}"
output="${output_name}.$extension"
if [[ $target_crf == "" || $target_crf == "0" ]] then
target_crf=$default_crf
fi
if [[ $max_bitrate_mb == "" ]] then
max_bitrate_mb=$default_max_bitrate
fi
# bufsize is typically double the maxrate
bufsize=$((max_bitrate_mb * 2))
bufsize="${bufsize}M"
max_bitrate="${max_bitrate_mb}M"
timing_args=""
if [[ $start_time != "" ]]; then
timing_args="-ss $start_time "
fi
if [[ $end_time != "" ]]; then
if [[ $start_time == "0" && $end_time == "0" ]]; then
# We treat a start and end with 0 values as no op.
timing_args=""
elif [[ $end_time != "0" ]]; then
# Handle having a start time but end time is set to 0, can just ignore it and it'll use the remainder of the video.
timing_args+="-to $end_time"
fi
fi
printf "\n${YELLOW}${BOLD}Trimming '$filename.$extension' | output: $output | start: $start_time | end: $end_time | crf: $target_crf | max rate: $max_bitrate | buffer size: $bufsize ${NORMAL}\n"
# You might have issues if the file has multiple video streams or embedded
# subtitles. The -map 0 arg is typically given when copying a video stream, but
# I'm not sure if it's appropriate to use here. If you want to target one of
# the video streams then use `-map 0:v:0` and if you want to re-encode
# subtitles then include `-c:s mov_text` with either of the `-map` args.
# -preset 3 and 4 are the fastest on my 3080 and have the same output file
# size. p3 seems slightly faster. Apparently 7 is the best for 30 series
# according to chatgpt (lol) but it was slow and compressed.
# -accurate_seek doesn't seem to be needed when -ss is after -i, only when -ss
# is before -i. Having -ss after is apparently a way of enabling accurate seeking.
# The catch is that if the trim doesn't start at 0, it can be a long wait before
# the encoder gets to the portion you want to start trimming from. To speed this up
# we are putting -ss and -accurate_seek before -i ... the video duration might be
# slightly off from this but so far I haven't seen any seeking issues. If they come
# up then we can move -ss to after -i again and delete -accurate_seek.
# I'm using -rc:v vbr -cq:v 20 (higher = more compression) to have a variable
# bit-rate with a quality target. This gives us a good looking re-encoding that
# does a good job preserving the quality of the source video (at least for
# twitch streams I'm trimming) but the bitrate might be higher or lower than
# the source. The quality metric will fluctuate during encoding because NVENC
# does't use a fixed quantization parameter. -cq:v is a target, not a constraint.
# So you can have quite the fluctuating (or high) bitrate with these two params.
# In order to set a birate cap that is prioritized over visual fidelity, we can
# add in:
# -maxrate:v -bufsize:v
# (with bufsize typically being double the maxrate)
#
# Using this with the vbr and crf params tells the NVENC encoder to try to
# encode at a perceptual quality level around CQ=20 but keep the bitrate under
# 6000 kbps (constrained VBR). If a scene is too complex to maintain CQ=20
# within the bitrate cap, the encoder will:
# * Raise the quantizer (lower quality) to obey the -maxrate
# * Result: CQ metric fluctuates upward
# If a scene is simple:
# * Encoder can exceed CQ=20 (e.g. better quality) while staying under maxrate
# * Result: CQ metric fluctuates downward
#
# To have a mostly fixed bitrate with no variable quality, use trim-video-target-rate
time ffmpeg -y -stats -loglevel level+error -hwaccel cuda -hwaccel_output_format cuda $timing_args -accurate_seek -i "$filename.$extension" -c:v h264_nvenc -profile:v high -preset 3 -rc:v vbr -cq:v $target_crf -maxrate:v $max_bitrate -bufsize:v $bufsize -c:a copy -movflags +faststart "$output"
printf "\n${GREEN}${BOLD}Finished trimming${NORMAL}\n\n"