Added better help text, cleaned up ctrl+c, and fixed more fis
This commit is contained in:
parent
edad99ba39
commit
775455af11
38
bash/jpg
38
bash/jpg
@ -5,23 +5,33 @@
|
|||||||
# Command:
|
# Command:
|
||||||
# https://zwbetz.com/convert-heic-images-to-jpg/
|
# https://zwbetz.com/convert-heic-images-to-jpg/
|
||||||
|
|
||||||
|
red="$(tput setaf 1)"
|
||||||
|
rst="$(tput sgr0)"
|
||||||
|
|
||||||
# trap ctrl-c and call ctrl_c()
|
# trap ctrl-c and call ctrl_c()
|
||||||
# https://rimuhosting.com/knowledgebase/linux/misc/trapping-ctrl-c-in-bash
|
# https://rimuhosting.com/knowledgebase/linux/misc/trapping-ctrl-c-in-bash
|
||||||
trap ctrl_c INT
|
trap ctrl_c INT
|
||||||
function ctrl_c() {
|
function ctrl_c() {
|
||||||
echo "${rst}${red}CTRL+C Detected, exiting.${rst}"
|
if [ $filecount -eq 0 ]; then printf "%s\n" "${rst}${red}exiting${rst}"
|
||||||
|
else printf "\n%s\n%s\n\n" "${rst}${red}CTRL+C detected - this may have corrupted the most recent attempted file" "conversion. Please inspect the latest file and delete or retry as needed.${rst}"
|
||||||
|
fi
|
||||||
exit
|
exit
|
||||||
}
|
}
|
||||||
|
|
||||||
padding=" "
|
padding=" "
|
||||||
|
|
||||||
if [[ "${@}" == *"-h"* ]]; then
|
if [[ "${@}" == *"-h"* ]]; then
|
||||||
|
printf "%s\n" "Command:"
|
||||||
|
printf "$padding%s\n" "Converts heic files (Apple's native picture format) to either jpg (default)"
|
||||||
|
printf "$padding%s\n" "or png (larger file size) for easier manipulation or sharing. Requires that"
|
||||||
|
printf "$padding%s\n" "Imagemagick's mogrify tool be complied and installed with heic support. See"
|
||||||
|
printf "$padding%s\n" "step 1 at https://dismy.link/heicmogrify for a brief install guide."
|
||||||
printf "%s\n" "Command usage:"
|
printf "%s\n" "Command usage:"
|
||||||
printf "$padding%-20s%s\n" "--dir [directory]" "Specify a directory other than current"
|
printf "$padding%-20s%s\n" "--dir [directory]" "Specify a directory other than current"
|
||||||
printf "$padding%-20s%s\n" "-d --delete" "Delete original files (default keeps originals)"
|
printf "$padding%-20s%s\n" "-d --delete" "Delete original files (default keeps originals)"
|
||||||
printf "$padding%-20s%s\n" "-q --quiet" "Suppresses progress output (summaries are still displayed)"
|
printf "$padding%-20s%s\n" "-q --quiet" "Suppresses progress output (summaries are still displayed)"
|
||||||
printf "$padding%-20s%s\n" "[--png|--jpg]" "Save as jpg/png (default is jpg)"
|
printf "$padding%-20s%s\n" "[--png|--jpg]" "Save as jpg/png (default is jpg)"
|
||||||
printf "$padding%-20s%s\n" "-h" "Print this help message and exit"
|
printf "$padding%-20s%s\n" "-h --help" "Print this help message and exit"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -35,6 +45,7 @@ saveasjpg=1
|
|||||||
# Suppress progress output
|
# Suppress progress output
|
||||||
keepquiet=0
|
keepquiet=0
|
||||||
|
|
||||||
|
filecount=0
|
||||||
customdir=0
|
customdir=0
|
||||||
for _arg in ${@}; do
|
for _arg in ${@}; do
|
||||||
if [ $customdir -eq 1 ]; then directory="$_arg"; customdir=2; continue; fi
|
if [ $customdir -eq 1 ]; then directory="$_arg"; customdir=2; continue; fi
|
||||||
@ -50,16 +61,22 @@ done
|
|||||||
# Make sure the mogrify command is available. If this is being
|
# Make sure the mogrify command is available. If this is being
|
||||||
# run as a cron job the which command will fail so we check the
|
# run as a cron job the which command will fail so we check the
|
||||||
# default location just in case.
|
# default location just in case.
|
||||||
|
mymogrify=`which mogrify`
|
||||||
which mogrify > /dev/null
|
which mogrify > /dev/null
|
||||||
if [[ $? -eq 1 ]]; then
|
if [[ $? -eq 1 ]]; then
|
||||||
if [[ ! -e "/usr/local/bin/mogrify" ]]; then
|
if [[ -e "/usr/local/bin/mogrify" ]]; then
|
||||||
echo "Unable to run the mogrify command. Please follow the build and installation instructions at the top of the script to install the program on your system."
|
mymogrify="/usr/local/bin/mogrify"
|
||||||
exit
|
else
|
||||||
|
printf "%s\n" "Unable to locate the mogrify command. Please follow the build and"
|
||||||
|
printf "%s\n" "installation guide at https://dismy.link/heicmogrify to install"
|
||||||
|
printf "%s\n" "the ImageMagick mogfify tool on your system with heic support."
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# summary:
|
# summary:
|
||||||
printf "\n%s\n" "Summary:"
|
printf "\n%s\n" "Summary:"
|
||||||
|
printf "$padding%s\n" "Binary: $mymogrify"
|
||||||
printf "$padding%s\n" "Directory: $directory"
|
printf "$padding%s\n" "Directory: $directory"
|
||||||
[ $keeporiginals -eq 0 ] && printf "$padding%s\n" "Originals: Delete" || printf "$padding%s\n" "Originals: Keep"
|
[ $keeporiginals -eq 0 ] && printf "$padding%s\n" "Originals: Delete" || printf "$padding%s\n" "Originals: Keep"
|
||||||
[ $keepquiet -eq 0 ] && printf "$padding%s\n" "Output: Visible" || printf "$padding%s\n" "Output: Hidden"
|
[ $keepquiet -eq 0 ] && printf "$padding%s\n" "Output: Visible" || printf "$padding%s\n" "Output: Hidden"
|
||||||
@ -67,7 +84,6 @@ printf "$padding%s\n" "Directory: $directory"
|
|||||||
printf "\n%s" "Press enter to begin or ^c to exit"
|
printf "\n%s" "Press enter to begin or ^c to exit"
|
||||||
read
|
read
|
||||||
|
|
||||||
filecount=0
|
|
||||||
[ $saveasjpg -eq 0 ] && destformat="png" || destformat="jpg"
|
[ $saveasjpg -eq 0 ] && destformat="png" || destformat="jpg"
|
||||||
# Loop over all the .heic files in the folder
|
# Loop over all the .heic files in the folder
|
||||||
for heic in *.heic; do
|
for heic in *.heic; do
|
||||||
@ -77,7 +93,14 @@ for heic in *.heic; do
|
|||||||
else [ $keepquiet -eq 0 ] && printf "%s\n" "Converting $heic to $img"; fi
|
else [ $keepquiet -eq 0 ] && printf "%s\n" "Converting $heic to $img"; fi
|
||||||
[ -f "$img" ] || let filecount=filecount+1
|
[ -f "$img" ] || let filecount=filecount+1
|
||||||
# If the file we are going to make does not exist, run the command to convert it
|
# If the file we are going to make does not exist, run the command to convert it
|
||||||
[ -f "$img" ] || /usr/local/bin/mogrify -format $destformat "$heic"
|
if [ ! -f "$img" ]; then
|
||||||
|
$mymogrify -format $destformat "$heic"
|
||||||
|
mogrified=$?
|
||||||
|
if [ $mogrified -ne 0 ]; then
|
||||||
|
printf "\n%s\n\n" "${rst}${red}Encountered the above error while trying to mogrify. Exiting.${rst}"
|
||||||
|
exit $mogrified
|
||||||
|
fi
|
||||||
|
fi
|
||||||
# If we don't want the originals, give them the axe
|
# If we don't want the originals, give them the axe
|
||||||
[ $keeporiginals -eq 0 ] && rm "$heic"
|
[ $keeporiginals -eq 0 ] && rm "$heic"
|
||||||
done
|
done
|
||||||
@ -93,4 +116,5 @@ else
|
|||||||
printf "\n%s\n\n" "Found 0 heic files to convert. Are you sure you specified the right directory?"
|
printf "\n%s\n\n" "Found 0 heic files to convert. Are you sure you specified the right directory?"
|
||||||
else
|
else
|
||||||
printf "\n%s\n\n" "Found 0 heic files to convert. Are you sure you're in the right directory?"
|
printf "\n%s\n\n" "Found 0 heic files to convert. Are you sure you're in the right directory?"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
Loading…
x
Reference in New Issue
Block a user