From f2019d0aac71739d3ea6e780a501cd0be9af6b4a Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Tue, 24 Feb 2026 23:14:39 +0100 Subject: refactor: simplify several small code patterns - Drop the UMASK variable; umask is called directly with the literal value - Simplify filesize() to a one-liner using || instead of if/local/printf - Fold token lowercasing into the local declaration in add_skip_codec_token and drop the now-redundant empty-string guard - Replace manual whitespace trimming in the skip.conf reader with read's built-in IFS trimming - Replace the yuvj* case statement with a single parameter substitution - Collapse nested only-if-smaller conditions into one && expression - Move the "Sourcing preset file" debug call to just before source --- transcode.sh | 45 +++++++++++---------------------------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/transcode.sh b/transcode.sh index 64840df..a74b6c3 100755 --- a/transcode.sh +++ b/transcode.sh @@ -73,8 +73,7 @@ hash -r IFS=$'\n\t' # Set a secure default file creation mask (controls default permissions) -UMASK=002 -umask "$UMASK" +umask 002 # Ensure ERR traps are inherited by functions and subshells set -o errtrace @@ -230,13 +229,7 @@ ${RED}${BOLD}License:${ALL_OFF} %s\n" "$SCRIPTNAME" "$VERSION" "$DESCRIPTION" "$ } filesize() { - local f="$1" - local out - if out=$(stat -c "%s" -- "$f" 2>/dev/null); then - printf "%s\n" "$out" - else - stat -f "%z" -- "$f" - fi + stat -c "%s" -- "$1" 2>/dev/null || stat -f "%z" -- "$1" } ############################################################################### @@ -316,16 +309,13 @@ setup_colors # - Ignores empty tokens # - Used both for CLI --skip-codec and for skip.conf lines add_skip_codec_token() { - local token="$1" - [[ -z "$token" ]] && return 0 - token=${token,,} # lowercase + local token="${1,,}" # lowercase if [[ ! -v SKIP_CODECS["$token"] ]]; then debug "Add to skip codec:" "$token" SKIP_CODECS["$token"]= else debug "Codec already skipped:" "$token" fi - } # Parse a user-provided codec list (comma/space/colon separated) @@ -340,11 +330,7 @@ parse_skip_codec_parameter() { } if [[ -f "$SKIP_CODECS_FILE" ]]; then - while IFS= read -r line || [[ -n "$line" ]]; do - # strip leading/trailing whitespace - line="${line#"${line%%[![:space:]]*}"}" - line="${line%"${line##*[![:space:]]}"}" - # skip blanks and comments + while read -r line || [[ -n "$line" ]]; do [[ -z "$line" || "${line:0:1}" == "#" ]] && continue parse_skip_codec_parameter "$line" done <"$SKIP_CODECS_FILE" @@ -379,8 +365,6 @@ load_preset() { exit $EXIT_CONFIG_ERROR } - debug "Sourcing preset file:" "$file" - # Reject preset files that are world-writable to prevent arbitrary users # from injecting shell code. # @@ -399,6 +383,8 @@ load_preset() { unset -v ffargs ffargs=() + debug "Sourcing preset file:" "$file" + # shellcheck source=/dev/null source "$file" @@ -623,14 +609,7 @@ encode_one() { # Some files report "yuvj*" formats (full-range JPEG-style). For encoding # purposes we normalize to the equivalent "yuv*" formats. - # Covers all five variants ffmpeg can produce: 411p, 420p, 422p, 440p, 444p. - case "$input_pixel_format" in - yuvj411p) input_pixel_format="yuv411p" ;; - yuvj420p) input_pixel_format="yuv420p" ;; - yuvj422p) input_pixel_format="yuv422p" ;; - yuvj440p) input_pixel_format="yuv440p" ;; - yuvj444p) input_pixel_format="yuv444p" ;; - esac + input_pixel_format="${input_pixel_format/yuvj/yuv}" debug "Normalized input pixel format:" "$input_pixel_format" # Preserve chroma subsampling (420/422/444) and bit depth (8/10/12/16-bit) @@ -732,12 +711,10 @@ encode_one() { new_filesize=$(filesize "$tmp") # If requested, keep the original if the new file is larger. - if [[ $ONLY_IF_SMALLER -eq 1 ]]; then - if [[ $new_filesize -gt $original_filesize ]]; then - msg "Did not replace $file as it was larger than the original." - rm -f -- "$tmp" - return $EXIT_OK - fi + if [[ $ONLY_IF_SMALLER -eq 1 && $new_filesize -gt $original_filesize ]]; then + msg "Did not replace $file as it was larger than the original." + rm -f -- "$tmp" + return $EXIT_OK fi if [[ $SAVING -eq 1 ]]; then -- cgit v1.3.1