From e69b5d2d338347d9dc72fac9471882b971378849 Mon Sep 17 00:00:00 2001 From: Hyperling Date: Sun, 19 Jan 2025 16:37:02 -0700 Subject: [PATCH] Determine default video bitrates based on dimensions. Add file options to extension. Fix shrinking to certain sizes by using -2 as width. Tested successfully! --- files/scripts/compress_video.sh | 37 ++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/files/scripts/compress_video.sh b/files/scripts/compress_video.sh index 9939667..f172177 100755 --- a/files/scripts/compress_video.sh +++ b/files/scripts/compress_video.sh @@ -32,7 +32,14 @@ function usage { -i input : The input file or folder with which to search for video files. If nothing is provided, current directory (.) is assumed. -v bitrate : The video bitrate to convert to. - Defaults to '2000k'. + Defaults are based on the size passed. + >= 2160: '30M' + >= 1440: '12M' + >= 1080: '5M' + >= 720: '2000k' + >= 480: '750k' + >= 360: '250k' + < 360: '100k' -a bitrate : The audio bitrate to convert to. Defaults to '192k'. -c vcodec : The video codec you'd like to use, such as libopenh264. @@ -96,13 +103,30 @@ if [[ -z "$input" ]]; then fi if [[ -z "$video_bitrate" ]]; then - video_bitrate="2000k" + # Based roughly on the 2.5 step iteration used for 720 and 1080. + if (( $size >= 2160 )); then + video_bitrate="30M" + elif (( $size >= 1440 )); then + video_bitrate="12M" + elif (( $size >= 1080 )); then + video_bitrate="5M" + elif (( $size >= 720 )); then + video_bitrate="2000k" + elif (( $size >= 480 )); then + video_bitrate="750k" + elif (( $size >= 360 )); then + video_bitrate="250k" + else + video_bitrate="100k" + fi fi +compress_tags="$video_bitrate" video_bitrate="-b:v $video_bitrate -minrate 0 -maxrate $video_bitrate -bufsize $video_bitrate" if [[ -z "$audio_bitrate" ]]; then audio_bitrate="192k" fi +compress_tags="$compress_tags-$audio_bitrate" audio_bitrate="-b:a $audio_bitrate" if [[ -z "$codec" ]]; then @@ -124,7 +148,11 @@ fi if [[ -z $size ]]; then size="720" fi -size="-filter:v scale=-1:$size" +while (( ${#size} < 4 )); do + size="0$size" +done +compress_tags="$size-$compress_tags" +size="-filter:v scale=-2:$size" ## Main ## @@ -172,6 +200,9 @@ $search_command "$input" | sort | while read file; do # Build the new filename to signify it is different than the original. newfile="${file//$extension/$filename_flag-$date_YYYYMMDD.$extension_lower}" + # Add the size, video, and audio quality it was converted to. + newfile="${newfile//\.$extension_lower/_$compress_tags.$extension_lower}" + if [[ $newfile == $file ]]; then echo "ERROR: The new calculated filename matches the old, skipping." >&2 continue