-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbashrc_functions_gif
62 lines (59 loc) · 2.43 KB
/
bashrc_functions_gif
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
# Gif-related
# ===========
function gifspeedchange() {
# args: $gif_path $frame_delay (1 = 0.1s)
local orig_gif="${1?'Missing GIF filename parameter'}"
local frame_delay=${2?'Missing frame delay parameter'}
gifsicle --batch --delay $frame_delay $orig_gif
local newframerate=$(echo "$frame_delay*10" | bc)
echo "new GIF frame rate: $newframerate ms"
}
function gifopt() {
# args: $input_file ($loss_level)
if [ -z "$2" ]
then
# use default of 30
local loss_level=30
elif [[ "$2" =~ ^[0-9]+$ ]] && [ "$2" -ge 30 -a "$2" -le 200 ]
then
local loss_level=$2
else
echo "${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 "$gifname" | sed 's/_reduced_x[0-9]//g')
local outputgif="$basegifname-opt.gif"
gifsicle -O3 $gifdelay --lossy="$loss_level" -o "$outputgif" "$inputgif";
local oldfilesize=$(du -h $inputgif | cut -f1)
local newfilesize=$(du -h $outputgif | cut -f1)
echo "File reduced from $oldfilesize to $newfilesize as $outputgif"
}
function gif_framecount_reducer () {
# args: $gif_path $frames_reduction_factor
local orig_gif="${1?'Missing GIF filename parameter'}"
local reduction_factor=${2?'Missing reduction factor parameter'}
# Extracting the delays between each frames
local orig_delay=$(gifsicle -I "$orig_gif" | sed -ne 's/.*delay \([0-9.]\+\)s/\1/p' | uniq)
# Ensuring this delay is constant
[ $(echo "$orig_delay" | wc -l) -ne 1 ] \
&& echo "Input GIF doesn't have a fixed framerate" >&2 \
&& return 1
# Computing the current and new FPS
local new_fps=$(echo "(1/$orig_delay)/$reduction_factor" | bc)
# Exploding the animation into individual images in /var/tmp
local tmp_frames_prefix="/var/tmp/${orig_gif%.*}_"
convert "$orig_gif" -coalesce +adjoin "$tmp_frames_prefix%05d.gif"
local frames_count=$(ls "$tmp_frames_prefix"*.gif | wc -l)
# Creating a symlink for one frame every $reduction_factor
local sel_frames_prefix="/var/tmp/sel_${orig_gif%.*}_"
for i in $(seq 0 $reduction_factor $((frames_count-1))); do
local suffix=$(printf "%05d.gif" $i)
ln -s "$tmp_frames_prefix$suffix" "$sel_frames_prefix$suffix"
done
# Assembling the new animated GIF from the selected frames
convert -delay $new_fps "$sel_frames_prefix"*.gif "${orig_gif%.*}_reduced_x${reduction_factor}.gif"
# Cleaning up
rm "$tmp_frames_prefix"*.gif "$sel_frames_prefix"*.gif
}