Skip to content

Instantly share code, notes, and snippets.

@jaredrummler
Last active February 1, 2023 23:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredrummler/cf4a8fd53a0cff5f8802d55f4f5d14aa to your computer and use it in GitHub Desktop.
Save jaredrummler/cf4a8fd53a0cff5f8802d55f4f5d14aa to your computer and use it in GitHub Desktop.
Convert an Android boot animation to an animated GIF
#!/bin/bash
################################################################################
#
# boot2gif.sh
#
# DESCRIPTION: Convert an Android boot animation to a GIF
# You will need ImageMagick to run this script.
#
# USAGE:
#
# boot2gif.sh FILE [OPTIONS]...
#
# --create-gif Creates a GIF from the boot animation ZIP archive
# The GIF will be created at ./animation.gif
#
# --create-resized-gif=[SIZE] Create a GIF a resize it.
# The SIZE is the GIF's width in pixels
#
# --create-thumbnail Finds the best option for a preview/thumbnail.
# The image will be created at ./thumbnail.jpg
#
# --out=[DIRECTORY] Set the output directory
#
get_image_with_most_colors() {
local images=$@
local colors=0
local histogram_count=0
local path="";
for image in $images;
do
color_count=$(identify -format %k "$image")
case $color_count in
''|*[!0-9]*)
continue ;;
*)
if [ $color_count -gt $colors ]
then
colors=$color_count
path=$image
histogram_count=$(convert $image rose: -colorspace rgb -colors 10 miff:- | convert - -format "%c" histogram:info: | wc -l)
elif [ $color_count -eq $colors ]
then
# Same amount of colors. Check histogram count
histogram_line_count=$(convert $image rose: -colorspace rgb -colors 10 miff:- | convert - -format "%c" histogram:info: | wc -l)
if [ $histogram_line_count -gt $histogram_count ]
then
histogram_count=$histogram_line_count
path=$image
fi
fi
esac
done
echo $path
}
case $1 in
-h|--help) # never done this before ༽つ۞﹏۞༼つ
println=false; echo
while read line; do
case $line in
\#*DESCRIPTION*) println=true; echo ${line:1} ;;
\#*) if $println; then echo ${line:1}; fi ;;
*) break;
esac;
done < "$0"
exit 0
esac
create_thumbnail=false
create_gif=false
create_resized_gif=false
gif_size=300
file=$1
out=$(dirname $0)
# parse args
while [[ $# > 1 ]]
do
shift
arg="$1"
case $arg in
--create-gif)
create_gif=true
;;
--create-thumbnail)
create_thumbnail=true
;;
--create-resized-gif=*)
create_resized_gif=true
gif_size="${arg#*=}"
;;
--out=*)
out="${arg#*=}"
;;
*) # invalid/unknown arg
esac
done
case "$(uname -s)" in
CYGWIN*) out=$(cygpath -a "$out")
esac
temp=$(mktemp -d)
unzip -q -o "$file" -d "$temp"
# read the desc.txt file
desc_txt="$temp/desc.txt"
first_line=`head -n 1 $desc_txt | dos2unix`
width=`echo $first_line | awk '{print $1}'` # unused
height=`echo $first_line | awk '{print $2}'` # unused
size=`echo "${width}x${height}"` # unused
fps=`echo $first_line | awk '{print $3}'`
folders=`awk '{print $4}' $desc_txt | dos2unix`
delay=`echo "scale=2; 100/${fps}" | bc` # unused # fps = 1/(delay in seconds) = 100/(delay ticks)
# get the list of images in the correct order
files=""
for folder in $folders
do
files=$(find "$temp/$folder/" -type f \( -iname \*.png -o -iname \*.jpg -o -iname \*.jpeg \) | sort)
images="$images $files"
done
if $create_thumbnail
then
thumbnail=$(get_image_with_most_colors $images)
extension="${thumbnail##*.}"
cp "$thumbnail" "$out/frame.$extension"
fi
if $create_gif #|| $create_resized_gif
then
convert -delay $delay -loop 0 $images "$out/animation.gif"
fi
if $create_resized_gif
then
convert "$out/animation.gif" -resize ${gif_size}x -coalesce -layers optimize "$out/animation_${gif_size}w.gif"
#if ! $create_gif; then rm "$out/animation.gif"; fi
fi
# clean up
rm -rf "$temp"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment