-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
447 lines (376 loc) · 11 KB
/
main.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package main
// TODO: check shellPath logic - doesn't support DOS paths
import (
"errors"
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/jessevdk/go-flags"
)
type CommandLineOptions struct {
FfmpegPath string `short:"m" long:"ffmpeg" description:"Path to ffmpeg."`
FrameDirPath string `short:"d" long:"frame-dir" description:"Path to directory that will contain the captured frames. Default: ~/Pictures/pmcctv"`
RemoteDir string `short:"r" long:"remote-dir" description:"Remote location where frames will be saved to. Must contain a path compatible with scp (eg. user@someip:~/pmcctv)."`
RemotePort string `short:"p" long:"remote-port" description:"Port of remote location where frames will be saved to. If not set, whatever is the default scp port will be used (should be 22)."`
BurstModeDuration int `short:"b" long:"burst-mode-duration" description:"Duration of burst mode, in seconds. Set to -1 to disable burst mode altogether. Default: 10."`
FramesTtl int `short:"t" long:"time-to-live" description:"For how long captured frames should be kept, in days. Default: 7."`
InputDevice string `short:"i" long:"input-device" description:"Name of capture input device. Default: auto-detect, except on Windows."`
}
var useRsync = false
var captureWorkerDone = make(chan bool)
var capturedFrames = make(chan string, 4096)
func captureFrame(filePath string, inputDevice string) error {
// Linux: ffmpeg -y -loglevel fatal -f video4linux2 -i /dev/video0 -r 1 -t 0.0001 $FILENAME
// OSX: $FFMPEG -loglevel fatal -f avfoundation -i "" -r 1 -t 0.0001 $FILENAME
// Windows: ffmpeg -y -loglevel fatal -f dshow -i video="USB2.0 HD UVC WebCam" -r 1 -t 0.0001 test.jpg
var args []string
if runtime.GOOS == "linux" { // Linux
args = []string{
"-y",
"-loglevel", "error",
"-f", "video4linux2",
"-i", inputDevice,
"-r", "1",
"-t", "0.0001",
filePath,
}
} else if runtime.GOOS == "darwin" { // OSX
args = []string{
"-y",
"-loglevel", "error",
"-f", "avfoundation",
"-i", inputDevice,
"-r", "1",
"-t", "0.0001",
filePath,
}
} else if runtime.GOOS == "windows" { // Windows
args = []string{
"-y",
"-loglevel", "error",
"-f", "dshow",
"-i", "video=" + inputDevice,
"-r", "1",
"-t", "0.0001",
filePath,
}
} else {
panic("Unsupported OS: " + runtime.GOOS)
}
cmd := exec.Command("ffmpeg", args...)
buff, err := cmd.CombinedOutput()
if err != nil {
return errors.New(fmt.Sprintf("%s: %s", err, string(buff)))
}
return nil
}
func compareFrames(path1 string, path2 string, diffPath string) (int, error) {
//compare -fuzz 20% -metric ae $PREVIOUS_FILENAME $FILENAME diff.png 2> $DIFF_RESULT_FILE
args := []string{
"-fuzz", "20%",
"-metric", "ae",
path1,
path2,
diffPath,
}
cmd := exec.Command("compare", args...)
buff, err := cmd.CombinedOutput()
// On Windows, `compare` appears to always return an error code, even when successful
// so the `parseInt` code after that will take care of checking if it's really an
// error or if it worked.
if err != nil && runtime.GOOS != "windows" {
return 0, errors.New(fmt.Sprintf("%s: %s", err, string(buff)))
}
r, err := strconv.ParseInt(strings.Trim(string(buff), "\n\r\t "), 10, 64)
if err != nil {
return 0, errors.New(fmt.Sprintf("%s: %s", err, string(buff)))
}
return int(r), nil
}
func remoteCopy(path string, opts CommandLineOptions) error {
// scp <path> <remote_dir>
args := []string{
path,
}
if opts.RemotePort != "" {
args = append(args, "-P")
args = append(args, opts.RemotePort)
}
args = append(args, opts.RemoteDir)
cmd := exec.Command("scp", args...)
buff, err := cmd.CombinedOutput()
if err != nil {
return errors.New(fmt.Sprintf("%s: %s", err, string(buff)))
}
return nil
}
func multipleRemoteCopy(paths []string, opts CommandLineOptions) error {
args := []string{}
// It only makes sense to use rsync if there's more than one file to transfer
if useRsync && len(paths) > 1 {
// rsync -a <paths> <remote_dir>
args = append(args, "-a")
for _, path := range paths {
args = append(args, shellPath(path))
}
if opts.RemotePort != "" {
args = append(args, "-e")
args = append(args, "ssh -p " + opts.RemotePort)
}
args = append(args, opts.RemoteDir)
cmd := exec.Command("rsync", args...)
buff, err := cmd.CombinedOutput()
if err != nil {
return errors.New(fmt.Sprintf("%s: %s", err, string(buff)))
}
} else {
// scp <path> <remote_dir>
for _, path := range paths {
args = append(args, shellPath(path))
}
if opts.RemotePort != "" {
args = append(args, "-P")
args = append(args, opts.RemotePort)
}
args = append(args, opts.RemoteDir)
cmd := exec.Command("scp", args...)
buff, err := cmd.CombinedOutput()
if err != nil {
return errors.New(fmt.Sprintf("%s: %s", err, string(buff)))
}
}
return nil
}
func captureWorker(opts CommandLineOptions) {
previousFramePath := ""
lastMotionTime := time.Now().Add(-60 * time.Second)
burstMode := false
for {
now := time.Now()
baseName := "cap_" + now.Format("20060102T150405") + "_" + fmt.Sprintf("%09d", now.Nanosecond())
framePath := opts.FrameDirPath + "/" + baseName + ".jpg"
err := captureFrame(framePath, opts.InputDevice)
if err != nil {
fmt.Printf("Error: %s\n", err)
continue
}
if previousFramePath != "" {
diff, err := compareFrames(previousFramePath, framePath, framePath+".diff.png")
if err != nil {
fmt.Printf("Error: %s\n", err)
diff = 9999999
}
os.Remove(framePath + ".diff.png")
burstModeMarker := ""
if burstMode {
burstModeMarker = "[BM] "
}
if diff <= 20 {
fmt.Printf(burstModeMarker+"Same as previous image: delete (Diff = %d)\n", diff)
os.Remove(framePath)
} else {
fmt.Printf(burstModeMarker+"Different image: keep (Diff = %d)\n", diff)
capturedFrames <- framePath
previousFramePath = framePath
lastMotionTime = now
}
} else {
capturedFrames <- framePath
previousFramePath = framePath
}
if opts.BurstModeDuration >= 0 {
burstMode = now.Sub(lastMotionTime) <= time.Duration(opts.BurstModeDuration)*time.Second
}
waitingTime := 1000 * time.Millisecond
if burstMode {
waitingTime = 15 * time.Millisecond
}
time.Sleep(waitingTime)
}
}
func remoteCopyWorker(opts CommandLineOptions) {
for {
var paths []string
itemCount := len(capturedFrames)
if itemCount <= 0 {
time.Sleep(50 * time.Millisecond)
continue
}
for i := 0; i < itemCount; i++ {
f := <-capturedFrames
paths = append(paths, f)
}
err := multipleRemoteCopy(paths, opts)
if err != nil {
fmt.Printf("Error: could not remote copy \"%s\": %s", paths, err)
}
}
}
func cleanUpLocalFilesWorker(opts CommandLineOptions) {
for {
cleanUpLocalFiles(opts)
time.Sleep(1 * time.Hour)
}
}
func cleanUpRemoteFilesWorker(opts CommandLineOptions) {
for {
cleanUpRemoteFiles(opts)
time.Sleep(1 * time.Hour)
}
}
func cleanUpLocalFiles(opts CommandLineOptions) error {
args := []string{}
args = appendCleanUpFindCommandArgs(args, opts.FrameDirPath, opts.FramesTtl)
cmd := exec.Command("find", args...)
buff, err := cmd.CombinedOutput()
if err != nil {
return errors.New(fmt.Sprintf("%s: %s", err, string(buff)))
}
return nil
}
func appendCleanUpFindCommandArgs(args []string, dir string, framesTtl int) []string {
args = append(args, dir)
args = append(args, "-name")
args = append(args, "cap_*.jpg")
args = append(args, "-mtime")
args = append(args, "+"+strconv.Itoa(framesTtl))
args = append(args, "-delete")
return args
}
func cleanUpRemoteFiles(opts CommandLineOptions) error {
args := []string{}
s := strings.Split(opts.RemoteDir, ":")
userHost := s[0]
dir := "."
if len(s) >= 2 {
dir = s[1]
}
if opts.RemotePort != "" {
args = append(args, "-p")
args = append(args, opts.RemotePort)
}
args = append(args, userHost)
args = append(args, "find")
args = appendCleanUpFindCommandArgs(args, dir, opts.FramesTtl)
cmd := exec.Command("ssh", args...)
buff, err := cmd.CombinedOutput()
if err != nil {
return errors.New(fmt.Sprintf("%s: %s", err, string(buff)))
}
return nil
}
func commandIsAvailable(commandName string) bool {
err := exec.Command("type", commandName).Run()
if err == nil {
return true
} else {
err = exec.Command("sh", "-c", "type "+commandName).Run()
if err == nil {
return true
}
}
return false
}
var cygwinCheckDone_ = false
var isCygwin_ = false
func isCygwin() bool {
if cygwinCheckDone_ {
return isCygwin_;
}
cygwinCheckDone_ = true
r, err := exec.Command("uname", "-o").CombinedOutput()
if err != nil {
isCygwin_ = false
} else {
isCygwin_ = strings.ToLower(strings.Trim(string(r), "\r\n\t ")) == "cygwin"
}
return isCygwin_
}
func shellPath(path string) string {
if isCygwin() {
r, err := exec.Command("cygpath", "-u", path).CombinedOutput()
if err != nil {
fmt.Println("Error: cannot convert Cygwin path: %s", path)
}
return strings.Trim(string(r), "\r\n\t ")
}
return filepath.ToSlash(path)
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
var err error
var opts CommandLineOptions
flagParser := flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash)
args, err := flagParser.Parse()
if err != nil {
t := err.(*flags.Error).Type
if t == flags.ErrHelp {
flagParser.WriteHelp(os.Stdout)
os.Exit(0)
} else {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
}
_ = args
if opts.BurstModeDuration == 0 {
opts.BurstModeDuration = 10
}
if opts.FramesTtl == 0 {
opts.FramesTtl = 7
}
if opts.InputDevice == "" {
if runtime.GOOS == "linux" {
opts.InputDevice = "/dev/video0"
} else if runtime.GOOS == "darwin" {
opts.InputDevice = ""
} else {
args = []string{
"-hide_banner",
"-list_devices",
"true",
"-f", "dshow",
"-i", "dummy",
}
cmd := exec.Command("ffmpeg", args...)
buff, _ := cmd.CombinedOutput()
fmt.Println("Please specify the input device that should be used to capture the video. It can be any of the devices listed below under \"DirectShow video devices\":")
fmt.Println("")
fmt.Println("Then run the command again with the --input-device option. eg. pmcctv --input-device \"My USB WebCam\"")
fmt.Println("");
fmt.Println(string(buff))
os.Exit(1)
}
}
if opts.FrameDirPath == "" {
u, err := user.Current()
if err != nil {
fmt.Println("No frame dir specified and cannot detect default Pictures dir. Please specify it with the --frame-dir option")
os.Exit(1)
}
opts.FrameDirPath = u.HomeDir + "/Pictures/pmcctv"
}
opts.FrameDirPath = strings.TrimRight(opts.FrameDirPath, "/")
os.MkdirAll(opts.FrameDirPath, 0700)
fmt.Printf("Input device: %s\n", opts.InputDevice)
fmt.Printf("Local frame dir: %s\n", opts.FrameDirPath)
if opts.RemoteDir != "" {
p := "Default"
if opts.RemotePort != "" { p = opts.RemotePort }
fmt.Printf("Remote frame dir: %s Port: %s\n", opts.RemoteDir, p)
}
go captureWorker(opts)
go cleanUpLocalFilesWorker(opts)
if opts.RemoteDir != "" {
useRsync = commandIsAvailable("rsync")
go remoteCopyWorker(opts)
go cleanUpRemoteFilesWorker(opts)
}
<-captureWorkerDone
}