/
thumbnails.go
294 lines (259 loc) · 7.13 KB
/
thumbnails.go
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"os"
"os/user"
"path"
"reflect"
"strconv"
"strings"
"text/template"
"github.com/dulo-tech/service-thumbnails/cli"
"github.com/dulo-tech/service-thumbnails/core"
"github.com/dulo-tech/service-thumbnails/http"
)
func main() {
opts := config()
if opts.PrintHelp {
executeHelpTemplate("")
} else if opts.PrintVersion {
fmt.Println(core.BuildInfo())
os.Exit(0)
}
switch opts.Mode {
case "cli":
if anyEmptyString(opts.InFile, opts.OutFile, opts.ThumbType) {
executeHelpTemplate("Missing InFile, OutFile, or ThumbType.")
}
if !inArrayString(opts.ThumbType, core.ValidThumbTypes) {
executeHelpTemplate("Invalid thumbnail type.")
}
cli.Go()
case "http":
http.Go()
default:
executeHelpTemplate("Invalid mode.")
}
}
// config parses command line arguments and reads from configuration files.
//
// First attempts to read from a configuration file specified at the command
// line. Then tries to read from .service-thumbnails.conf in the user's home
// directory. Then tries reading from /etc/service-thumbnails.conf. Finally
// parses the command line arguments. The command line arguments override
// configuration file values.
func config() *core.Options {
confCli := ""
confHome := ""
confEtc := "/etc/service-thumbnails.conf"
u, err := user.Current()
if err == nil {
confHome = path.Join(u.HomeDir, "/.service-thumbnails.conf")
}
/*
set := flag.NewFlagSet("conf", flag.ContinueOnError)
set.StringVar(
&confCli,
"conf",
"",
"Path to configuration file.")
set.Parse(os.Args[1:])
*/
if confCli != "" {
readConfigFile(confCli, core.Opts)
} else if core.FileExists(confHome) {
readConfigFile(confHome, core.Opts)
} else if core.FileExists(confEtc) {
readConfigFile(confEtc, core.Opts)
}
flag.String(
"conf",
"",
"Path to configuration file.")
flag.StringVar(
&core.Opts.Mode,
"m",
core.Opts.Mode,
"Running mode, either 'cli' or 'http'. Defaults to 'cli'.")
flag.BoolVar(
&core.Opts.PrintHelp,
"help",
core.Opts.PrintHelp,
"Display command help.")
flag.BoolVar(
&core.Opts.PrintVersion,
"version",
core.Opts.PrintVersion,
"Display the app version and quit.")
flag.BoolVar(
&core.Opts.Quiet,
"q",
core.Opts.Quiet,
"Run in quiet mode.")
flag.IntVar(
&core.Opts.SkipSeconds,
"s",
core.Opts.SkipSeconds,
"Skip this number of seconds into the video before thumbnailing.")
flag.IntVar(
&core.Opts.Count,
"c",
core.Opts.Count,
"Number of thumbs to generate in a sprite. 30 is the default.")
flag.IntVar(
&core.Opts.Width,
"w",
core.Opts.Width,
"The thumbnail width. Overrides the built in defaults.")
flag.StringVar(
&core.Opts.ThumbType,
"t",
core.Opts.ThumbType,
"The type of thumbnail to generate. 'simple' is the default.")
flag.StringVar(
&core.Opts.InFile,
"i",
core.Opts.InFile,
"The input video file. Separate multiple files with a comma.")
flag.StringVar(
&core.Opts.OutFile,
"o",
core.Opts.OutFile,
"The output image file.")
flag.StringVar(
&core.Opts.Host,
"h",
core.Opts.Host,
"The host name to listen on.")
flag.IntVar(
&core.Opts.Port,
"p",
core.Opts.Port,
"The port to listen on.")
flag.Parse()
return core.Opts
}
// ExecuteHelpTemplate() prints the command line help using the given template and exits.
func executeHelpTemplate(errMsg string) {
buff := bytes.Buffer{}
flag.VisitAll(func(f *flag.Flag) {
buff.WriteString(fmt.Sprintf("\t-%-8s%s\n", f.Name, f.Usage))
})
data := struct {
BuildInfo string
Flags string
Error string
}{
core.BuildInfo(),
buff.String(),
errMsg,
}
t, _ := template.New("help").Parse(helpTemplate)
t.Execute(os.Stdout, data)
os.Exit(1)
}
// readConfigFile sets the values in opts by reading a configuration file.
func readConfigFile(file string, opts *core.Options) {
of := reflect.ValueOf(opts)
st := of.Elem()
fin, err := os.Open(file)
if err != nil {
panic(err)
}
defer fin.Close()
line := 1
scanner := bufio.NewScanner(fin)
for scanner.Scan() {
text := strings.Trim(scanner.Text(), " \t\r\n")
if !strings.HasPrefix(text, "#") && text != "" {
parts := strings.SplitN(text, "=", 2)
if len(parts) != 2 {
panic(fmt.Sprintf("Invalid configuration at line %d: %q", line, text))
}
parts[0] = strings.Trim(parts[0], " \t")
parts[1] = strings.Trim(parts[1], " \t")
field := st.FieldByName(parts[0])
if field.IsValid() && field.CanSet() {
if field.Kind() == reflect.Int {
x, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
panic(fmt.Sprintf("Invalid configuration at line %d. Expecting integer: %q", line, text))
}
if !field.OverflowInt(x) {
field.SetInt(x)
}
} else if field.Kind() == reflect.String {
field.SetString(parts[1])
} else if field.Kind() == reflect.Bool {
x := strings.ToLower(parts[1])
if x == "true" || x == "yes" || x == "1" {
field.SetBool(true)
} else {
field.SetBool(false)
}
} else {
panic(fmt.Sprintf("Invalid configuration at line %d: %q", line, text))
}
}
}
line++
}
if err := scanner.Err(); err != nil {
panic(err)
}
}
// inArrayString returns a boolean value indicating whether the needle is found in the haystack.
func inArrayString(needle string, haystack []string) bool {
for _, val := range haystack {
if needle == val {
return true
}
}
return false
}
// anyEmpty returns a boolean value indicating whether any of the given arguments are empty.
func anyEmptyString(values ...string) bool {
for _, val := range values {
if val == "" {
return true
}
}
return false
}
// helpTemplate is the template used for displaying command line help.
const helpTemplate = `{{.BuildInfo}}
{{if .Error}}
{{.Error}}
{{end}}
The app can run as a command line app or as an http server. Use the -m
switch to change modes. See usage and examples of each below.
Options can be set using a configuration file by using the -conf switch.
service-thumbnails -conf thumbnails.conf
When the -conf switch isn't used the app will try to read from
.service-thumbnails.conf in the user's directory. Finally the app will try to
read from /etc/service-thumbnails.conf.
See the example thumbnails.conf for a description of each configuration value.
OPTIONS:
{{.Flags}}
CLI USAGE:
thumbnailer -t <sprite|simple> -i <video> -o <image>
<sprite|simple> determines the type of thumbnail being generated. Either
a sprite or a simple thumbnail. Simple is the default when not specified.
<video> is one or more source videos. Separate multiple videos with commas.
<image> may contain the place holders {name} and {type} which correspond
to the name of the source video (without file extension) and the type of
of thumbnail. One of 'sprite' or 'simple'. The <image> may also contain
the verb %d which will be replaced with the file number. See the fmt package
for more information on verbs.
CLI EXAMPLES:
thumbnailer -t sprite -i source.mp4 -o thumb.jpg
thumbnailer -i source1.mp4,source2.mp4 -o out%02d.jpg
thumbnailer -t sprite -i source.mp4 -o thumb{name}{type}.jpg
HTTP USAGE:
thumbnailer -m http -h <host> -p <port>
HTTP EXAMPLES:
thumbnailer -m http -h 127.0.0.1 -p 3366
`