Last active
August 21, 2022 04:05
-
-
Save strizhechenko/b12816e029eae6011f6e6e31577f7727 to your computer and use it in GitHub Desktop.
Script to convert directory with photos from iPhone (.HEIC) to .jpg format. Requires libheif-examples package installed. Notice: .jpg output is ~20% larger than .HEIC original.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euE | |
echo "$0 $* [$$] START" >&2 | |
declare DIR="${1:-.}" | |
declare REMOVE="${2:-no}" | |
at_least_one_output_exists() { | |
set -e | |
local filename="$1" | |
local noext="${filename%.HEIC}" | |
local output | |
for output in "$noext.JPG" "$noext.PNG" "$noext.jpg" "$noext.png"; do | |
# not -f, empty output may lead to a lose of photo | |
if [ -s "$output" ]; then | |
echo "SKIP: $filename & $output are both already exist" >&2 | |
echo "yes" | |
return 0 | |
fi | |
done | |
echo "no" | |
return 0 | |
} | |
heic2jpg() { | |
local filename="$1" | |
local output="${filename%.HEIC}.jpg" | |
# heif-convert comes from libheif-examples package | |
echo START heif-convert "$filename" "$output" >&2 | |
if ! heif-convert "$filename" "$output"; then | |
echo "FAILED $filename -> $output keep original in place" | |
elif [ "$REMOVE" = '--remove' ]; then | |
rm -vf "$filename" | |
fi | |
echo DONE heif-convert "$filename" "$output" >&2 | |
return 0 | |
} | |
main() { | |
local exists | |
find "$DIR" -type f -name "*.HEIC" | sort | while read -r filename; do | |
exists="$(at_least_one_output_exists "$filename")" | |
[ "$exists" != 'yes' ] && heic2jpg "$filename" | |
done | |
return 0 | |
} | |
main "$@" | |
echo "$0 $* [$$] SUCCESS" >&2 | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment