-
Notifications
You must be signed in to change notification settings - Fork 21
/
thunar-video-to-gif.sh
executable file
·170 lines (135 loc) · 3.57 KB
/
thunar-video-to-gif.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/bin/sh
#
# Convert a video file to an animated gif (high quality mode).
#
# * Put this file into your home binary dir: ~/bin/
# * Make it executable: chmod +x
#
#
# Required Software:
# -------------------------
# * zenity (for gui mode - default)
# * ffmpeg
#
#
# Thunar Integration
# ------------------------
#
# Command: ~/bin/thunar-video-to-gif.sh -f %f -t %n
# File Pattern: *
# Appear On: Video Files
#
#
# Usage:
# -------------------------
# thunar-video-to-gif.sh -f <filename> [-c]
#
# required:
# -f input filename
#
# optional:
# -c no-gui, show console output (zenity not required)
# default is to show gui
#
#
# Note:
# -------------------------
#
# Feel free to adjust/improve and commit back to:
# https://github.com/cytopia/thunar-custom-actions
#
# Test if argument is an integer.
#
# @param mixed
# @return integer 0: is number | 1: not a number
isint(){
printf "%d" "${1}" >/dev/null 2>&1 && return 0 || return 1;
}
usage() {
echo "$0 -f <filename> [-c]"
echo
echo " required:"
echo " -f input filename"
echo
echo " optional:"
echo " -c no-gui, show console output (zenity not required)"
echo " default is to show gui"
echo
exit 1
}
# No console output
c="no"
while getopts ":f:c" i; do
case "${i}" in
f)
f=${OPTARG}
;;
# Console output instead of zenity
c)
c="yes"
;;
*)
echo "Error - unrecognized option $1" 1>&2;
usage
;;
esac
done
shift $((OPTIND-1))
# Check if file is specified
if [ -z "${f}" ]; then
echo "Error - no file specified" 1>&2;
usage
fi
# Check for mktemp
if ! command -v mktemp >/dev/null 2>&1 ; then
echo "Error - 'mktemp' not found (requited to build the palette)." 1>&2
exit 1
fi
# Check if zenity exists (on gui output)
if [ "${c}" != "yes" ]; then
if ! command -v zenity >/dev/null 2>&1 ; then
echo "Error - 'zenity' not found." 1>&2
exit 1
fi
fi
# Check if ffmpeg exists
if ! command -v ffmpeg >/dev/null 2>&1 ; then
echo "Error - 'ffmpeg' not found." 1>&2
exit 1
fi
########################## OPTIONS ###############################
# Tmpfile for palette (to produce better quality)
FILE_PALETTE="$(mktemp).png"
########################## console output ###############################
# Do we have textbased output?
if [ "${c}" = "yes" ]; then
while true; do
# shellcheck disable=SC2039
read -r -p "Enter output video width in pixel (integer): " VIDEO_WIDTH
if ! isint "${VIDEO_WIDTH}"; then
echo "Please enter a valid integer"
else
break;
fi
done
# FFMPEG Filters
FF_FILTERS="fps=15,scale=${VIDEO_WIDTH}:-1:flags=lanczos"
ffmpeg -v warning -i "${f}" -vf "${FF_FILTERS},palettegen" -y "${FILE_PALETTE}"
ffmpeg -v warning -i "${f}" -i "${FILE_PALETTE}" -lavfi "${FF_FILTERS} [x]; [x][1:v] paletteuse" -y "${f}.gif"
########################## gui output ###############################
else
while true; do
VIDEO_WIDTH="$(zenity --entry --title="Video width" --text="Enter output video width in pixel (integer):")"
if ! isint "${VIDEO_WIDTH}"; then
zenity --error --text="Not a valid integer"
else
break;
fi
done
# FFMPEG Filters
FF_FILTERS="fps=15,scale=${VIDEO_WIDTH}:-1:flags=lanczos"
ffmpeg -v warning -i "${f}" -vf "${FF_FILTERS},palettegen" -y "${FILE_PALETTE}" | zenity --title="Run 1/2" --text="Run 1/2\nCreating palette for better quality" --progress --pulsate
ffmpeg -v warning -i "${f}" -i "${FILE_PALETTE}" -lavfi "${FF_FILTERS} [x]; [x][1:v] paletteuse" -y "${f}.gif" | zenity --title="Run 2/2" --text="Run 2/2\n Converting Video to gif" --progress --pulsate
fi
rm "${FILE_PALETTE}"
exit 0