dotfiles/dotfiles/bin/join-video

67 lines
2.3 KiB
Bash

#!/usr/bin/env bash
# Re-encodes the video to get a more accurate timeline. Same settings as trim-video-vbr.
# If you want fast video joining at the expense of accuracy then use join-video-fast.
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="$2"
target_crf="$3"
max_bitrate_mb="$4"
default_crf="20"
default_max_bitrate="6"
if [[ $filename == "" || $output == "" ]]; then
printf "${BOLD}${RED}Usage: create a text file that lists the input video paths on separate lines using the format: file '/path/to/video'. Then call:\n\njoin-video <list filename> <output name> <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="${output##*.}"
if [[ $extension == $output ]]; then
printf "${BOLD}${RED}output arg should have an extension!\n\nUsage: join-video <list filename> <output name> <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
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"
printf "\n${YELLOW}${BOLD}Joining contents of '$filename' | output: $output | crf: $target_crf | max rate: $max_bitrate | buffer size: $bufsize ${NORMAL}\n"
time ffmpeg -y -stats -loglevel level+error -hwaccel cuda -hwaccel_output_format cuda -f concat -safe 0 -accurate_seek -i "$filename" -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 joining${NORMAL}\n\n"