dotfiles/dotfiles/bin/trim-video-target-rate

93 lines
3.6 KiB
Bash

#!/usr/bin/env bash
# Re-encodes the video using a constrained bitrate/output size. If you want to
# To target the visual quality with a variable bitrate, use trim-video-vbr
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"
target_bitrate_mb="$3"
start_time="$4"
end_time="$5"
if [[ $filename == "" || $output_name == "" || $target_bitrate_mb == "" || $start_time == "" ]]; then
printf "${BOLD}${RED}Usage: trim-video <filename> <output name> <target bitrate in MB, e.g. 6> <start time HH:MM:SS> <optional: end time HH:MM:SS, use empty string or 0 for no value>${NORMAL}\n"
exit 1
fi
extension="${filename##*.}"
filename="${filename%.*}"
output="${output_name}.$extension"
# bufsize is typically double the maxrate
bufsize=$((target_bitrate_mb * 2))
bufsize="${bufsize}M"
bitrate="${target_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 | max rate: $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 -b:v, -maxrate:v, and -bufsize:v to constrain the bitrate and
# indirectly control quality. The bitrate won't be fixed exactly to -b:v
# unless using CBR, but NVENC will attempt to average around it and cap the
# bitrate at -maxrate:v, using -bufsize:v as a smoothing buffer.
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 -b:v $bitrate -maxrate:v $bitrate -bufsize:v $bufsize -c:a copy -movflags +faststart "$output"
printf "\n${GREEN}${BOLD}Finished trimming${NORMAL}\n\n"