-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.go
179 lines (154 loc) · 3.77 KB
/
extract.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
package skiptro
import (
"bytes"
"context"
"fmt"
"image"
"image/jpeg"
"os/exec"
"sync"
"time"
"github.com/corona10/goimagehash"
)
var (
soi = []byte{0xff, 0xd8}
eoi = []byte{0xff, 0xd9}
HashDifference HashFunc = goimagehash.DifferenceHash
HashPerception HashFunc = goimagehash.PerceptionHash
HashAverage HashFunc = goimagehash.AverageHash
)
type (
hashResult struct {
hash *goimagehash.ImageHash
err error
index int
}
imageData struct {
index int
bytes []byte
}
HashFunc func(image.Image) (*goimagehash.ImageHash, error)
HashExtractor struct {
HashFunc HashFunc
FPS int
WorkerCount int
ffmpegScale string
}
)
func NewExtractor(f *HashFunc, fps int, workers int) *HashExtractor {
scale := ""
switch f {
case &HashDifference:
scale = ",scale=9:8"
case &HashAverage:
scale = ",scale=8:8"
case &HashPerception:
scale = ",scale=64:64"
}
return &HashExtractor{
HashFunc: *f,
FPS: fps,
WorkerCount: workers,
ffmpegScale: scale,
}
}
// Hashes returns hashed images from a video file
func (h *HashExtractor) Hashes(filename string, at time.Duration, duration time.Duration) ([]*goimagehash.ImageHash, error) {
cmd := exec.Command("ffmpeg",
"-ss", fmt.Sprintf("%.0f", at.Seconds()),
"-i", filename, // Set input file
"-an", // Disable audio stream
"-c:v", "mjpeg", // Set encoder to mjpeg
"-f", "image2pipe", // Set output to image2pipe
"-vf", fmt.Sprintf("fps=%d%s", h.FPS, h.ffmpegScale),
"-pix_fmt", "yuvj422p", // Set a common pixel format for output
"-q", "1",
"-to", fmt.Sprintf("%.0f", duration.Seconds()), // Duration
"pipe:1", // Pipe to file descriptor 1 (stdout)
)
out := bytes.Buffer{}
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return nil, fmt.Errorf("command failed: %w", err)
}
buf := out.Bytes()
var imagesData [][]byte
off := 0
index := 0
for {
if off >= len(buf) {
break
}
tail := buf[off:]
start := bytes.Index(tail, soi)
if start == -1 {
break
}
end := bytes.Index(tail, eoi)
if end == -1 {
break
}
// Account for the two bytes which are searched at the end
end += 2
// Advance the offset after the last image found
off += end
imagesData = append(imagesData, tail[start:end])
index++
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wg := &sync.WaitGroup{}
wg.Add(h.WorkerCount)
workCh := make(chan imageData, len(imagesData))
for i, imgBytes := range imagesData {
workCh <- imageData{index: i, bytes: imgBytes}
}
close(workCh)
resultCh := make(chan hashResult, len(imagesData))
for i := 0; i < h.WorkerCount; i++ {
go h.startWorker(wg, ctx, workCh, resultCh)
}
errCh := make(chan error, h.WorkerCount)
doneCh := make(chan []*goimagehash.ImageHash, 1)
defer close(doneCh)
go func() {
hashes := make([]*goimagehash.ImageHash, len(imagesData))
for res := range resultCh {
if res.err != nil {
errCh <- res.err
cancel()
return
}
hashes[res.index] = res.hash
}
doneCh <- hashes
}()
wg.Wait()
close(resultCh)
// deferred close of errCh so that there are no nil values in the channel
defer close(errCh)
select {
case err := <-errCh:
return nil, fmt.Errorf("error while extracting frames: %w", err)
case hashes := <-doneCh:
return hashes, nil
}
}
func (h *HashExtractor) startWorker(wg *sync.WaitGroup, ctx context.Context, input <-chan imageData, output chan<- hashResult) {
defer wg.Done()
for data := range input {
r := NewReader(ctx, bytes.NewBuffer(data.bytes))
frame, err := jpeg.Decode(r)
if err != nil {
output <- hashResult{err: fmt.Errorf("could not decode image: %w", err)}
break
}
hash, err := h.HashFunc(frame)
output <- hashResult{
hash: hash,
err: err,
index: data.index,
}
}
}