Files
tool__scripts/manage-video/resolution-classifier/resolution-classifier.sh
2026-07-09 01:05:56 +02:00

163 lines
4.0 KiB
Bash
Executable File

#!/bin/bash
IGNORED_FOLDERS=("backdrops" "samples" "extras" "featurettes")
VIDEO_EXTENSIONS=(mp4 mkv mov avi wmv m4v ts m2ts webm)
EXISTING_FILES="existing_target.txt"
classify_resolution() {
local WIDTH="$1"
local HEIGHT="$2"
local MAX=$((WIDTH > HEIGHT ? WIDTH : HEIGHT))
if ((MAX >= 3800)); then
echo " [2160p]"
elif ((MAX >= 1900)); then
echo " [1080p]"
elif ((MAX >= 1200)); then
echo " [720p]"
elif ((MAX >= 700)); then
echo " [480p]"
elif ((MAX >= 500)); then
echo " [360p]"
else
echo " [240p]"
fi
}
strip_resolution_tag() {
echo "$1" | sed -E 's/ ?\[[0-9]{3,4}p\]//g'
}
move_item() {
local SRC="$1"
local DST="$2"
if [[ "$SRC" != "$DST" ]]; then
if [[ -e "$DST" ]]; then
echo "⚠️ Target already exists, skipping:"
echo " existing: $DST"
echo " incoming: $SRC"
echo "$SRC -> $DST" >>$EXISTING_FILES
continue
fi
if $DRY_RUN; then
echo "[DRY RUN] Would rename:"
echo " $SRC"
echo " $DST"
else
echo "Renaming:"
echo " $SRC"
echo " $DST"
mv "$SRC" "$DST"
fi
fi
}
DRY_RUN=false
if [[ "$1" == "--dry-run" || "$1" == "-n" ]]; then
DRY_RUN=true
echo "Running in DRY-RUN mode. No files will be renamed."
fi
if ! $DRY_RUN; then
read -p "This will automatically classify and rename the resolution for all nested files in the current dir. Continue? [Y/n] " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Batch renaming cancelled by user."
exit 1
fi
fi
echo
FIND_IGNORE_PARAMS=()
for FOLDER in "${IGNORED_FOLDERS[@]}"; do
FIND_IGNORE_PARAMS+=(-type d -name "$FOLDER" -o)
done
unset 'FIND_IGNORE_PARAMS[${#FIND_IGNORE_PARAMS[@]}-1]'
EXT_ARGS=()
for EXT in "${VIDEO_EXTENSIONS[@]}"; do
EXT_ARGS+=(-iname "*.${EXT}" -o)
done
unset 'EXT_ARGS[${#EXT_ARGS[@]}-1]'
find . \( "${FIND_IGNORE_PARAMS[@]}" \) -prune -o \
-type f \
-size +1M \
! -iname "*_delete" \
\( "${EXT_ARGS[@]}" \) \
-print0 | while IFS= read -r -d '' VIDEO_FILE; do
echo "-----------------------------"
echo "Processing file: $VIDEO_FILE"
RESOLUTION=$(ffprobe -v error \
-select_streams v:0 \
-show_entries stream=width,height \
-of csv=p=0:s=x \
"$VIDEO_FILE" | head -n1)
WIDTH=$(echo $RESOLUTION | cut -d'x' -f1)
HEIGHT=$(echo $RESOLUTION | cut -d'x' -f2)
# echo "Resolution: $RESOLUTION (${WIDTH}x${HEIGHT})"
if ((WIDTH < 100 || HEIGHT < 100)); then
echo "Invalid resolution detected (${WIDTH}x${HEIGHT}), skipping"
continue
fi
CLASSIFICATION=$(classify_resolution "$WIDTH" "$HEIGHT")
# echo "Resolution classification: $CLASSIFICATION"
BASENAME=$(basename -- "$VIDEO_FILE")
DIRNAME=$(dirname -- "$VIDEO_FILE")
BASENAME_NO_EXT="${BASENAME%.*}"
BASE_CLEAN=$(strip_resolution_tag "$BASENAME_NO_EXT")
EXTENSION="${BASENAME##*.}"
# detect cd suffix like -cd1, -cd2 etc (because Jellyfin absolutely requires those to be the last thing before the extension)
CD_SUFFIX=""
if [[ "$BASE_CLEAN" =~ (.*)(-(cd|disc|disk|part)[0-9]+)$ ]]; then
BASE_CLEAN="${BASH_REMATCH[1]}"
CD_SUFFIX="${BASH_REMATCH[2]}"
fi
NEW_BASE="${BASE_CLEAN}${CLASSIFICATION}${CD_SUFFIX}"
NEW_FILENAME="${BASE_CLEAN}${CLASSIFICATION}${CD_SUFFIX}.${EXTENSION}"
TARGET="$DIRNAME/$NEW_FILENAME"
OLD_BASE="$BASENAME_NO_EXT"
OLD_TRICKPLAY="$DIRNAME/${OLD_BASE}.trickplay"
NEW_TRICKPLAY="$DIRNAME/${NEW_BASE}.trickplay"
move_item "$VIDEO_FILE" "$TARGET"
if [[ -d "$OLD_TRICKPLAY" ]]; then
echo
echo "[Trickplay] Matching trickplay folder detected"
move_item "$OLD_TRICKPLAY" "$NEW_TRICKPLAY"
else
VIDEO_COUNT=$(find "$DIRNAME" -maxdepth 1 -type f \( "${EXT_ARGS[@]}" \) | wc -l)
TRICKPLAY_COUNT=$(find "$DIRNAME" -maxdepth 1 -type d -name "*.trickplay" | wc -l)
if [[ "$VIDEO_COUNT" -eq 1 && "$TRICKPLAY_COUNT" -eq 1 ]]; then
FOUND_TRICKPLAY=$(find "$DIRNAME" -maxdepth 1 -type d -name "*.trickplay")
echo
echo "[Trickplay] Detected lone trickplay folder:"
echo " $FOUND_TRICKPLAY"
echo "[Trickplay] Assuming it belongs to this video"
move_item "$FOUND_TRICKPLAY" "$NEW_TRICKPLAY"
fi
fi
done