-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanigif
More file actions
executable file
·111 lines (95 loc) · 2.85 KB
/
anigif
File metadata and controls
executable file
·111 lines (95 loc) · 2.85 KB
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
#!/usr/bin/env bash
usage ()
{
usage="Usage: $0 [-rotate <90>] [-yoyo <1|0>] [-size <'[300x300]'>] [-out <\$(date +%s).gif>] [-preview <ql>] <image.jpg>|<image.jpg,...>
-rotate <90> specifies degree of rotation (default 90)
-yoyo <1|0> should gif reverse frames to provide back-and-forth (yoyo) effect (default true)
-size <'[300x300]'> size of bounding box for frames to fit in (default 300x300)
-out <output.gif> name of output file (default unix_epoch_timestamp.gif)
-preview <cmd> command to execute to view output .gif
Input file(s) can be a single image file, in which case all files within a second +/- its timestamp will be used as input files. Or you can give a list of input files that will be considered individually to be the frames of the output file.
"
printf %s\\n "$usage"
exit
}
err(){
printf >&2 '[ERROR] %s\n' "$*"
exit 1
}
ql ()
{
out=$(qlmanage -p "$@" 2>&1) || echo "$out"
}
command -v convert &>/dev/null || err "You must have the ''convert'' tool from ImageMagick in your PATH."
(($#)) || usage
# base comma d to which we append aadditional arguments
cmd=(convert -loop 0)
# default to 90º rotation by default
rotate=90
# default to going backwards at the end of the video
yoyo=1
# default to re-sizing on read to fit in a 300x300 box
size='[300x300]'
# should we preview (open) the .gif after generating it?
preview=ql
# (default outfile set after option processing)
outfile=
while :; do
case $1 in
-h)
usage
;;
-rotate)
rotate=$2
shift
;;
-yoyo)
yoyo=1
;;
-out)
outfile=$2
shift
;;
-size)
size=$2
shift
;;
-preview)
preview=$2
shift
;;
*)
break
esac
shift
done
cmd+=( -rotate "$rotate" )
# default outfile is current timestamp in seconds to try to prevent chaos and overwrites
[[ $outfile ]] || outfile=$(date +%s).gif
[[ -e $outfile ]] && err "Outfile $outfile already exists"
frames=();
if (($# == 1)); then
infile=$1;
[[ -f $infile ]] || err "Infile should be a single, existing image file ('$infile' is not a file)"
ttime=$(stat -t %s -f %m "$infile");
for f in "${infile%/*}"/* ;
do
ctime=$(stat -t %s -f %m "$f");
if (( ctime >= ttime-1 && ctime <=ttime+1 )); then
frames+=( "${f}${size}" );
fi;
done;
else
for f; do
[[ -f $f ]] || err "Infile '$infile' does not exist"
frames+=( "${f}${size}" );
done
fi
if ((yoyo)); then
for ((i=${#frames[@]};i>0;)); do
frames+=( "${frames[--i]}" )
done
fi
# printf "%q " "${cmd[@]}" "${frames[@]}" "$outfile"; printf \\n
"${cmd[@]}" "${frames[@]}" "$outfile";
[[ $preview ]] && eval "$preview" "$outfile"