-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
379 lines (321 loc) · 8.51 KB
/
config.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
package recoder
import (
"encoding/json"
"fmt"
"image"
"strings"
"github.com/goccy/go-yaml"
)
type EncoderConfig struct {
OutputAudioTracks []AudioTrackConfig
OutputVideoTracks []VideoTrackConfig
}
type VideoTrackConfig struct {
InputID uint
InputTrackIDs []int
EncodeVideoConfig
}
type EncodeVideoConfig struct {
FlipV bool `json:"flip_v,omitempty" yaml:"flip_v,omitempty"`
FlipH bool `json:"flip_h,omitempty" yaml:"flip_h,omitempty"`
Crop *image.Rectangle `json:"crop,omitempty" yaml:"crop,omitempty"`
Scale *image.Point `json:"scale,omitempty" yaml:"scale,omitempty"`
Codec VideoCodec `json:"codec,omitempty" yaml:"codec,omitempty"`
Quality VideoQuality `json:"quality,omitempty" yaml:"quality,omitempty"`
}
func (c *EncodeVideoConfig) UnmarshalJSON(b []byte) (_err error) {
c.Quality = videoQualitySerializable{}
err := json.Unmarshal(b, c)
if err != nil {
return fmt.Errorf("unable to un-JSON-ize: %w", err)
}
if c.Quality != nil {
c.Quality, err = c.Quality.(videoQualitySerializable).Convert()
if err != nil {
return fmt.Errorf("unable to convert the 'quality' field: %w", err)
}
}
return nil
}
func (c *EncodeVideoConfig) UnmarshalYAML(b []byte) (_err error) {
c.Quality = videoQualitySerializable{}
err := yaml.Unmarshal(b, c)
if err != nil {
return fmt.Errorf("unable to un-JSON-ize: %w", err)
}
if c.Quality != nil {
c.Quality, err = c.Quality.(videoQualitySerializable).Convert()
if err != nil {
return fmt.Errorf("unable to convert the 'quality' field: %w", err)
}
}
return nil
}
type AudioTrackConfig struct {
InputID uint
InputTrackIDs []int
EncodeAudioConfig
}
type EncodeAudioConfig struct {
Codec AudioCodec `json:"codec,omitempty" yaml:"codec,omitempty"`
Quality AudioQuality `json:"quality,omitempty" yaml:"quality,omitempty"`
}
func (c *EncodeAudioConfig) UnmarshalJSON(b []byte) (_err error) {
c.Quality = audioQualitySerializable{}
err := json.Unmarshal(b, c)
if err != nil {
return fmt.Errorf("unable to un-JSON-ize: %w", err)
}
if c.Quality != nil {
c.Quality, err = c.Quality.(audioQualitySerializable).Convert()
if err != nil {
return fmt.Errorf("unable to convert the 'quality' field: %w", err)
}
}
return nil
}
func (c *EncodeAudioConfig) UnmarshalYAML(b []byte) (_err error) {
c.Quality = audioQualitySerializable{}
err := yaml.Unmarshal(b, c)
if err != nil {
return fmt.Errorf("unable to un-JSON-ize: %w", err)
}
if c.Quality != nil {
c.Quality, err = c.Quality.(audioQualitySerializable).Convert()
if err != nil {
return fmt.Errorf("unable to convert the 'quality' field: %w", err)
}
}
return nil
}
type AudioQuality interface {
audioQuality()
typeName() string
setValues(vq audioQualitySerializable) error
}
type AudioQualityConstantBitrate uint
func (AudioQualityConstantBitrate) typeName() string {
return "constant_bitrate"
}
func (AudioQualityConstantBitrate) audioQuality() {}
func (aq AudioQualityConstantBitrate) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]any{
"type": aq.typeName(),
"bitrate": uint(aq),
})
}
func (aq *AudioQualityConstantBitrate) setValues(in audioQualitySerializable) error {
bitrate, ok := in["bitrate"].(float64)
if !ok {
return fmt.Errorf("have not found float64 value using key 'bitrate' in %#+v", in)
}
*aq = AudioQualityConstantBitrate(bitrate)
return nil
}
type audioQualitySerializable map[string]any
func (audioQualitySerializable) audioQuality() {}
func (aq audioQualitySerializable) typeName() string {
result, _ := aq["type"].(string)
return result
}
func (aq audioQualitySerializable) setValues(in audioQualitySerializable) error {
for k := range aq {
delete(aq, k)
}
for k, v := range in {
aq[k] = v
}
return nil
}
func (aq audioQualitySerializable) Convert() (AudioQuality, error) {
typeName, ok := aq["type"].(string)
if !ok {
return nil, fmt.Errorf("field 'type' is not set")
}
var r AudioQuality
for _, sample := range []AudioQuality{
ptr(AudioQualityConstantBitrate(0)),
} {
if sample.typeName() == typeName {
r = sample
break
}
}
if r == nil {
return nil, fmt.Errorf("unknown type '%s'", typeName)
}
if err := r.setValues(aq); err != nil {
return nil, fmt.Errorf("unable to convert the value: %w", err)
}
return r, nil
}
type AudioCodec uint
const (
AudioCodecUndefined = AudioCodec(iota)
AudioCodecCopy
AudioCodecAAC
AudioCodecVorbis
AudioCodecOpus
EndOfAudioCodec
)
func (ac *AudioCodec) String() string {
if ac == nil {
return "null"
}
switch *ac {
case AudioCodecUndefined:
return "<undefined>"
case AudioCodecCopy:
return "<copy>"
case AudioCodecAAC:
return "aac"
case AudioCodecVorbis:
return "vorbis"
case AudioCodecOpus:
return "opus"
}
return fmt.Sprintf("unexpected_audio_codec_id_%d", uint(*ac))
}
func (ac AudioCodec) MarshalJSON() ([]byte, error) {
return []byte(`"` + ac.String() + `"`), nil
}
func (ac *AudioCodec) UnmarshalJSON(b []byte) error {
if ac == nil {
return fmt.Errorf("AudioCodec is nil")
}
s := strings.ToLower(strings.Trim(string(b), `"`))
for cmp := AudioCodecUndefined; cmp < EndOfAudioCodec; cmp++ {
if cmp.String() == s {
*ac = cmp
return nil
}
}
return fmt.Errorf("unknown value of the AudioCodec: '%s'", s)
}
type VideoQuality interface {
videoQuality()
typeName() string
setValues(vq videoQualitySerializable) error
}
type VideoQualityConstantBitrate uint
func (VideoQualityConstantBitrate) typeName() string {
return "constant_bitrate"
}
func (VideoQualityConstantBitrate) videoQuality() {}
func (vq VideoQualityConstantBitrate) MarshalJSON() ([]byte, error) {
return json.Marshal(videoQualitySerializable{
"type": vq.typeName(),
"bitrate": uint(vq),
})
}
func (vq *VideoQualityConstantBitrate) setValues(in videoQualitySerializable) error {
bitrate, ok := in["bitrate"].(float64)
if !ok {
return fmt.Errorf("have not found float64 value using key 'bitrate' in %#+v", in)
}
*vq = VideoQualityConstantBitrate(bitrate)
return nil
}
type VideoQualityConstantQuality uint8
func (VideoQualityConstantQuality) typeName() string {
return "constant_quality"
}
func (VideoQualityConstantQuality) videoQuality() {}
func (vq VideoQualityConstantQuality) MarshalJSON() ([]byte, error) {
return json.Marshal(videoQualitySerializable{
"type": vq.typeName(),
"quality": uint(vq),
})
}
func (vq *VideoQualityConstantQuality) setValues(in videoQualitySerializable) error {
bitrate, ok := in["quality"].(float64)
if !ok {
return fmt.Errorf("have not found float64 value using key 'quality' in %#+v", in)
}
*vq = VideoQualityConstantQuality(bitrate)
return nil
}
type videoQualitySerializable map[string]any
func (videoQualitySerializable) videoQuality() {}
func (vq videoQualitySerializable) typeName() string {
result, _ := vq["type"].(string)
return result
}
func (vq videoQualitySerializable) setValues(in videoQualitySerializable) error {
for k := range vq {
delete(vq, k)
}
for k, v := range in {
vq[k] = v
}
return nil
}
func (vq videoQualitySerializable) Convert() (VideoQuality, error) {
typeName, ok := vq["type"].(string)
if !ok {
return nil, fmt.Errorf("field 'type' is not set")
}
var r VideoQuality
for _, sample := range []VideoQuality{
ptr(VideoQualityConstantBitrate(0)),
ptr(VideoQualityConstantQuality(0)),
} {
if sample.typeName() == typeName {
r = sample
break
}
}
if r == nil {
return nil, fmt.Errorf("unknown type '%s'", typeName)
}
if err := r.setValues(vq); err != nil {
return nil, fmt.Errorf("unable to convert the value: %w", err)
}
return r, nil
}
func ptr[T any](in T) *T {
return &in
}
type VideoCodec uint
const (
VideoCodecUndefined = VideoCodec(iota)
VideoCodecCopy
VideoCodecH264
VideoCodecHEVC
VideoCodecAV1
EndOfVideoCodec
)
func (vc *VideoCodec) String() string {
if vc == nil {
return "null"
}
switch *vc {
case VideoCodecUndefined:
return "<undefined>"
case VideoCodecCopy:
return "<copy>"
case VideoCodecH264:
return "h264"
case VideoCodecHEVC:
return "hevc"
case VideoCodecAV1:
return "av1"
}
return fmt.Sprintf("unexpected_video_codec_id_%d", uint(*vc))
}
func (vc VideoCodec) MarshalJSON() ([]byte, error) {
return []byte(`"` + vc.String() + `"`), nil
}
func (vc *VideoCodec) UnmarshalJSON(b []byte) error {
if vc == nil {
return fmt.Errorf("VideoCodec is nil")
}
s := strings.ToLower(strings.Trim(string(b), `"`))
for cmp := VideoCodecUndefined; cmp < EndOfVideoCodec; cmp++ {
if cmp.String() == s {
*vc = cmp
return nil
}
}
return fmt.Errorf("unknown value of the VideoCodec: '%s'", s)
}