/
mediaattrs.go
153 lines (132 loc) · 3.23 KB
/
mediaattrs.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
package file
import (
"context"
"encoding/json"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io"
_ "golang.org/x/image/bmp"
_ "golang.org/x/image/tiff"
_ "golang.org/x/image/webp"
"github.com/koud-fi/pkg/blob"
"github.com/koud-fi/pkg/file/format/raw"
"github.com/koud-fi/pkg/shell"
)
type MediaAttributes struct {
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
Duration float64 `json:"duration,omitempty"`
HasAudio bool `json:"hasAudio,omitempty"`
FrameCount int `json:"frameCount,omitempty"`
}
func (ma MediaAttributes) AspectRatio() float64 {
if ma.Width <= 0 || ma.Height <= 0 {
return 1.0
}
return float64(ma.Width) / float64(ma.Height)
}
func (ma MediaAttributes) Megapixels() float64 {
return float64(ma.Width) * float64(ma.Height) / 1_000_000
}
func MediaAttrs() Option {
return func(a *Attributes, b blob.Blob, contentType string) error {
switch contentType {
case "image/jpeg", "image/png", "image/webp", "image/bmp", "image/tiff":
return resolveImageAttrs(&a.MediaAttributes, b)
case "image/gif":
return resolveGIFAttrs(&a.MediaAttributes, b)
case "video/mp4", "video/webm":
return resolveVideoAttrs(&a.MediaAttributes, b)
// TODO: common audio formats
case raw.RAFMime:
return resolveRAFAttrs(&a.MediaAttributes, b)
}
// TODO: more raw image formats
return nil
}
}
func resolveImageAttrs(a *MediaAttributes, b blob.Blob) error {
return blob.Use(b, func(r io.Reader) error {
c, _, err := image.DecodeConfig(r)
if err != nil {
switch err.(type) {
case jpeg.FormatError, png.FormatError:
return nil
}
return err
}
a.Width = c.Width
a.Height = c.Height
return nil
})
}
func resolveGIFAttrs(a *MediaAttributes, b blob.Blob) error {
return blob.Use(b, func(r io.Reader) error {
g, err := gif.DecodeAll(r)
if err != nil {
return err
}
if len(g.Image) == 0 {
return nil
}
for _, delay := range g.Delay {
a.Duration += float64(delay) / 100.0
}
a.Width = g.Image[0].Bounds().Dx()
a.Height = g.Image[0].Bounds().Dy()
a.FrameCount = len(g.Image)
return nil
})
}
func resolveRAFAttrs(a *MediaAttributes, b blob.Blob) error {
raf, err := raw.DecodeRAF(b)
if err != nil {
return err
}
a.Width = raf.JPEGConfig.Width
a.Height = raf.JPEGConfig.Height
return nil
}
func resolveVideoAttrs(a *MediaAttributes, b blob.Blob) error {
// TODO: native implementation
var info ffprobeInfo
if err := blob.Unmarshal(json.Unmarshal, shell.Run(context.TODO(), "ffprobe",
"-i", "-", b,
"-v", "fatal",
"-of", "json",
"-show_format", "-show_streams",
), &info); err != nil {
return err
}
vstream := info.findStream("video")
if vstream == nil {
return nil
}
a.Width = vstream.Width
a.Height = vstream.Height
a.Duration = info.Format.Duration
a.HasAudio = info.findStream("audio") != nil
return nil
}
type ffprobeInfo struct {
Streams []ffprobeSteam
Format ffprobeFormat
}
func (t ffprobeInfo) findStream(codecType string) *ffprobeSteam {
for _, s := range t.Streams {
if s.CodecType == codecType {
return &s
}
}
return nil
}
type ffprobeSteam struct {
CodecType string `json:"codec_type"`
Width int
Height int
}
type ffprobeFormat struct {
Duration float64 `json:",string"`
}