Improve some video encoding scripts, making use of the GPU again

This commit is contained in:
Michael Campagnaro 2025-08-16 16:22:47 -04:00
parent 240d38eea0
commit 676831426f
10 changed files with 336 additions and 62 deletions

125
.aliases
View File

@ -794,20 +794,26 @@ download_twitch_vid() {
# e.g. --cookies cookies.txt --referer https://gillyandkeeves.tv https://player.vimeo.com/video/756941969 # e.g. --cookies cookies.txt --referer https://gillyandkeeves.tv https://player.vimeo.com/video/756941969
# The vid ID can be found by looking at the embed's iframe src attribute. # The vid ID can be found by looking at the embed's iframe src attribute.
download_vimeo_vid() { download_vimeo_vid() {
local shortname="$1" local format="$1"
local compress="$2" local shortname="$2"
local transcribe="$3" local compress="$3"
local format="$4" local transcribe="$4"
local make_folder="$5" local make_folder="$5"
local url="$6" local url="$6"
shift 5 shift 6
local opts="$@" local opts="$@"
if [[ $format == "" || $url == "" ]]; then if [[ $url == "" ]]; then
error "Usage: <format> <make folder?> <url> <optional args>" error "Usage: <make folder?> <url> <optional args>"
return return
fi fi
if [[ $format == "" ]]; then
printf "${BOLD}No format given; using best available.${NORMAL}\n"
# Download best mp4 video.
format="bv*[ext=mp4]+ba[ext=m4a]"
fi
if [[ $shortname == "1" || $compress == "1" || $transcribe == "1" ]]; then if [[ $shortname == "1" || $compress == "1" || $transcribe == "1" ]]; then
printf "${BOLD}Downloading Vimeo vid " printf "${BOLD}Downloading Vimeo vid "
if [[ $shortname == "1" ]]; then printf "| ${YELLOW}using short name${NORMAL}${BOLD} "; fi if [[ $shortname == "1" ]]; then printf "| ${YELLOW}using short name${NORMAL}${BOLD} "; fi
@ -871,7 +877,7 @@ download_twitter_vid() {
local format="$1" local format="$1"
local make_folder="$2" local make_folder="$2"
local url="$3" local url="$3"
local vid_name="$4" local vid_name="$4" # optional
if [[ $url == "" ]]; then if [[ $url == "" ]]; then
error "Usage: <make folder?> <url> <optional filename> <optional args>" error "Usage: <make folder?> <url> <optional filename> <optional args>"
@ -882,12 +888,24 @@ download_twitter_vid() {
local opts="" local opts=""
if [[ $# > 3 ]]; then
# Since the name is optional and we might pass options, like --cookie etc
# we need to check if the vid name is set to an option.
if [[ $vid_name == -* ]]; then
echo "vid name is an option"
shift 3
vid_name=""
opts="$@"
else
shift 4
opts="$@"
fi
fi
if [[ $vid_name == "" ]]; then if [[ $vid_name == "" ]]; then
local name_format="%(upload_date>%Y-%m-%d)s-%(title)s-twitter-%(id)s" local name_format="%(upload_date>%Y-%m-%d)s-%(title)s-twitter-%(id)s"
else else
local name_format="%(upload_date>%Y-%m-%d)s-${vid_name}-twitter-%(id)s" local name_format="%(upload_date>%Y-%m-%d)s-${vid_name}-twitter-%(id)s"
shift 4
opts="$@"
fi fi
if [[ $make_folder == "1" ]]; then if [[ $make_folder == "1" ]]; then
@ -924,10 +942,9 @@ download_twitter_vid() {
# Download Instagram videos. # Download Instagram videos.
download_instagram_vid() { download_instagram_vid() {
local transcribe="$1" local transcribe="$1"
local make_folder="$2" local make_folder="$2"
local url="$3" local url="$3"
local vid_name="$4" local vid_name="$4" # optional
if [[ $url == "" ]]; then if [[ $url == "" ]]; then
error "Usage: <make folder?> <url> <optional filename> <optional args>" error "Usage: <make folder?> <url> <optional filename> <optional args>"
@ -938,12 +955,24 @@ download_instagram_vid() {
local opts="" local opts=""
if [[ $# > 3 ]]; then
# Since the name is optional and we might pass options, like --cookie etc
# we need to check if the vid name is set to an option.
if [[ $vid_name == -* ]]; then
echo "vid name is an option"
shift 3
vid_name=""
opts="$@"
else
shift 4
opts="$@"
fi
fi
if [[ $vid_name == "" ]]; then if [[ $vid_name == "" ]]; then
local name_format="%(upload_date>%Y-%m-%d)s-%(title)s-ig-%(id)s" local name_format="%(upload_date>%Y-%m-%d)s-%(title)s-ig-%(id)s"
else else
local name_format="%(upload_date>%Y-%m-%d)s-${vid_name}-ig-%(id)s" local name_format="%(upload_date>%Y-%m-%d)s-${vid_name}-ig-%(id)s"
shift 4
opts="$@"
fi fi
if [[ $make_folder == "1" ]]; then if [[ $make_folder == "1" ]]; then
@ -1069,29 +1098,46 @@ alias yt-list-desc='download_youtube_uploads_list 1 '
alias yt='download_youtube_vid "" $SHORTNAME_OFF $TRANSCRIBE_OFF' alias yt='download_youtube_vid "" $SHORTNAME_OFF $TRANSCRIBE_OFF'
alias yt-shortname='download_youtube_vid "" $SHORTNAME_ON $TRANSCRIBE_OFF' alias yt-shortname='download_youtube_vid "" $SHORTNAME_ON $TRANSCRIBE_OFF'
alias yt-1440='download_youtube_vid "620+140" $SHORTNAME_OFF $TRANSCRIBE_OFF'
alias yt-1440p60='download_youtube_vid "400+140" $SHORTNAME_OFF $TRANSCRIBE_OFF' alias yt-4k='download_youtube_vid "625+234" $SHORTNAME_OFF $TRANSCRIBE_OFF'
alias yt-1440-shortname='download_youtube_vid "620+140" $SHORTNAME_OFF $TRANSCRIBE_OFF' alias yt-4k-shortname='download_youtube_vid "625+234" $SHORTNAME_ON $TRANSCRIBE_OFF'
alias yt-1080='download_youtube_vid "137+140" $SHORTNAME_OFF $TRANSCRIBE_OFF'
alias yt-1080-shortname='download_youtube_vid "137+140" $SHORTNAME_ON $TRANSCRIBE_OFF' alias yt-1440='download_youtube_vid "620+234" $SHORTNAME_OFF $TRANSCRIBE_OFF'
alias yt-720='download_youtube_vid "136+140" $SHORTNAME_OFF $TRANSCRIBE_OFF' alias yt-1440p60='download_youtube_vid "400+234" $SHORTNAME_OFF $TRANSCRIBE_OFF'
alias yt-720-shortname='download_youtube_vid "136+140" $SHORTNAME_ON $TRANSCRIBE_OFF' alias yt-1440-shortname='download_youtube_vid "620+234" $SHORTNAME_OFF $TRANSCRIBE_OFF'
# Premium
alias yt-1080p='download_youtube_vid "616+234" $SHORTNAME_OFF $TRANSCRIBE_OFF'
alias yt-1080p-shortname='download_youtube_vid "616+234" $SHORTNAME_ON $TRANSCRIBE_OFF'
# Normal
alias yt-1080='download_youtube_vid "270+234" $SHORTNAME_OFF $TRANSCRIBE_OFF'
alias yt-1080-shortname='download_youtube_vid "270+234" $SHORTNAME_ON $TRANSCRIBE_OFF'
alias yt-720='download_youtube_vid "136+234" $SHORTNAME_OFF $TRANSCRIBE_OFF'
alias yt-720-shortname='download_youtube_vid "136+234" $SHORTNAME_ON $TRANSCRIBE_OFF'
#TRANSCRIPTION ON #TRANSCRIPTION ON
alias ytt='download_youtube_vid "" $SHORTNAME_OFF $TRANSCRIBE_ON' alias ytt='download_youtube_vid "" $SHORTNAME_OFF $TRANSCRIBE_ON'
alias yt-shortname-t='download_youtube_vid "" $SHORTNAME_ON $TRANSCRIBE_ON' alias yt-shortname-t='download_youtube_vid "" $SHORTNAME_ON $TRANSCRIBE_ON'
alias yt-1440-t='download_youtube_vid "620+140" $SHORTNAME_OFF $TRANSCRIBE_ON' alias yt-4k-t='download_youtube_vid "625+234" $SHORTNAME_OFF $TRANSCRIBE_ON'
alias yt-1440-shortname-t='download_youtube_vid "620+140" $SHORTNAME_ON $TRANSCRIBE_ON' alias yt-4k-shortname-t='download_youtube_vid "625+234" $SHORTNAME_ON $TRANSCRIBE_ON'
alias yt-1080-t='download_youtube_vid "137+140" $SHORTNAME_OFF $TRANSCRIBE_ON' alias yt-1440-t='download_youtube_vid "620+234" $SHORTNAME_OFF $TRANSCRIBE_ON'
alias yt-1080-shortname-t='download_youtube_vid "137+140" $SHORTNAME_ON $TRANSCRIBE_ON' alias yt-1440-shortname-t='download_youtube_vid "620+234" $SHORTNAME_ON $TRANSCRIBE_ON'
alias yt-720-t='download_youtube_vid "136+140" $SHORTNAME_OFF $TRANSCRIBE_ON' alias yt-1080p-t='download_youtube_vid "616+234" $SHORTNAME_OFF $TRANSCRIBE_ON'
alias yt-720-shortname-t='download_youtube_vid "136+140" $SHORTNAME_ON $TRANSCRIBE_ON' alias yt-1080p-shortname-t='download_youtube_vid "616+234" $SHORTNAME_ON $TRANSCRIBE_ON'
alias yt-1080-t='download_youtube_vid "270+234" $SHORTNAME_OFF $TRANSCRIBE_ON'
alias yt-1080-shortname-t='download_youtube_vid "270+234" $SHORTNAME_ON $TRANSCRIBE_ON'
alias yt-720-t='download_youtube_vid "136+234" $SHORTNAME_OFF $TRANSCRIBE_ON'
alias yt-720-shortname-t='download_youtube_vid "136+234" $SHORTNAME_ON $TRANSCRIBE_ON'
#--------------------------- #---------------------------
alias yt-playlist='download_youtube_playlist ""' alias yt-playlist='download_youtube_playlist ""'
alias yt-playlist-audio='download_youtube_playlist "140"' alias yt-playlist-audio='download_youtube_playlist "234"'
alias yt-playlist-1440='download_youtube_playlist "620+140"' alias yt-playlist-4k='download_youtube_playlist "625+234"'
alias yt-playlist-1080='download_youtube_playlist "137+140"' alias yt-playlist-1440='download_youtube_playlist "620+234"'
alias yt-playlist-720='download_youtube_playlist "136+140"' alias yt-playlist-1080p='download_youtube_playlist "616+234"'
alias yt-playlist-tiny='download_youtube_playlist "160+140"' alias yt-playlist-1080='download_youtube_playlist "270+234"'
alias yt-playlist-720='download_youtube_playlist "136+234"'
alias yt-playlist-tiny='download_youtube_playlist "160+234"'
alias yt-playlist-list='download_youtube_playlist_list ' alias yt-playlist-list='download_youtube_playlist_list '
#--------------------------- #---------------------------
alias yt-audio='download_youtube_audio' alias yt-audio='download_youtube_audio'
@ -1173,16 +1219,16 @@ alias tw-4k-shortname-compressed-t='download_twitch_vi "2160p" $SHORTNAM
#------------------------------------------------- #-------------------------------------------------
# Vimeo Vid DL # Vimeo Vid DL
#------------------------------------------------- #-------------------------------------------------
alias vimeo='download_vimeo_vid $SHORTNAME_OFF $COMPRESSION_ON $TRANSCRIBE_OFF' alias vimeo='download_vimeo_vid "" $SHORTNAME_OFF $COMPRESSION_OFF $TRANSCRIBE_OFF'
alias vimeo-t='download_vimeo_vid $SHORTNAME_OFF $COMPRESSION_ON $TRANSCRIBE_ON' alias vimeo-t='download_vimeo_vid "" $SHORTNAME_OFF $COMPRESSION_OFF $TRANSCRIBE_ON'
alias vimeo-compressed='download_vimeo_vid $SHORTNAME_OFF $COMPRESSION_ON $TRANSCRIBE_OFF' alias vimeo-compressed='download_vimeo_vid "" $SHORTNAME_OFF $COMPRESSION_ON $TRANSCRIBE_OFF'
alias vimeo-compressed-t='download_vimeo_vid $SHORTNAME_OFF $COMPRESSION_ON $TRANSCRIBE_ON' alias vimeo-compressed-t='download_vimeo_vid "" $SHORTNAME_OFF $COMPRESSION_ON $TRANSCRIBE_ON'
#------------------------------------------------- #-------------------------------------------------
# Instagram Vid DL # Instagram Vid DL
#------------------------------------------------- #-------------------------------------------------
alias ig='download_instagram_vid $TRANSCRIBE_OFF' alias ig='echo using my accounts with cookies to dl ig vids makes instagram think i am a bot and they might close my account. do not use your accounts' #download_instagram_vid $TRANSCRIBE_OFF'
alias igt='download_instagram_vid $TRANSCRIBE_ON' alias igt='echo using my accounts with cookies to dl ig vids makes instagram think i am a bot and they might close my account. do not use your accounts' #download_instagram_vid $TRANSCRIBE_ON'
#------------------------------------------------- #-------------------------------------------------
# Twitter Vid DL # Twitter Vid DL
@ -1194,7 +1240,6 @@ alias twitter='download_twitter_vid "" '
#------------------------------------------------- #-------------------------------------------------
alias download-mp4='download_mp4' alias download-mp4='download_mp4'
alias download-from-stream='download_mp4_from_stream' alias download-from-stream='download_mp4_from_stream'
alias download-from-m3u8='echo Use download-from-stream instead.'
alias download-audio-from-stream='download_aac_from_stream' alias download-audio-from-stream='download_aac_from_stream'
alias download-audio-from-m3u8='echo Use download-audio-from-stream instead.' alias download-audio-from-m3u8='echo Use download-audio-from-stream instead.'
@ -1223,7 +1268,7 @@ alias av='analyze-volume'
alias aa='analyze-volume' alias aa='analyze-volume'
alias nv='normalize-volume' alias nv='normalize-volume'
alias na='normalize-volume' alias na='normalize-volume'
alias tv='trim-video' alias tv='trim-video-vbr'
#################################################################################################### ####################################################################################################
# Git # Git

View File

@ -23,10 +23,10 @@ else
NORMAL="" NORMAL=""
fi fi
use_gpu=0 use_gpu=1
if [[ $# < 2 || $# > 3 ]]; then if [[ $# < 2 || $# > 5 ]]; then
printf "${BOLD}${RED}Usage: compress-video <filename> <output name> <optional: use-gpu (1|0), defaults to $use_gpu> ${NORMAL}\n" printf "${BOLD}${RED}Usage: compress-video <filename> <output name> <optional: use-gpu (1|0), defaults to $use_gpu> <optional: start time HH:MM:SS> <optional: end time HH:MM:SS> ${NORMAL}\n"
exit 1 exit 1
fi fi
@ -40,8 +40,8 @@ fi
# Found the following to work best with vids containing text (e.g. programming vid): 21 for CPU encoding and 28 for GPU (similar bitrates). # Found the following to work best with vids containing text (e.g. programming vid): 21 for CPU encoding and 28 for GPU (similar bitrates).
use_crf=21 use_crf=21
if [[ $use_gpu -eq 1 ]]; then if [[ $use_gpu -eq 1 ]]; then
use_crf=28 use_crf=33
fi fi
compress-video-with-crf $use_crf "$filename" "$output_name" $use_gpu compress-video-with-crf $use_crf "$filename" "$output_name" $use_gpu $4 $5

View File

@ -23,7 +23,7 @@ else
NORMAL="" NORMAL=""
fi fi
use_gpu=0 use_gpu=1
if [[ "$#" < 3 || "$#" > 6 ]]; then if [[ "$#" < 3 || "$#" > 6 ]]; then
printf "${BOLD}${RED}Usage: compress-video-with-crf <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" printf "${BOLD}${RED}Usage: compress-video-with-crf <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"
@ -66,18 +66,24 @@ function encode() {
if [[ $use_gpu -eq 1 ]]; then if [[ $use_gpu -eq 1 ]]; then
# RTX 3080 # 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" # vbr_hq is smaller file and less bitrate than vbr - strange!
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 $crf -c:a copy -max_muxing_queue_size 9999 -movflags +faststart "$output"
# GTX 1070 # capped bitrate w/ crf is weird. gives a more compressed copy
#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" # 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 -cq:v $crf -b:v 1200K -maxrate:v 1200K -bufsize:v 2400K -c:a copy -max_muxing_queue_size 9999 -movflags +faststart "$output"
else 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" 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 fi
printf "\n${GREEN}${BOLD}Finished encoding '$filename.$extension' (CRF $crf) | output name '$output'${NORMAL}\n\n" printf "\n${GREEN}${BOLD}Finished encoding '$filename.$extension' (CRF $crf) | output name '$output'${NORMAL}\n\n"
original_size=$(wc -c <"$filename.$extension")
new_size=$(wc -c <"$output")
printf "${BOLD}Original size: $original_size${NORMAL}\n"
printf "${BOLD}New size: $new_size${NORMAL}\n\n"
} }
if [[ $use_crf -ne -1 ]]; then if [[ $use_crf -ne -1 ]]; then
encode $use_crf time encode $use_crf
else else
printf "\n${YELLOW}${BOLD}Encoding using a range of CRF values.${NORMAL}\n" printf "\n${YELLOW}${BOLD}Encoding using a range of CRF values.${NORMAL}\n"

View File

@ -53,5 +53,8 @@ printf "\n${YELLOW}${BOLD}Trimming '$filename' | output: $output_name_with_exten
ffmpeg -y -stats -loglevel level+error $timing_args -accurate_seek -i "$filename" -map a "$output_name_with_extension" ffmpeg -y -stats -loglevel level+error $timing_args -accurate_seek -i "$filename" -map a "$output_name_with_extension"
#If you want to have a specific bit rate and sample rate then you need to add it before the output filename. Might also need to remove the -map a arg. e.g.
#ffmpeg -y -stats -loglevel level+error $timing_args -accurate_seek -i "$filename" -c:a aac -b:a 191k -ar 48000 "$output_name_with_extension"
printf "\n${GREEN}${BOLD}Finished trimming${NORMAL}\n\n" printf "\n${GREEN}${BOLD}Finished trimming${NORMAL}\n\n"

View File

@ -0,0 +1,92 @@
#!/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"

124
dotfiles/bin/trim-video-vbr Normal file
View File

@ -0,0 +1,124 @@
#!/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"

View File

@ -59,18 +59,18 @@
127.0.0.1 www.wip2.adobe.com www.wip3.adobe.com www.wip4.adobe.com wwis-dubc1-vip60.adobe.com crl.verisign.net CRL.VERISIGN.NET ood.opsource.net 127.0.0.1 www.wip2.adobe.com www.wip3.adobe.com www.wip4.adobe.com wwis-dubc1-vip60.adobe.com crl.verisign.net CRL.VERISIGN.NET ood.opsource.net
# Block Facebook # Block Facebook
127.0.0.1 www.facebook.com #127.0.0.1 www.facebook.com
127.0.0.1 facebook.com #127.0.0.1 facebook.com
127.0.0.1 static.ak.fbcdn.net #127.0.0.1 static.ak.fbcdn.net
127.0.0.1 www.static.ak.fbcdn.net #127.0.0.1 www.static.ak.fbcdn.net
127.0.0.1 login.facebook.com #127.0.0.1 login.facebook.com
127.0.0.1 www.login.facebook.com #127.0.0.1 www.login.facebook.com
127.0.0.1 fbcdn.net #127.0.0.1 fbcdn.net
127.0.0.1 www.fbcdn.net #127.0.0.1 www.fbcdn.net
127.0.0.1 fbcdn.com #127.0.0.1 fbcdn.com
127.0.0.1 www.fbcdn.com #127.0.0.1 www.fbcdn.com
127.0.0.1 static.ak.connect.facebook.com #127.0.0.1 static.ak.connect.facebook.com
127.0.0.1 www.static.ak.connect.facebook.com #127.0.0.1 www.static.ak.connect.facebook.com
#---------------------------------------------- #----------------------------------------------

View File

@ -0,0 +1,4 @@
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\SOFTWARE\Classes\Local Settings\Software\Microsoft\Windows\Shell\Bags\AllFolders\Shell]
"FolderType"="NotSpecified"