generated from edgexfoundry-holding/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 28
/
ffmpeg.go
149 lines (132 loc) · 3.48 KB
/
ffmpeg.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
package driver
import (
"fmt"
"reflect"
"strings"
"github.com/edgexfoundry/go-mod-core-contracts/v2/errors"
)
type FFmpeg struct {
inputOptions []string
outputOptions []string
}
func (f FFmpeg) ObtainOutputFrames(value string) []string {
if len(value) != 0 {
return []string{FFmpegFrames, value}
}
return nil
}
func (f FFmpeg) ObtainInputFps(value string) []string {
if len(value) != 0 {
return []string{FFmpegFps, value}
}
return nil
}
func (f FFmpeg) ObtainOutputFps(value string) []string {
if len(value) != 0 {
return []string{FFmpegFps, value}
}
return nil
}
func (f FFmpeg) ObtainInputImageSize(value string) []string {
if len(value) != 0 {
return []string{FFmpegSize, value}
}
return nil
}
func (f FFmpeg) ObtainOutputImageSize(value string) []string {
if len(value) != 0 {
return []string{FFmpegSize, value}
}
return nil
}
func (f FFmpeg) ObtainOutputAspect(value string) []string {
if len(value) != 0 {
return []string{FFmpegAspect, value}
}
return nil
}
func (f FFmpeg) ObtainOutputVideoQuality(value string) []string {
if len(value) != 0 {
return []string{FFmpegQScale, value}
}
return nil
}
func (f FFmpeg) ObtainOutputVideoCodec(value string) []string {
if len(value) != 0 {
return []string{FFmpegVCodec, value}
}
return nil
}
func (f FFmpeg) ObtainInputPixelFormat(value string) []string {
if len(value) != 0 {
return []string{FFmpegInputFormat, value}
}
return nil
}
func (f *FFmpeg) setOptions(name, val string) bool {
opt := reflect.ValueOf(f).MethodByName(fmt.Sprintf("Obtain%s", name))
if (opt != reflect.Value{}) {
result := opt.Call([]reflect.Value{reflect.ValueOf(val)})
if val, ok := result[0].Interface().([]string); ok {
if strings.HasPrefix(name, PrefixInput) {
f.inputOptions = append(f.inputOptions, val...)
} else if strings.HasPrefix(name, PrefixOutput) {
f.outputOptions = append(f.outputOptions, val...)
} else {
return false
}
return true
}
}
return false
}
func setupFFmpegOptions(dev *Device, opts interface{}, attr map[string]interface{}) errors.EdgeX {
options, ok := opts.(map[string]interface{})
if !ok {
return errors.NewCommonEdgeX(errors.KindContractInvalid,
"failed to parse request body", nil)
}
ffmpeg := &FFmpeg{}
// obtain FFmpeg options defined in request body
for optName, value := range options {
optVal, ok := value.(string)
if !ok {
return errors.NewCommonEdgeX(errors.KindContractInvalid,
"argument should be a string", nil)
}
if ffmpeg.setOptions(optName, optVal) {
dev.updateFFmpegOptions(optName, optVal)
continue
}
return errors.NewCommonEdgeX(errors.KindContractInvalid,
fmt.Sprintf("unsupported option: %s", optName), nil)
}
// obtain default FFmpeg options defined in resource attributes
for name, value := range attr {
if name == Command {
continue
}
optName := strings.ReplaceAll(name, "default", "")
if _, ok := options[optName]; ok {
continue
}
optVal, ok := value.(string)
if !ok {
return errors.NewCommonEdgeX(errors.KindContractInvalid,
"argument should be a string", nil)
}
if ffmpeg.setOptions(optName, optVal) {
dev.updateFFmpegOptions(optName, optVal)
continue
}
return errors.NewCommonEdgeX(errors.KindContractInvalid,
fmt.Sprintf("unsupported option: %s", optName), nil)
}
if len(ffmpeg.inputOptions) > 0 {
dev.transcoder.MediaFile().SetRawInputArgs(ffmpeg.inputOptions)
}
if len(ffmpeg.outputOptions) > 0 {
dev.transcoder.MediaFile().SetRawOutputArgs(ffmpeg.outputOptions)
}
return nil
}