forked from jaredrummler/bootanimation-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
boot2gif.sh
executable file
·124 lines (113 loc) · 2.89 KB
/
boot2gif.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/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
#
get_image_with_most_colors() {
local images=$@
local n=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 $n ]
then
n=$color_count
path=$image
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
# 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#*=}"
;;
*) # invalid/unknown arg
esac
done
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" "./thumbnail.$extension"
fi
if $create_gif || $create_resized_gif
then
convert -delay $delay -loop 0 $images ./animation.gif
fi
if $create_resized_gif
then
echo convert ./animation.gif -resize ${gif_size}x -coalesce -layers optimize ./animation_${gif_size}w.gif
convert ./animation.gif -resize ${gif_size}x -coalesce -layers optimize ./animation_${gif_size}w.gif
if ! $create_gif; then rm ./animation.gif; fi
fi
# clean up
rm -rf "$temp"