35 lines
873 B
Bash
35 lines
873 B
Bash
#!/bin/bash
|
|
|
|
# Prompt for input and output directories
|
|
read -p "Enter the input directory containing images: " INPUT_DIR
|
|
read -p "Enter the output directory for compressed images: " OUTPUT_DIR
|
|
|
|
# Check if input directory exists
|
|
if [ ! -d "$INPUT_DIR" ]; then
|
|
echo "Input directory does not exist. Please provide a valid directory."
|
|
exit 1
|
|
fi
|
|
|
|
# Create output directory if it doesn't exist
|
|
mkdir -p "$OUTPUT_DIR"
|
|
|
|
# Loop through all images in the input directory
|
|
for img in "$INPUT_DIR"/*.{jpg,jpeg,png}; do
|
|
[ -e "$img" ] || continue
|
|
|
|
# Get filename without extension
|
|
filename=$(basename "$img")
|
|
output="${OUTPUT_DIR}/${filename%.*}.jpg"
|
|
|
|
# Compress image while maintaining quality
|
|
ffmpeg -i "$img" \
|
|
-vf "scale=iw*0.9:ih*0.9" \
|
|
-q:v 2 \
|
|
"$output"
|
|
|
|
echo "Compressed $img -> $output"
|
|
done
|
|
|
|
echo "All images compressed and saved in $OUTPUT_DIR"
|
|
|