diff options
| author | Dennis Fink | 2026-02-25 09:17:24 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-02-25 09:17:24 +0100 |
| commit | 6061504cdc9216c085ef334726bbf7ed3af4ea3f (patch) | |
| tree | 2b1ffb201e7bccea0796216408fd04bd60ec711b | |
| parent | b10b81fefbe529be7db14ca65ec4b4a293782d39 (diff) | |
| download | transcode.sh-6061504cdc9216c085ef334726bbf7ed3af4ea3f.tar.gz transcode.sh-6061504cdc9216c085ef334726bbf7ed3af4ea3f.zip | |
refactor(core): combine ffprobe calls and simplify pixel format selection
Merge the two separate ffprobe invocations for codec and pixel format
into one, halving the per-file probe overhead on large batches. Both
error messages now include the filename for easier diagnosis in batch
runs.
Replace the three-way nested if/elif/else for pixel format selection
with two independent case statements — one for chroma subsampling, one
for bit depth — that compose the output format string from two parts.
This eliminates the repetition across the three subsampling branches
and makes adding a new bit depth or subsampling variant a single-line
change. Also add a verbose fallback warning for exotic formats that
don't match any known subsampling pattern.
| -rwxr-xr-x | transcode.sh | 80 |
1 files changed, 37 insertions, 43 deletions
diff --git a/transcode.sh b/transcode.sh index 6b0bb22..436c89f 100755 --- a/transcode.sh +++ b/transcode.sh @@ -632,25 +632,31 @@ encode_one() { return $EXIT_RUNTIME_FAILURE } - local input_codec - input_codec=$(ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=nk=1:nw=1 "file:$file" 2>/dev/null) + # Probe codec and pixel format in a single ffprobe invocation to halve + # the startup overhead on large batches. + local probe_out + probe_out=$(ffprobe -v error -select_streams v:0 \ + -show_entries stream=codec_name,pix_fmt \ + -of default=nk=1:nw=1 "file:$file" 2>/dev/null) + + local input_codec input_pixel_format + input_codec=$(printf '%s\n' "$probe_out" | sed -n '1p') + input_pixel_format=$(printf '%s\n' "$probe_out" | sed -n '2p') + if [[ -z "$input_codec" ]]; then - error "Could not determine input video codec" + error "Could not determine input video codec:" "$file" + return $EXIT_RUNTIME_FAILURE + fi + if [[ -z "$input_pixel_format" ]]; then + error "Could not determine input pixel format:" "$file" return $EXIT_RUNTIME_FAILURE fi if [[ -v SKIP_CODECS["$input_codec"] ]]; then - msg "Skip because of input codec:" "$input_codec" + msg "Skipping (codec excluded):" "$input_codec" return $EXIT_OK fi - local input_pixel_format - input_pixel_format=$(ffprobe -v error -select_streams v:0 -show_entries stream=pix_fmt -of default=nk=1:nw=1 "file:$file" 2>/dev/null) - if [[ -z "$input_pixel_format" ]]; then - error "Could not determine input pixel format" - return $EXIT_RUNTIME_FAILURE - fi - # Some files report "yuvj*" formats (full-range JPEG-style). For encoding # purposes we normalize to the equivalent "yuv*" formats. input_pixel_format="${input_pixel_format/yuvj/yuv}" @@ -660,38 +666,26 @@ encode_one() { # to avoid unintended quality loss or incompatible output. # Known limitation: exotic formats not matching these patterns (e.g. gbrp, # yuva*, gray*) fall through to yuv420p. Extend the logic below if needed. - local output_pixel_format - if [[ "$input_pixel_format" == *"444"* ]]; then - if [[ "$input_pixel_format" == *"16"* ]]; then - output_pixel_format="yuv444p16le" - elif [[ "$input_pixel_format" == *"12"* ]]; then - output_pixel_format="yuv444p12le" - elif [[ "$input_pixel_format" == *"10"* ]]; then - output_pixel_format="yuv444p10le" - else - output_pixel_format="yuv444p" - fi - elif [[ "$input_pixel_format" == *"422"* ]]; then - if [[ "$input_pixel_format" == *"16"* ]]; then - output_pixel_format="yuv422p16le" - elif [[ "$input_pixel_format" == *"12"* ]]; then - output_pixel_format="yuv422p12le" - elif [[ "$input_pixel_format" == *"10"* ]]; then - output_pixel_format="yuv422p10le" - else - output_pixel_format="yuv422p" - fi - else - if [[ "$input_pixel_format" == *"16"* ]]; then - output_pixel_format="yuv420p16le" - elif [[ "$input_pixel_format" == *"12"* ]]; then - output_pixel_format="yuv420p12le" - elif [[ "$input_pixel_format" == *"10"* ]]; then - output_pixel_format="yuv420p10le" - else - output_pixel_format="yuv420p" - fi - fi + local subsampling depth output_pixel_format + + case "$input_pixel_format" in + *444*) subsampling="444" ;; + *422*) subsampling="422" ;; + *) + subsampling="420" + [[ "$input_pixel_format" != *420* ]] && + verbose "Unrecognised chroma subsampling in '$input_pixel_format', falling back to yuv420p" + ;; + esac + + case "$input_pixel_format" in + *16*) depth="p16le" ;; + *12*) depth="p12le" ;; + *10*) depth="p10le" ;; + *) depth="p" ;; + esac + + output_pixel_format="yuv${subsampling}${depth}" debug "Selected output pixel format:" "$output_pixel_format" # Load preset early so a missing/invalid preset fails before we create a |
