forked from noctuid/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makegif
executable file
·110 lines (98 loc) · 3.11 KB
/
makegif
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
#!/bin/bash
# http://www.reddit.com/r/reactiongifs/comments/x55z9/after_i_learned_how_to_make_large_well_compressed/
# http://superuser.com/questions/556029/how-do-i-convert-a-video-to-gif-using-ffmpeg-with-reasonable-quality
# Simple wrapper for creating a gif from a video using ffmpeg, imagemagick, and optionally gifsicle
# TODO:
# subtitles
function print_help() {
echo "
Creates a gif and/or frames from a video file (last argument) or existing frames (-u).
-w <width> specify a width for the final gif (default: 600)
-f <fps> specify an fps for the final gif (default: 10)
-o <filename> specify an output name (default: output.gif)
-d <dir> specify the dir to create the frames and output gif in (default: ~/move/gif)
-O <opt level> level/whether to optimize with gifsicle (1-3, default: none/false)
-z <percent> choose fuzz percent for convert (default: 1.6)
-c just create frames from video (default: false)
options can be used with: -w, -f
-u use existing frames in frames folder and don't create frames (default false)
options can be used with: -z, -f, -o, -O
Only one or none of the above two should be set.
Note: The fps option should be the same value when creating the frames and
when making a gif from existing frames or the speed will be off.
-s copy frames/ to frames_<date>/
-h print help and exit
"
if [[ $1 == illegal_opt ]]; then
exit 1
else
exit 0
fi
}
# default values
width=600
fps=10
fuzz_percent=1.6
optimize=false
cur_time=$(date +%Y.%m.%d_%H:%M:%S)
output_name=${cur_time}_output.gif
output_dir=$HOME/move/gif
create_frames_only=false
use_existing_frames=false
save_frames=false
while getopts :w:f:z:O:o:d:cush opt
do
case $opt in
w) width=$OPTARG;;
f) fps=$OPTARG;;
z) fuzz_percent=$OPTARG;;
O) optimize=true
optimization_level=$OPTARG;;
o) output_name="$OPTARG";;
d) output_dir="$OPTARG";;
c) create_frames_only=true;;
u) use_existing_frames=true;;
s) save_frames=true;;
h) print_help;;
*) print_help illegal_opt;;
esac
done
create_frames() {
# remove previously created frames
rm frames/*
# make new frames
ffmpeg -i "$full_path" -vf scale="$width":-1 -r "$fps" frames/ffout%05d.png
}
make_gif() {
# -layers OptimizePlus
convert -fuzz "$fuzz_percent"% -delay 1/"$fps" \
-loop 0 frames/*.png \
-layers OptimizeTransparency "$output_name"
if $optimize; then
gifsicle -O"$optimization_level" --colors 256 "$output_name" \
> "${output_name%.*}"_gifsicled"$optimization_level".gif
fi
}
if ! $use_existing_frames; then
# last arg should be the filename, unless using existing frames
file=${!#}
full_path=$(readlink -f "$file")
fi
mkdir -p "$output_dir"/frames
cd "$output_dir"
if $create_frames_only && $use_existing_frames; then
echo "create_frames_only and use_existing_frames are not meant to both be used."
sleep 1
print_help illegal_opt
elif $create_frames_only; then
create_frames
elif $use_existing_frames; then
make_gif
else
create_frames
make_gif
fi
if $save_frames; then
mkdir frames_"${cur_time}"
cp frames/* frames_"${cur_time}"
fi