Skip to content

Instantly share code, notes, and snippets.

@stevekrenzel
Created December 24, 2024 11:05
Show Gist options
  • Save stevekrenzel/c8d6096fdb34f6e29b9efea6ec40bff6 to your computer and use it in GitHub Desktop.
Save stevekrenzel/c8d6096fdb34f6e29b9efea6ec40bff6 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Check if a directory path was provided
if [ $# -eq 0 ]; then
echo "Please provide a directory path"
echo "Usage: $0 /path/to/directory"
exit 1
fi
# Store the directory path
DIR="$1"
# Check if the directory exists
if [ ! -d "$DIR" ]; then
echo "Error: Directory '$DIR' does not exist"
exit 1
fi
# Count total HEIC files
total_files=$(find "$DIR" -type f -iname "*.HEIC" | wc -l)
echo "Found $total_files HEIC files to convert"
# Initialize counter
count=0
# Find all HEIC files in the directory and its subdirectories
find "$DIR" -type f -iname "*.HEIC" | while read -r file; do
# Increment counter
((count++))
# Get the directory path and filename
dir_path=$(dirname "$file")
filename=$(basename $(basename "$file" .HEIC) .heic)
# Create JPG path
jpg_path="$dir_path/$filename.jpg"
echo "[$count/$total_files] Converting: $file"
# Convert HEIC to JPG using sips
sips -s format jpeg "$file" --out "$jpg_path" >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "✓ Successfully converted to: $jpg_path"
rm "$file"
else
echo "✗ Failed to convert: $file"
fi
done
echo "Conversion complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment