-
Notifications
You must be signed in to change notification settings - Fork 0
/
video2gif.sh
executable file
·97 lines (81 loc) · 2.08 KB
/
video2gif.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
#!/bin/bash
# Script to convert video files to GIFs using ffmpeg
FFMPEG_PATH=$(which ffmpeg)
if [[ -z FFMPEG_PATH ]]; then
echo "FATAL: ffmpeg was not found in your path - please install beforehand"
exit 1
fi
FPS=10
SCALE=1
CUTOFF=0
FULLPATH=
function usage() {
echo "Usage: $0 [-f|--fps <fps>] [-s|--scale <scale>] file"
echo
echo "Arguments:"
echo " file File to convert to GIF"
echo "Options:"
echo " -h|--help Print this help"
echo " -f|--fps Framerate (FPS) to use for GIF (default: 10)"
echo " -s|--scale Scale ratio to use during conversion - will be used as 1/x,"
echo " i.e. a scale of 2 will result in half the size (default: 1)"
echo " -c|--cutoff Number of seconds to cut off at the end (default: 0)"
exit $1
}
while [[ $# -gt 0 ]]; do
KEY=$1
case $KEY in
-h | --help)
usage 0
;;
-f | --fps)
FPS=$2
shift
;;
-s | --scale)
SCALE=$2
shift
;;
-c | --cutoff)
CUTOFF=$2
shift
;;
*)
if [[ $# -eq 1 ]]; then
FULLPATH=$KEY
shift
else
echo "ERROR: Unknown option: $KEY"
echo
usage 1
fi
;;
esac
shift
done
if [[ -z $FULLPATH ]]; then
echo "Required argument file is missing"
echo
usage 1
fi
if [[ ! -e $FULLPATH ]]; then
echo "File $FULLPATH does not exist"
echo
usage 1
fi
FILENAME=${FULLPATH%%.*}
GIF="${FILENAME}.gif"
PALETTE="${FILENAME}-palette.png"
echo "Converting $FULLPATH to $GIF..."
FILTERS="fps=$FPS,scale=iw/$SCALE:ih/$SCALE:flags=lanczos"
if [[ $CUTOFF -gt 0 ]]; then
DURATION=$(ffprobe -loglevel quiet -of compact=p=0:nk=1 -show_entries format=duration -i "$FULLPATH")
DURATION_DIFF=$(echo "$DURATION - $CUTOFF" | bc -l)
ffmpeg -v warning -i $FULLPATH -vf "$FILTERS,palettegen" -t $DURATION_DIFF -y $PALETTE
ffmpeg -v warning -i $FULLPATH -i $PALETTE -t $DURATION_DIFF -lavfi "$FILTERS [x]; [x][1:v] paletteuse" -y $GIF
else
ffmpeg -v warning -i $FULLPATH -vf "$FILTERS,palettegen" -y $PALETTE
ffmpeg -v warning -i $FULLPATH -i $PALETTE -lavfi "$FILTERS [x]; [x][1:v] paletteuse" -y $GIF
fi
rm $PALETTE
echo "Done - see output $GIF"