#!/usr/bin/env /bin/bash
#
# Author: Lilian BESSON
# Email: Lilian.BESSON[AT]ens-cachan[DOT]fr
# Date: 23-06-2015
#
# A script to compress a buntch of gif animated files (using gifsicle).
# Copyright (C) 2015 Lilian BESSON (lbesson at ens-cachan dot fr)
#
# Find the latest version on http://perso.crans.org/besson/bin/gifcompress.sh
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see ,
# or .
#
# Requires:
# - gifsicle
#
# References:
# - http://askubuntu.com/a/654305/507614
#
version='0.1'
# If possible, use ~/.color.sh (http://perso.crans.org/besson/bin/.color.sh)
[ -f ~/.color.sh ] && ( . ~/.color.sh ; clear )
function gifcompressf() {
# args: $input_file ($loss_level)
if [ -z "$2" ]; then
# use default of 30
loss_level=30
elif [[ "$2" =~ ^[0-9]+$ ]] && [ "$2" -ge 30 -a "$2" -le 200 ]; then
loss_level=$2
else
echo -e "${2:-"Loss level parameter must be an integer from 30-200."}" 1>&2
exit 1
fi
local inputgif="${1?'Missing input file parameter'}"
local gifname="$(basename ${inputgif} .gif)"
local basegifname=$(echo -e "${gifname}" | sed 's/_reduced_x[0-9]//g')
local outputgif="${basegifname}-opt.gif"
gifsicle -O3 -lossy="${loss_level}" -o "${outputgif}" "${inputgif}";
local oldfilesize=$(du -h ${inputgif} | cut -f1)
local newfilesize=$(du -h ${outputgif} | cut -f1)
echo -e "${green}File ${black}${inputgif}${white} reduced from ${u}${oldfilesize}${U} to ${u}${newfilesize}${U} as ${blue}${outputgif}${white} ..."
}
# Usage
if [ "X$*" = "X" ]; then
echo -e "${yellow}Usage: gifcompress FILE.gif [FILE.gif ...]${white}"
echo -e " Compress the gif files using gifsicle (with optimal options)."
exit 0
fi
# Use this function
log=/tmp/gifcompress_$$.log
echo -e "$0 have been called with the arguments (after processing the options) : ${blue}$@${white}." | tee -a "${log}"
for i in "$@"; do
echo -e " Calling the function ${magenta}'gifcompressf'${white} for the argument ${u}'${i}'${U} (on pwd = $(pwd))..." | tee -a "${log}"
gifcompressf "$i" | tee -a "${log}"
echo -e "Done for ${magenta}'gifcompressf'${white} on ${u}'${i}'${U}..." | tee -a "${log}"
done
# End of gifcompress.sh