#!/bin/sh # # This is an example of a custom HandBrake args hook. This script is always # invoked with /bin/sh (shebang ignored). # # The first parameter is the full path to the source file. # # The secomd argument is the name of the HandBrake preset that will be used to # convert the video. # SOURCE_FILE="$1" PRESET="$2" # Print any debug to stderr since stdout is for custom arguments. echo "★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ HB CUSTOM ARGS >>>" >&2 echo "custom-handbrake-args: Source File = $SOURCE_FILE" >&2 echo "custom-handbrake-args: Preset = $PRESET" >&2 # Extract QUALITY from preset (last word after last dot) QUALITY=$(echo "$PRESET" | awk -F. '{print toupper($NF)}') #echo "Source file: $SOURCE_FILE" >&2 #echo "Preset: $PRESET" >&2 #echo "Quality extracted from preset: $QUALITY" >&2 # Output scan for debugging to a local variable scan_output=$(HandBrakeCLI -i "$SOURCE_FILE" --scan 2>&1) # Try to extract the first size: line from the local variable size_line=$(echo "$scan_output" | grep -m 1 -E 'size: [0-9]+x[0-9]+') width=$(echo "$size_line" | sed -n 's/.*size: \([0-9]\+\)x\([0-9]\+\).*/\1/p') height=$(echo "$size_line" | sed -n 's/.*size: \([0-9]\+\)x\([0-9]\+\).*/\2/p') # Extract duration from scan_output (supports hours, minutes, seconds) duration=$(echo "$scan_output" | grep -m 1 -E 'Duration: [0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]+' | sed -n 's/.*Duration: \([0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}\.[0-9]\+\).*/\1/p') # Extract bitrate (number before 'kb/s') bitrate=$(echo "$scan_output" | grep -m 1 -E 'bitrate: [0-9]+ kb/s' | sed -n 's/.*bitrate: \([0-9]\+\) kb\/s.*/\1/p') # Extract creation_time from the file's metadata (using stat) creation_time=$(stat -c %W "$SOURCE_FILE") if [ -z "$creation_time" ] || [ "$creation_time" -le 0 ] 2>/dev/null; then # Fallback to modification time if birth time is unavailable or empty creation_time=$(stat -c %Y "$SOURCE_FILE") fi creation_time=$(date -u -d @"$creation_time" +"%Y-%m-%dT%H:%M:%SZ") # Extract the Stream #0:0 line stream_line=$(echo "$scan_output" | grep -m 1 '^ Stream #0:0') # Extract fps (number before 'fps') fps=$(echo "$stream_line" | sed -n 's/.* \([0-9.]*\) fps.*/\1/p') # Extract Video codec (first word after 'Video:') video_codec=$(echo "$stream_line" | sed -n 's/.*Video: *\([^ ]*\).*/\1/p') if [ -z "$width" ] || [ -z "$height" ]; then echo "Could not detect resolution; defaulting to 720x576" >&2 width=720 height=576 else echo "Detected resolution: ${width}x${height}" >&2 fi QUALITY=${QUALITY:-LOW} set_rf() { local quality="$1" local width="$2" local height="$3" if [ "$width" -le 1024 ] && [ "$height" -le 576 ]; then # Standard Definition (SD): 480p/576p and below case "$quality" in LOW) echo '24.0' ;; MEDIUM) echo '22.0' ;; HIGH) echo '20.0' ;; ULTRA) echo '18.0' ;; esac elif [ "$width" -le 1280 ] && [ "$height" -le 720 ]; then # High Definition (HD): 720p case "$quality" in LOW) echo '25.0' ;; MEDIUM) echo '23.0' ;; HIGH) echo '21.0' ;; ULTRA) echo '19.0' ;; esac elif [ "$width" -le 1920 ] && [ "$height" -le 1080 ]; then # Full High Definition (Full HD): 1080p case "$quality" in LOW) echo '26.0' ;; MEDIUM) echo '24.0' ;; HIGH) echo '22.0' ;; ULTRA) echo '20.0' ;; esac else # Ultra High Definition (UHD): 2160p (4K) and above case "$quality" in LOW) echo '28.0' ;; MEDIUM) echo '26.0' ;; HIGH) echo '24.0' ;; ULTRA) echo '22.0' ;; esac fi } RF=$(set_rf "$QUALITY" "$width" "$height") echo -e "${width}x${height}\n${video_codec}\n${fps}\n${bitrate}\n${duration}\n${creation_time}\n${RF}" >> /config/stat echo "Set RF to $RF (quality: $QUALITY, resolution: ${width}x${height})" >&2 echo "★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ <<< HB CUSTOM ARGS" >&2 echo "--quality $RF"