#!/bin/bash -e THREADS="$(nproc --all)" THREADS="$((THREADS / 2))" function wait_queue() { for job in $(jobs -p | awk "FNR>=$THREADS"); do wait "$job" done } function compress() {( local nb total dir newdir file cd "$DIR" dir="$PWD" total="$(printf '%s\n' * | wc -l)" newdir="${PWD%/*}/$(basename "$dir")_1080p" mkdir -p "$newdir" for file in *; do "${IDENTIFY[@]}" "$file" 2> /dev/null >&2 \ && "${CONVERT[@]}" -resize x1080 "$file" "$newdir/$file" & wait_queue nb="$(printf '%s\n' "$newdir"/* | wc -l)" echo "$((nb * 100 / total))" done )} IDENTIFY=(identify) CONVERT=(convert) if command -v gm > /dev/null; then IDENTIFY=(gm "${IDENTIFY[@]}") CONVERT=(gm "${CONVERT[@]}") fi if [ -z "$1" ]; then set "$(zenity --file-selection --multiple --directory --separator=' ')" > /dev/null fi if [ -z "$1" ]; then exit 1 fi for DIR in "$@"; do compress | zenity --progress --title="$DIR" done