-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathprocessor.go
More file actions
233 lines (222 loc) · 5.93 KB
/
Copy pathprocessor.go
File metadata and controls
233 lines (222 loc) · 5.93 KB
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
package imagorvideo
import (
"context"
"io"
"math"
"strconv"
"strings"
"time"
"github.com/cshum/imagor"
"github.com/cshum/imagor/imagorpath"
"github.com/cshum/imagorvideo/ffmpeg"
"github.com/gabriel-vasile/mimetype"
"go.uber.org/zap"
)
// Processor for imagorvideo that implements imagor.Processor interface
type Processor struct {
Logger *zap.Logger
Debug bool
FallbackImage string
// MaxAnimationFrames caps the frames decoded by the gif filter
MaxAnimationFrames int
}
// NewProcessor creates Processor
func NewProcessor(options ...Option) *Processor {
p := &Processor{
Logger: zap.NewNop(),
MaxAnimationFrames: defaultMaxAnimationFrames,
}
for _, option := range options {
option(p)
}
return p
}
// Startup implements imagor.Processor interface
func (p *Processor) Startup(_ context.Context) error {
ffmpeg.SetLogging(func(level ffmpeg.AVLogLevel, message string) {
message = strings.TrimSuffix(message, "\n")
switch level {
case ffmpeg.AVLogTrace, ffmpeg.AVLogDebug, ffmpeg.AVLogVerbose:
p.Logger.Debug("ffmpeg", zap.String("log", message))
case ffmpeg.AVLogInfo:
p.Logger.Info("ffmpeg", zap.String("log", message))
case ffmpeg.AVLogWarning, ffmpeg.AVLogError, ffmpeg.AVLogFatal, ffmpeg.AVLogPanic:
p.Logger.Warn("ffmpeg", zap.String("log", message))
}
})
if p.Debug {
ffmpeg.SetFFmpegLogLevel(ffmpeg.AVLogDebug)
} else {
ffmpeg.SetFFmpegLogLevel(ffmpeg.AVLogError)
}
return nil
}
// Shutdown implements imagor.Processor interface
func (p *Processor) Shutdown(_ context.Context) error {
return nil
}
// Process implements imagor.Processor interface
func (p *Processor) Process(ctx context.Context, in *imagor.Blob, params imagorpath.Params, load imagor.LoadFunc) (out *imagor.Blob, err error) {
if in == nil {
return in, imagor.ErrForward{Params: params}
}
defer func() {
if err == nil || out != nil {
return
}
if _, ok := err.(imagor.ErrForward); ok {
return
}
err = imagor.NewError(err.Error(), 406)
// fallback image on error
out = imagor.NewBlobFromBytes(transPixel)
if p.FallbackImage != "" {
if o, e := load(p.FallbackImage); e == nil {
out = o
}
}
}()
// Forward camera RAW formats — they are not video/audio.
// CR3 (Canon) is ISO BMFF-based and may be misdetected as video by mimetype.
if in.IsRaw() {
err = imagor.ErrForward{Params: params}
out = in
return
}
var filters imagorpath.Filters
var mime = mimetype.Detect(in.Sniff())
if typ := mime.String(); !strings.HasPrefix(typ, "video/") &&
!strings.HasPrefix(typ, "audio/") {
// forward identical for non video nor audio
err = imagor.ErrForward{Params: params}
out = in
return
}
rs, size, err := in.NewReadSeeker()
if err != nil {
return
}
defer func() {
_ = rs.Close()
}()
if size <= 0 && rs != nil {
// size must be known
if size, err = rs.Seek(0, io.SeekEnd); err != nil {
return
}
if _, err = rs.Seek(0, io.SeekStart); err != nil {
return
}
}
av, err := ffmpeg.LoadAVContext(rs, size)
if err != nil {
return
}
defer av.Close()
meta := av.Metadata()
if params.Meta {
out = imagor.NewBlobFromJsonMarshal(Metadata{
Format: strings.TrimPrefix(mime.Extension(), "."),
ContentType: mime.String(),
Metadata: meta,
})
return
}
bands := 3
var (
anim *animation
start time.Duration
hasFormat bool
)
for _, filter := range params.Filters {
switch filter.Name {
case "format":
hasFormat = true
switch strings.ToLower(filter.Args) {
case "webp", "png", "gif":
switch mime.Extension() {
case ".webm", ".flv", ".mov", ".avi":
bands = 4
}
}
case "gif":
if a, ok := parseAnimation(filter.Args); ok {
anim = &a
}
case "frame":
if ts, e := time.ParseDuration(filter.Args); e == nil {
if err = av.SelectDuration(ts); err != nil {
return
}
} else if f, e := strconv.ParseFloat(filter.Args, 64); e == nil {
if strings.Contains(filter.Args, ".") {
if err = av.SelectPosition(f); err != nil {
return
}
} else if n := int(f); n >= 1 {
if err = av.SelectFrame(n); err != nil {
return
}
}
}
case "seek":
if ts, e := time.ParseDuration(filter.Args); e == nil {
start = ts
if err = av.SeekDuration(ts); err != nil {
return
}
} else if f, e := strconv.ParseFloat(filter.Args, 64); e == nil {
start = time.Duration(float64(meta.Duration) * math.Max(math.Min(f, 1), 0) * float64(time.Millisecond))
if err = av.SeekPosition(f); err != nil {
return
}
}
case "max_frames":
n, _ := strconv.Atoi(filter.Args)
if err = av.ProcessFrames(n); err != nil {
return
}
}
}
switch meta.Orientation {
case 3:
filters = append(filters, imagorpath.Filter{Name: "orient", Args: "180"})
case 6:
filters = append(filters, imagorpath.Filter{Name: "orient", Args: "270"})
case 8:
filters = append(filters, imagorpath.Filter{Name: "orient", Args: "90"})
}
if anim != nil {
if out, err = exportAnimation(av, meta, start, *anim, bands, p.MaxAnimationFrames); err != nil {
out = nil
return
}
if !hasFormat {
// keep the animation as gif unless a format was requested explicitly
filters = append(filters, imagorpath.Filter{Name: "format", Args: "gif"})
}
} else {
buf, e := av.Export(bands)
if e != nil || len(buf) == 0 {
if e == nil {
e = imagor.ErrUnsupportedFormat
}
err = e
return
}
out = imagor.NewBlobFromMemory(buf, meta.Width, meta.Height, bands)
}
if len(filters) > 0 {
params.Filters = append(params.Filters, filters...)
params.Path = imagorpath.GeneratePath(params)
}
err = imagor.ErrForward{Params: params}
return
}
// Metadata imagorvideo metadata
type Metadata struct {
Format string `json:"format"`
ContentType string `json:"content_type"`
*ffmpeg.Metadata
}
var transPixel = []byte("\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x21\xF9\x04\x01\x00\x00\x00\x00\x2C\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3B")