-
Notifications
You must be signed in to change notification settings - Fork 0
/
uncaption.go
185 lines (174 loc) · 4.05 KB
/
uncaption.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
package discordbot
import (
"bytes"
"errors"
"image"
"image/color"
"image/gif"
"image/png"
"io"
"net/http"
"os"
"strconv"
"github.com/diamondburned/arikawa/v3/gateway"
ff "samhza.com/ffmpeg"
)
// TODO: split Uncaption into multiple functions
func (bot *Bot) Uncaption(m *gateway.MessageCreateEvent) error {
media, err := bot.findMedia(m.Message)
if err != nil {
return err
}
resp, err := http.Get(media.URL)
if err != nil {
return err
}
done := bot.startWorking(m.ChannelID, m.ID)
defer done()
defer resp.Body.Close()
switch media.Type {
case mediaImage:
im, _, err := image.Decode(resp.Body)
resp.Body.Close()
if err != nil {
return err
}
newMinY, found := detectCaption(im)
if !found {
return errors.New("couldn't find the caption")
}
b := im.Bounds()
b.Min.Y = newMinY
cropped := cropImage(im, b)
r, w := io.Pipe()
defer r.Close()
go func() {
png.Encode(w, cropped)
w.Close()
}()
done()
return bot.sendFile(m.ChannelID, m.ID, "png", r)
case mediaGIF:
dgif, err := gif.DecodeAll(resp.Body)
resp.Body.Close()
if err != nil {
return err
}
newMinY, found := detectCaption(dgif.Image[0])
if !found {
return errors.New("couldn't find the caption")
}
bounds := dgif.Image[0].Rect
bounds.Min.Y = newMinY
for i, img := range dgif.Image {
dgif.Image[i] = img.SubImage(bounds).(*image.Paletted)
dgif.Image[i].Rect.Min.Y = 0
dgif.Image[i].Rect.Max.Y -= newMinY
}
dgif.Config.Height -= newMinY
r, w := io.Pipe()
go func() {
w.CloseWithError(gif.EncodeAll(w, dgif))
}()
done()
return bot.sendFile(m.ChannelID, m.ID, "gif", r)
}
in, err := downloadInput(resp.Body)
resp.Body.Close()
if err != nil {
return err
}
defer os.Remove(in.Name())
defer in.Close()
firstFrame, err := firstVidFrame(in.Name())
if err != nil {
return err
}
newMinY, found := detectCaption(firstFrame)
if !found {
return errors.New("couldn't find the caption")
}
var instream ff.Stream = ff.InputFile{File: in}
probed, err := ff.Probe(in.Name())
if err != nil {
return err
}
var hasAudio bool
for _, stream := range probed.Streams {
if stream.CodecType == ff.CodecTypeAudio {
hasAudio = true
}
}
var v ff.Stream = ff.Video(instream)
b := firstFrame.Bounds()
v = ff.Filter(v, "crop=y="+strconv.Itoa(newMinY)+
":out_h="+strconv.Itoa(b.Max.Y-newMinY))
var outfmt string
streams := []ff.Stream{v}
if media.Type == mediaGIFV {
v = ff.Filter(v, "fps=20")
one, two := ff.Split(v)
palette := ff.PaletteGen(two)
v = ff.PaletteUse(one, palette)
outfmt = "gif"
} else {
outfmt = "mp4"
if hasAudio {
streams = append(streams, ff.Audio(instream))
}
}
out, err := bot.createOutput(m.ID, outfmt)
if err != nil {
return err
}
defer out.Cleanup()
fcmd := new(ff.Cmd)
fcmd.AddFileOutput(out.File, []string{"-y", "-f", outfmt}, streams...)
err = fcmd.Cmd().Run()
if err != nil {
return err
}
done()
return out.Send(bot.Ctx.Client, m.ChannelID)
}
func firstVidFrame(filename string) (image.Image, error) {
var v ff.Stream = ff.Input{Name: filename}
v = ff.Filter(v, "select=eq(n\\,0)")
cmd := new(ff.Cmd)
cmd.AddOutput("-", []string{"-f", "mjpeg"}, v)
out, err := cmd.Cmd().Output()
if err != nil {
return nil, err
}
img, _, err := image.Decode(bytes.NewReader(out))
return img, err
}
func detectCaption(m image.Image) (newMinY int, found bool) {
b := m.Bounds()
for y := b.Min.Y; y < b.Max.Y; y++ {
brightness := color.GrayModel.Convert(m.At(b.Min.X, y)).(color.Gray).Y
if brightness < 248 {
if y == b.Min.Y {
return 0, false
}
return y, true
}
}
return 0, false
}
func cropImage(im image.Image, b image.Rectangle) image.Image {
sub, ok := im.(interface {
SubImage(image.Rectangle) image.Image
})
if ok {
return sub.SubImage(b)
}
return croppedImage{im, b}
}
type croppedImage struct {
image image.Image
bounds image.Rectangle
}
func (c croppedImage) ColorModel() color.Model { return c.image.ColorModel() }
func (c croppedImage) At(x, y int) color.Color { return c.image.At(x, y) }
func (c croppedImage) Bounds() image.Rectangle { return c.bounds }