-
Notifications
You must be signed in to change notification settings - Fork 0
/
grab.go
175 lines (152 loc) · 3.42 KB
/
grab.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
package main
import (
"fmt"
"log"
"math"
"os"
"os/exec"
"strconv"
"sync"
"time"
)
type Sender interface {
Send(fn string, duration int, t time.Time)
}
type WorkInfo struct {
Dir string
Filename string
Time time.Time
}
// Grabber defines a procedure to grab live video
type Grabber struct {
Segment int // Record time for single video
Resolution string // 640x480... etc
SPF int // seconds per frame
Format string
Device string
process *exec.Cmd
*sync.Mutex
*log.Logger
}
func (g *Grabber) Interrupt() {
cmd := g.process
if cmd != nil {
if err := cmd.Process.Kill(); err != nil {
g.Printf("Cannot kill grabber process: %s")
}
}
}
// Grab image from web cam
func (g *Grabber) Grab(dir string) (ret WorkInfo, err error) {
g.Lock()
defer g.Unlock()
t := time.Now()
seg := strconv.Itoa(g.Segment)
fn := fmt.Sprintf("%s/%%0%.0fd.png", dir, math.Ceil(math.Log10(float64(g.Segment)/float64(g.SPF))))
g.Printf("[GRAB] Grabbing to dir %s ...", fn)
frameRate := fmt.Sprintf("1/%d", g.SPF)
proc := exec.Command(
"ffmpeg",
"-t", seg,
"-framerate", frameRate,
"-f", g.Format,
"-s", g.Resolution,
"-i", g.Device,
fn,
)
if f, err := os.Create(dir + "/grab.out"); err == nil {
proc.Stdout = f
proc.Stderr = f
defer f.Close()
}
g.process = proc
err = proc.Run()
g.process = nil
ret = WorkInfo{
Dir: dir,
Filename: fn,
Time: t,
}
return
}
type Encoder struct {
Segment int // Record time for single video
Resolution string // 640x480... etc
SPF int // seconds per frame
Senders []Sender
Queue chan WorkInfo
process *exec.Cmd
*log.Logger
}
func (e *Encoder) Interrupt() {
close(e.Queue)
for range e.Queue {
}
cmd := e.process
if cmd != nil {
if err := cmd.Process.Kill(); err != nil {
e.Printf("Cannot kill encoder process: %s")
}
}
}
// Encode grabbed image to video
func (e *Encoder) Run() {
for work := range e.Queue {
fn, dir, t := work.Filename, work.Dir, work.Time
if fn == "" || dir == "" {
e.Print("[ENC ] Got empty workinfo")
continue
}
if _, err := os.Stat(fmt.Sprintf(fn, 1)); err != nil {
e.Printf("[ENC ] No grabbed data found in %s ...", dir)
continue
}
e.Printf("[ENC ] Encoding from %s ...", fn)
// encode to mp4. ref: http://rodrigopolo.com/ffmpeg/cheats.php
proc := exec.Command(
"ffmpeg",
"-i", fn,
"-s", e.Resolution,
"-r", "24000/1001",
"-profile:v", "main",
"-level", "4.0",
"-pix_fmt", "yuv420p",
"-c:v", "libx264",
"-c:a", "libfaac",
"-ac", "2",
"-ar", "48000",
"-ab", "192k",
dir+"/ant.mp4",
)
f, err := os.Create(dir + "/encode.out")
if err != nil {
e.Printf("[ENC ] Cannot create log file for subprocess: %s", err)
continue
}
proc.Stdout = f
proc.Stderr = f
defer f.Close()
e.process = proc
if err = proc.Run(); err != nil {
e.Printf("[ENC ] Subprocess returns error: %s", err)
continue
}
e.process = nil
totalFrames := e.Segment / e.SPF
duration := float64(totalFrames) / (24000.0 / 1001.0)
e.Send(dir, int(duration), t)
e.Cleanup(work)
}
e.Print("[ENC ] No more works.")
}
// Cleanup work temp
func (e *Encoder) Cleanup(work WorkInfo) {
e.Printf("[ENC ] Cleaning up %s ...", work.Dir)
os.RemoveAll(work.Dir)
}
// Send video via registered senders
func (e *Encoder) Send(dir string, duration int, t time.Time) {
for _, sender := range e.Senders {
sender.Send(dir+"/ant.mp4", duration, t)
}
}