Jump to content

People photo resizer for Linux


anderbytes
 Share

Recommended Posts

I'd like to share a script that I built yesterday for resizing of people photos, that potentially can take massive Mb/Gb of our HDs.

With this script, you can set a max width or height and all photos larger than that will be reduced proportionally, saving diskspace and making page load easier.

 

For now, it's interactive, so it can't be scheduled. I may fix that in the future.

It requires ffmpeg and ffprobe in the server.

#!/bin/bash

EMBYPATH="/media/hard_disk/emby"               # Write here the official Emby path
MIN=300                                                                                         # You can set a warning for operations smaller than usual

echo -n "Would you like to set a max [W]idth or [H]eight of Emby People photos? : "; read TYPE;
if [ "${TYPE,,}" == "w" ]; then
        TYPE="Width";
elif [ "${TYPE,,}" == "h" ]; then
        TYPE="Height";
else
        echo "Invalid type.";
        exit;
fi

echo -n "What will be the Max "$TYPE" size (in pixels) : "; read MAX;
if [ ! $MAX -ge 1 ]; then
        echo "Invalid max size.";
        exit;
elif [ ! $MAX -ge $MIN ]; then
        echo "Photos with less than $MINpx will produce bad results. You were warned. Tap to continue..."; read;
fi

NFiles=$(find "$EMBYPATH/metadata/People" -type f -iname 'poster.jpg' | wc -l)
OldSize=$(find "$EMBYPATH/metadata/People" -type f -iname 'poster.jpg' -printf "%s\n" | awk '{t+=$1}END{print t}')
ind=0; hits=0;

IFS=$'\n'
filelist=$(find "$EMBYPATH/metadata/People" -type f -iname 'poster.jpg')
for photo in $filelist; do
        ### PHOTO ANALYSIS ###
        scale=""
        sizes=$(ffprobe -v quiet -show_entries stream=width,height -print_format csv "$photo")
        width=$(cut -d, -f2 <<< "$sizes")
        height=$(cut -d, -f3 <<< "$sizes")

        ### CONVERSION COMMAND ###
        if [ "$TYPE" == "Width" ] && [ $width -gt $MAX ]; then
                scale="$MAX:-1";
        elif [ "$TYPE" == "Height" ] && [ $height -gt $MAX ]; then
                scale="-1:$MAX";
        fi

        if [ ! -z $scale ]; then
                ffmpeg -hide_banner -v quiet -y -i "$photo" -vf scale="$scale" "$photo";
                hits=$(( $hits + 1 ));
        fi

        ### PROGRES BAR ###
        ind=$(( $ind + 1 ));
        perc=$(awk "BEGIN {printf \"%.2f\",(${ind}/${NFiles})*100}");
        echo -ne "Processing $ind of $NFiles [$perc%]\r"

done;

echo -e "\n";

if [ $hits -gt 0 ]; then
        NewSize=$(find "$EMBYPATH/metadata/People" -type f -iname 'poster.jpg' -printf "%s\n" | awk '{t+=$1}END{print t}')
        echo "Photos Reduced: $hits";
        CalcSize=$(( $OldSize - $NewSize ));

        if [ $CalcSize -ge 1073741824 ]; then
            CalcSize=$(awk 'BEGIN {printf "%.3g",'$CalcSize'/1073741824}')Gb
        elif [ $CalcSize -ge 1048576 ]; then
            CalcSize=$(awk 'BEGIN {printf "%.3g",'$CalcSize'/1048576}')Mb
        elif [ $CalcSize -ge 1024 ]; then
            CalcSize=$(awk 'BEGIN {printf "%.3g",'$CalcSize'/1024}')Kb
        fi

        echo "Diskspace Freed: $CalcSize";
else
        echo "All Photos already respect max $TYPE of $MAX. No Photos reduced."
fi
Edited by anderbytes
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...