-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcapture.go
More file actions
174 lines (150 loc) · 4.28 KB
/
capture.go
File metadata and controls
174 lines (150 loc) · 4.28 KB
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
package runal
import (
"fmt"
"image"
"image/color/palette"
"image/draw"
"image/gif"
"image/png"
"os"
"github.com/charmbracelet/log"
ansitoimage "github.com/xaviergodart/go-ansi-to-image"
)
// SaveCanvasToPNG exports the canvas to a png image file.
func (c *Canvas) SaveCanvasToPNG(filename string) {
c.save = true
c.saveFilename = filename
c.Redraw()
}
// SaveCanvasToGIF exports the canvas to an animated gif
// for a given duration (in seconds).
func (c *Canvas) SaveCanvasToGIF(filename string, duration int) {
if c.frames != nil {
return
}
c.videoFormat = videoFormatGif
totalFrames := duration * c.fps
c.frames = make([]image.Image, 0, totalFrames)
c.saveFilename = filename
}
// SaveCanvasToMP4 exports the canvas to a mp4 (h264) video
// for a given duration (in seconds).
// Depends on ffmpeg.
func (c *Canvas) SaveCanvasToMP4(filename string, duration int) {
if !checkFFMPEG() {
log.Error("Can't use SaveCanvasToMP4(). ffmpeg is not installed.")
c.DisableRendering()
}
if c.frames != nil {
return
}
c.videoFormat = videoFormatMp4
totalFrames := duration * c.fps
c.frames = make([]image.Image, 0, totalFrames)
c.saveFilename = filename
}
// SavedCanvasFont sets a custom font (tff) file used for rendering text characters
// in exported images generated via SaveCanvasTo...().
func (c *Canvas) SavedCanvasFont(filename string) {
file, err := os.ReadFile(filename)
if err != nil {
return
}
config := c.capture.Config()
config.MonoRegularFontBytes = file
c.capture, _ = ansitoimage.NewConverter(config)
}
// SavedCanvasFontSize sets the font size used for rendering text characters
// in exported images generated via SaveCanvas().
func (c *Canvas) SavedCanvasFontSize(size int) {
config := c.capture.Config()
config.MonoRegularFontPoints = float64(size)
c.capture, _ = ansitoimage.NewConverter(config)
}
func newCapture(width, height int) *ansitoimage.Converter {
imageCapture, _ := ansitoimage.NewConverter(newCaptureConfig(width, height))
return imageCapture
}
func newCaptureConfig(width, height int) ansitoimage.Config {
captureConfig := ansitoimage.DefaultConfig
captureConfig.Padding = 0
captureConfig.PageCols = width - 2
captureConfig.PageRows = height
return captureConfig
}
func (c *Canvas) captureResize(width, height int) {
config := c.capture.Config()
config.PageCols = width - 2
config.PageRows = height
c.capture, _ = ansitoimage.NewConverter(config)
}
func (c *Canvas) generateFrame(frame string) {
err := c.capture.Parse(frame)
if err != nil {
log.Fatalf("can't generate frame: %v", err)
}
}
func (c *Canvas) exportCanvasToPNG(frame string) {
fmt.Println("Saving png...")
c.generateFrame(frame)
img, _ := c.capture.ToPNG()
err := os.WriteFile(c.saveFilename, img, 0o644)
if err != nil {
log.Fatal("can't create png file: %v", err)
}
}
func (c *Canvas) exportFramesToGIF() error {
outGif := &gif.GIF{}
delay := 100 / c.fps
for _, img := range c.frames {
bounds := img.Bounds()
paletted := image.NewPaletted(bounds, palette.Plan9)
draw.FloydSteinberg.Draw(paletted, bounds, img, image.Point{})
outGif.Image = append(outGif.Image, paletted)
outGif.Delay = append(outGif.Delay, delay)
}
file, err := os.Create(c.saveFilename)
if err != nil {
return err
}
defer file.Close()
return gif.EncodeAll(file, outGif)
}
func (c *Canvas) exportFramesToMP4() error {
dir := randomDir()
defer os.RemoveAll(dir)
for i, img := range c.frames {
f, err := os.Create(fmt.Sprintf("%s/frame_%d.png", dir, i))
if err != nil {
log.Fatal("can't create frame file: %v", err)
}
defer f.Close()
if err := png.Encode(f, img); err != nil {
log.Fatal("can't encode frame: %v", err)
}
f.Close()
}
return framesToMP4Videos(c.fps, fmt.Sprintf("%s/frame_%%d.png", dir), c.saveFilename)
}
func (c *Canvas) recordFrame(output string) {
if len(c.frames) >= cap(c.frames) {
var err error
switch c.videoFormat {
case videoFormatGif:
fmt.Println("Saving GIF...")
err = c.exportFramesToGIF()
case videoFormatMp4:
fmt.Println("Saving MP4...")
err = c.exportFramesToMP4()
}
if err != nil {
log.Fatal("can't export frames: %v", err)
}
c.frames = nil
return
}
c.generateFrame(output)
frame := image.NewRGBA(c.capture.Image().Bounds())
copy(frame.Pix, c.capture.Image().(*image.RGBA).Pix)
c.frames = append(c.frames, frame)
}