3
0
mirror of https://github.com/NekoMonci12/mine-compress.git synced 2025-12-20 15:39:35 +00:00

Update Scripts

This commit is contained in:
Muhammad Tamir
2025-07-08 18:06:56 +07:00
parent ac62b4f74f
commit 3c62cf44d3
2 changed files with 86 additions and 47 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.zip

130
optimize_script.sh Normal file → Executable file
View File

@@ -1,68 +1,106 @@
#!/bin/bash #!/bin/bash
set -e # Manual error mode
set -o nounset -o pipefail
# =========[ Auto Dependency Installer ]========= # =========[ Auto Dependency Installer ]=========
install_dependencies() { install_dependencies() {
echo "Checking and installing dependencies: optipng, ffmpeg" echo "🔧 Installing dependencies: optipng, ffmpeg, zip"
# Detect package manager if command -v apt >/dev/null 2>&1; then
if command -v apt >/dev/null; then sudo apt update -qq
PKG_INSTALL="sudo apt install -y" sudo apt install -y -qq optipng ffmpeg zip
PKG_UPDATE="sudo apt update" elif command -v dnf >/dev/null 2>&1; then
elif command -v dnf >/dev/null; then sudo dnf install -y optipng ffmpeg zip
PKG_INSTALL="sudo dnf install -y" elif command -v pacman >/dev/null 2>&1; then
PKG_UPDATE="sudo dnf check-update" sudo pacman -Sy --noconfirm optipng ffmpeg zip
elif command -v pacman >/dev/null; then
PKG_INSTALL="sudo pacman -S --noconfirm"
PKG_UPDATE="sudo pacman -Sy"
else else
echo "Unsupported package manager. Please install 'optipng' and 'ffmpeg' manually." echo "Unsupported package manager. Please install optipng, ffmpeg, and zip manually."
exit 1 exit 1
fi fi
# Update package list and install
$PKG_UPDATE
$PKG_INSTALL optipng ffmpeg
} }
# Check if tools are already installed # =========[ Dependency Check ]=========
if ! command -v optipng >/dev/null || ! command -v ffmpeg >/dev/null; then for cmd in optipng ffmpeg zip; do
if ! command -v "$cmd" >/dev/null 2>&1; then
install_dependencies install_dependencies
else break
echo "Dependencies already installed." fi
fi done
echo "✅ Dependencies are installed."
# =========[ User Input ]========= # =========[ Ask for Directory ]=========
read -rp "Enter the full path to your resource pack folder: " target_folder read -rp "📁 Enter the full path to your resource pack folder: " SRC_DIR
if [[ ! -d "$SRC_DIR" ]]; then
if [[ ! -d "$target_folder" ]]; then echo "❌ Directory does not exist: $SRC_DIR"
echo "Folder does not exist: $target_folder"
exit 1 exit 1
fi fi
# =========[ Prepare Workspace ]=========
WORK_DIR="optimize_work"
UNPACK_DIR="$WORK_DIR/unpacked"
OUTPUT_ZIP="optimized-pack.zip"
rm -rf "$WORK_DIR" "$OUTPUT_ZIP"
mkdir -p "$UNPACK_DIR"
cp -r "$SRC_DIR"/. "$UNPACK_DIR"/
# =========[ PNG Optimization ]========= # =========[ PNG Optimization ]=========
echo "========================" echo "=============================="
echo "Optimizing PNG files..." echo "🖼️ Optimizing PNG files..."
echo "========================" echo "=============================="
find "$target_folder" -type f -iname '*.png' | while read -r file; do mapfile -t PNGS < <(find "$UNPACK_DIR" -type f -iname '*.png')
echo "Processing: $file" total_png=${#PNGS[@]}
optipng -o7 "$file" echo "📦 Found $total_png PNG file(s)."
done
# =========[ OGG Compression ]========= i=0
echo "========================" for file in "${PNGS[@]}"; do
echo "Compressing OGG files..." ((i++))
echo "========================" echo "🛠️ [$i/$total_png] $file"
if ! optipng -o7 -fix "$file" > /dev/null 2>&1; then
find "$target_folder" -type f -iname '*.ogg' | while read -r file; do echo "⚠️ Failed to optimize: $file"
echo "Processing: $file"
ffmpeg -y -i "$file" -ac 1 -ab 64k "${file}.tmp.ogg"
if [[ -f "${file}.tmp.ogg" ]]; then
mv -f "${file}.tmp.ogg" "$file"
fi fi
done done
echo "✅ PNG optimization complete."
echo "========================" # =========[ OGG Compression ]=========
echo "Optimization complete." echo "=============================="
echo "🔊 Compressing OGG files..."
echo "=============================="
mapfile -t OGGS < <(find "$UNPACK_DIR" -type f -iname '*.ogg')
total_ogg=${#OGGS[@]}
echo "📦 Found $total_ogg OGG file(s)."
if [[ "$total_ogg" -eq 0 ]]; then
echo "⚠️ No OGG files found!"
else
i=0
for file in "${OGGS[@]}"; do
((i++))
echo "🔄 [$i/$total_ogg] $file"
ffmpeg -v error -y -i "$file" -c:a libvorbis -qscale:a 3 "${file}.tmp.ogg" > /dev/null 2>&1
if [[ -f "${file}.tmp.ogg" ]]; then
mv -f "${file}.tmp.ogg" "$file"
else
echo "⚠️ Failed to compress: $file"
fi
done
echo "✅ OGG compression complete."
fi
# =========[ Create ZIP ]=========
echo "=============================="
echo "📦 Creating $OUTPUT_ZIP"
echo "=============================="
(cd "$UNPACK_DIR" && zip -rq "../../$OUTPUT_ZIP" .) || {
echo "❌ Failed to zip optimized pack."
exit 1
}
# =========[ Cleanup ]=========
rm -rf "$WORK_DIR"
echo "✅ All done! Output: $(realpath "$OUTPUT_ZIP")"