Changed the worktree to one directory back (i.e. $HOME) and simplified the setup
This commit is contained in:
91
dotfiles/bin/compress-video-with-crf
Normal file
91
dotfiles/bin/compress-video-with-crf
Normal file
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
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
|
||||
|
||||
use_gpu=0
|
||||
|
||||
if [[ "$#" < 3 || "$#" > 6 ]]; then
|
||||
printf "${BOLD}${RED}Usage: $0 <crf value> <filename> <output name> <optional: use-gpu (1|0), defaults to $use_gpu> <optional: start time HH:MM:SS> <optional: end time HH:MM:SS>\n\nIf you want to encode a range of CRF values then use -1 as the crf value.${NORMAL}\n"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
use_crf=$1
|
||||
|
||||
filename=$(basename -- "$2")
|
||||
extension="${filename##*.}"
|
||||
filename="${filename%.*}"
|
||||
|
||||
output_name="$3"
|
||||
|
||||
if [[ "$#" > 3 ]]; then
|
||||
use_gpu=$4
|
||||
if [[ "$#" > 4 ]]; then
|
||||
start_time="$5"
|
||||
if [[ "$#" > 5 ]]; then
|
||||
end_time="$6"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ $use_gpu -ne 0 && $use_gpu -ne 1 ]]; then
|
||||
printf "${BOLD}${RED}Invalid use_gpu value. Got $use_gpu, but expected either 0 or 1${NORMAL}\n"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
function encode() {
|
||||
crf=$1
|
||||
output="${output_name}.$extension"
|
||||
|
||||
printf "\n${YELLOW}${BOLD}Encoding '$filename.$extension' | GPU: $use_gpu | CRF $crf | output: $output | start: $start_time | end: $end_time${NORMAL}\n"
|
||||
|
||||
timing_args=""
|
||||
if [[ $start_time != "" ]]; then
|
||||
timing_args+="-ss $start_time -to $end_time"
|
||||
fi
|
||||
|
||||
if [[ $use_gpu -eq 1 ]]; then
|
||||
# File will be slightly larger than CPU encoding, but it's much faster to transcode and doesn't max out the CPU cores.
|
||||
|
||||
# RTX 3080
|
||||
ffmpeg -y -stats -loglevel level+error $timing_args -vsync 0 -hwaccel cuda -hwaccel_output_format cuda -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"
|
||||
|
||||
# GTX 1070
|
||||
#ffmpeg -y -stats -loglevel level+error $timing_args -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"
|
||||
else
|
||||
ffmpeg -y -stats -loglevel level+error $timing_args -i "$filename.$extension" -c:v libx264 -crf $crf -preset veryfast -profile:v high -level 3.0 -strict -2 "$output"
|
||||
fi
|
||||
printf "\n${GREEN}${BOLD}Finished encoding '$filename.$extension' (CRF $crf) | output name '$output'${NORMAL}\n\n"
|
||||
}
|
||||
|
||||
if [[ $use_crf -ne -1 ]]; then
|
||||
encode $use_crf
|
||||
else
|
||||
printf "\n${YELLOW}${BOLD}Encoding using a range of CRF values.${NORMAL}\n"
|
||||
|
||||
# Bigger crf values == bigger compression.
|
||||
for crf in {25..28}
|
||||
do
|
||||
encode $crf
|
||||
done
|
||||
fi
|
||||
Reference in New Issue
Block a user