|
1 | 1 | package media
|
2 | 2 |
|
3 |
| -import ffmpeg "github.com/mutablelogic/go-media/sys/ffmpeg61" |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "slices" |
| 6 | + "strings" |
| 7 | + |
| 8 | + // Package imports |
| 9 | + ff "github.com/mutablelogic/go-media/sys/ffmpeg61" |
| 10 | +) |
4 | 11 |
|
5 | 12 | ////////////////////////////////////////////////////////////////////////////
|
6 | 13 | // TYPES
|
7 | 14 |
|
8 | 15 | type manager struct {
|
9 | 16 | }
|
10 | 17 |
|
| 18 | +type inputformat struct { |
| 19 | + ctx *ff.AVInputFormat |
| 20 | + Name string `json:"name"` |
| 21 | + Description string `json:"description" writer:",wrap,width:40"` |
| 22 | + Extensions string `json:"extensions,omitempty"` |
| 23 | + MimeTypes string `json:"mimetypes,omitempty"` |
| 24 | +} |
| 25 | + |
| 26 | +type outputformat struct { |
| 27 | + ctx *ff.AVOutputFormat |
| 28 | + Name string `json:"name"` |
| 29 | + Description string `json:"description" writer:",wrap,width:40"` |
| 30 | + Extensions string `json:"extensions,omitempty"` |
| 31 | + MimeTypes string `json:"mimetypes,omitempty"` |
| 32 | +} |
| 33 | + |
11 | 34 | ////////////////////////////////////////////////////////////////////////////
|
12 | 35 | // LIFECYCLE
|
13 | 36 |
|
14 | 37 | func NewManager() *manager {
|
15 | 38 | return new(manager)
|
16 | 39 | }
|
17 | 40 |
|
| 41 | +func newInputFormat(ctx *ff.AVInputFormat) *inputformat { |
| 42 | + return &inputformat{ |
| 43 | + ctx: ctx, |
| 44 | + Name: ctx.Name(), |
| 45 | + Description: ctx.LongName(), |
| 46 | + Extensions: ctx.Extensions(), |
| 47 | + MimeTypes: ctx.MimeTypes(), |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func newOutputFormat(ctx *ff.AVOutputFormat) *outputformat { |
| 52 | + return &outputformat{ |
| 53 | + ctx: ctx, |
| 54 | + Name: ctx.Name(), |
| 55 | + Description: ctx.LongName(), |
| 56 | + Extensions: ctx.Extensions(), |
| 57 | + MimeTypes: ctx.MimeTypes(), |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +//////////////////////////////////////////////////////////////////////////// |
| 62 | +// STRINGIFY |
| 63 | + |
| 64 | +func (v inputformat) MarshalJSON() ([]byte, error) { |
| 65 | + return json.Marshal(v.ctx) |
| 66 | +} |
| 67 | + |
| 68 | +func (v outputformat) MarshalJSON() ([]byte, error) { |
| 69 | + return json.Marshal(v.ctx) |
| 70 | +} |
| 71 | + |
| 72 | +func (v inputformat) String() string { |
| 73 | + data, _ := json.MarshalIndent(v, "", " ") |
| 74 | + return string(data) |
| 75 | +} |
| 76 | + |
| 77 | +func (v outputformat) String() string { |
| 78 | + data, _ := json.MarshalIndent(v, "", " ") |
| 79 | + return string(data) |
| 80 | +} |
| 81 | + |
18 | 82 | ////////////////////////////////////////////////////////////////////////////
|
19 | 83 | // PUBLIC METHODS
|
20 | 84 |
|
21 |
| -// Return the list of input formats, filtering by name or mimetype |
22 |
| -func (this *manager) InputFormats(mimetype string) []InputFormat { |
| 85 | +// Return the list of matching input formats, optionally filtering by name, |
| 86 | +// extension or mimetype File extensions should be prefixed with a dot, |
| 87 | +// e.g. ".mp4" |
| 88 | +func (manager *manager) InputFormats(filter ...string) []InputFormat { |
23 | 89 | var result []InputFormat
|
| 90 | + |
| 91 | + // Iterate over all input formats |
24 | 92 | var opaque uintptr
|
25 | 93 | for {
|
26 |
| - demuxer := ffmpeg.AVFormat_demuxer_iterate(&opaque) |
| 94 | + demuxer := ff.AVFormat_demuxer_iterate(&opaque) |
27 | 95 | if demuxer == nil {
|
28 | 96 | break
|
29 | 97 | }
|
30 |
| - if this.matchesInput(demuxer, mimetype) { |
31 |
| - result = append(result, demuxer) |
| 98 | + if len(filter) == 0 { |
| 99 | + result = append(result, newInputFormat(demuxer)) |
| 100 | + } else if manager.matchesInput(demuxer, filter...) { |
| 101 | + result = append(result, newInputFormat(demuxer)) |
32 | 102 | }
|
33 | 103 | }
|
34 |
| - return result |
35 | 104 |
|
| 105 | + // Return success |
| 106 | + return result |
36 | 107 | }
|
37 | 108 |
|
38 |
| -// Return the list of output formats, filtering by name or mimetype |
39 |
| -func (this *manager) OutputFormats(name string) []OutputFormat { |
40 |
| - return nil |
| 109 | +// Return the list of matching output formats, optionally filtering by name, |
| 110 | +// extension or mimetype File extensions should be prefixed with a dot, |
| 111 | +// e.g. ".mp4" |
| 112 | +func (manager *manager) OutputFormats(filter ...string) []OutputFormat { |
| 113 | + var result []OutputFormat |
| 114 | + |
| 115 | + // Iterate over all output formats |
| 116 | + var opaque uintptr |
| 117 | + for { |
| 118 | + muxer := ff.AVFormat_muxer_iterate(&opaque) |
| 119 | + if muxer == nil { |
| 120 | + break |
| 121 | + } |
| 122 | + if len(filter) == 0 { |
| 123 | + result = append(result, newOutputFormat(muxer)) |
| 124 | + } else if manager.matchesOutput(muxer, filter...) { |
| 125 | + result = append(result, newOutputFormat(muxer)) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + // Return success |
| 130 | + return result |
41 | 131 | }
|
42 | 132 |
|
43 | 133 | ////////////////////////////////////////////////////////////////////////////
|
44 | 134 | // PRIVATE METHODS
|
45 | 135 |
|
46 |
| -func (this *manager) matchesInput(demuxer *ffmpeg.AVInputFormat, mimetype string) bool { |
47 |
| - return true |
| 136 | +func (this *manager) matchesInput(demuxer *ff.AVInputFormat, mimetype ...string) bool { |
| 137 | + // Match any |
| 138 | + if len(mimetype) == 0 { |
| 139 | + return true |
| 140 | + } |
| 141 | + // Match mimetype |
| 142 | + for _, mimetype := range mimetype { |
| 143 | + mimetype = strings.ToLower(strings.TrimSpace(mimetype)) |
| 144 | + if slices.Contains(strings.Split(demuxer.Name(), ","), mimetype) { |
| 145 | + return true |
| 146 | + } |
| 147 | + if strings.HasPrefix(mimetype, ".") { |
| 148 | + ext := strings.TrimPrefix(mimetype, ".") |
| 149 | + if slices.Contains(strings.Split(demuxer.Extensions(), ","), ext) { |
| 150 | + return true |
| 151 | + } |
| 152 | + } |
| 153 | + if slices.Contains(strings.Split(demuxer.MimeTypes(), ","), mimetype) { |
| 154 | + return true |
| 155 | + } |
| 156 | + } |
| 157 | + // No match |
| 158 | + return false |
| 159 | +} |
| 160 | + |
| 161 | +func (this *manager) matchesOutput(muxer *ff.AVOutputFormat, mimetype ...string) bool { |
| 162 | + // Match any |
| 163 | + if len(mimetype) == 0 { |
| 164 | + return true |
| 165 | + } |
| 166 | + // Match mimetype |
| 167 | + for _, mimetype := range mimetype { |
| 168 | + mimetype = strings.ToLower(strings.TrimSpace(mimetype)) |
| 169 | + if slices.Contains(strings.Split(muxer.Name(), ","), mimetype) { |
| 170 | + return true |
| 171 | + } |
| 172 | + if strings.HasPrefix(mimetype, ".") { |
| 173 | + ext := strings.TrimPrefix(mimetype, ".") |
| 174 | + if slices.Contains(strings.Split(muxer.Extensions(), ","), ext) { |
| 175 | + return true |
| 176 | + } |
| 177 | + } |
| 178 | + if slices.Contains(strings.Split(muxer.MimeTypes(), ","), mimetype) { |
| 179 | + return true |
| 180 | + } |
| 181 | + } |
| 182 | + // No match |
| 183 | + return false |
48 | 184 | }
|
0 commit comments