forked from TommyLeng/go-rod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page_screen_cast.go
257 lines (217 loc) · 6.7 KB
/
page_screen_cast.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package rod
import (
"fmt"
"os"
"os/exec"
"sort"
"strconv"
"time"
"github.com/go-rod/rod/lib/proto"
"github.com/icza/mjpeg"
)
//MaxVideoFrame prevent video non stop recording, memory may not enough
//if fps = 50, 20000 max frame mean you can capture 400s second video
const MaxVideoFrame = 20000
//VideoFrame store the data from screen cast event
type VideoFrame struct {
Data []byte
Timestamp time.Time
DurationInSecond float64
AccumDurationInSecond float64
FrameCnt float64
FrameCntRemaining float64
}
//ScreenCastRecordAvi listen PageScreenCastFrame and convert it directly into AVI Movie
func (p *Page) ScreenCastRecordAvi(videoAVIPath string, videoFrames *[]VideoFrame, fps int) (*mjpeg.AviWriter, error) {
browserBound, err := p.GetWindow()
if err != nil {
return nil, err
}
aviWriter, err := mjpeg.New(videoAVIPath, int32(*browserBound.Width), int32(*browserBound.Height), int32(fps))
if err != nil {
return nil, err
}
go p.EachEvent(func(e *proto.PageScreencastFrame) {
if len(*videoFrames) >= MaxVideoFrame {
fmt.Println("Max video frames reach")
return
}
err := proto.PageScreencastFrameAck{
SessionID: e.SessionID,
}.Call(p)
if err != nil {
fmt.Println("ScreencastFrameAck err:", err)
}
*videoFrames = append(*videoFrames, VideoFrame{
Data: e.Data,
Timestamp: e.Metadata.Timestamp.Time(),
})
})()
return &aviWriter, nil
}
//ScreenCastStart start screen cast event for video recording
func (p *Page) ScreenCastStart(JPEGQuality int) error {
everyNthFrame := 1
return proto.PageStartScreencast{
Format: proto.PageStartScreencastFormatJpeg,
Quality: &JPEGQuality,
EveryNthFrame: &everyNthFrame,
}.Call(p)
}
//ScreenCastStopAvi stop screen cast event and save videoframes data in an avi video file
func (p *Page) ScreenCastStopAvi(aviWriter *mjpeg.AviWriter, videoFrames *[]VideoFrame, fps int) error {
err := proto.PageStopScreencast{}.Call(p)
if err != nil {
return err
}
vfs := *videoFrames
sort.Slice(vfs, func(i int, y int) bool {
return vfs[i].Timestamp.Before(vfs[y].Timestamp)
})
//since screen cast event will not trigger if the page didn't change
//So I need to append a stop frame so that it can copy the last frame data to fill the time
vfs = append(vfs, VideoFrame{
Timestamp: time.Now(),
})
//screen cast frames may not has the same fps, so convert to our fps
for i, vf := range vfs {
//fmt.Printf("frame %d, data %d, timestamp %v\n", i, len(vf.Data), vf.Timestamp)
if i > 0 {
dur := float64(vf.Timestamp.Sub(vfs[i-1].Timestamp).Nanoseconds())/float64(time.Second) + vfs[i-1].AccumDurationInSecond
fc := (dur * float64(fps)) + vfs[i-1].FrameCntRemaining
fci := float64(int64(fc))
vfs[i-1].DurationInSecond = dur
vfs[i-1].FrameCnt = fci
// if frame count = 0, save the duration to current frame's AccumDurationInSecond
if fci == float64(0) {
vfs[i].AccumDurationInSecond += dur
continue
}
fcr := fc - fci
// save the remaining frame count portion to current frame
if fcr > 0 {
vfs[i].FrameCntRemaining += fcr
}
}
}
total := 0
aw := *aviWriter
for _, vf := range vfs {
//fmt.Printf("frame %d, data %d, duration %v,\t\tdurationAcc %v,\t\tframeCnt %v\t\tframeCntR %v\n", j, len(vf.Data), vf.DurationInSecond, vf.AccumDurationInSecond, vf.FrameCnt, vf.FrameCntRemaining)
if vf.FrameCnt > 0 {
for i := int64(0); i < int64(vf.FrameCnt); i++ {
err = aw.AddFrame(vf.Data)
if err != nil {
return err
}
total++
}
}
}
err = aw.Close()
if err != nil {
return err
}
fmt.Println("totalFrameCnt: ", total)
return nil
}
//ScreenCastRecordMp4 listen PageScreenCastFrame and convert it directly into MP4 using ffmpeg
func (p *Page) ScreenCastRecordMp4(videoFrames *[]VideoFrame) error {
go p.EachEvent(func(e *proto.PageScreencastFrame) {
if len(*videoFrames) >= MaxVideoFrame {
fmt.Println("Max video frames reach")
return
}
err := proto.PageScreencastFrameAck{
SessionID: e.SessionID,
}.Call(p)
if err != nil {
fmt.Println("ScreencastFrameAck err:", err)
}
*videoFrames = append(*videoFrames, VideoFrame{
Data: e.Data,
Timestamp: e.Metadata.Timestamp.Time(),
})
})()
return nil
}
//ScreenCastStopMp4 stop screen cast event and use ffmpeg to create mp4 from videoFrame data
func (p *Page) ScreenCastStopMp4(videoFrames *[]VideoFrame, outputFile string, fps int) error {
err := proto.PageStopScreencast{}.Call(p)
if err != nil {
return err
}
vfs := *videoFrames
sort.Slice(vfs, func(i int, y int) bool {
return vfs[i].Timestamp.Before(vfs[y].Timestamp)
})
//since screen cast event will not trigger if the page didn't change
//So I need to append a stop frame so that it can copy the last frame data to fill the time
vfs = append(vfs, VideoFrame{
Timestamp: time.Now(),
})
//screen cast frames may not has the same fps, so convert to our fps
for i, vf := range vfs {
if i > 0 {
dur := float64(vf.Timestamp.Sub(vfs[i-1].Timestamp).Nanoseconds())/float64(time.Second) + vfs[i-1].AccumDurationInSecond
fc := (dur * float64(fps)) + vfs[i-1].FrameCntRemaining
fci := float64(int64(fc))
vfs[i-1].DurationInSecond = dur
vfs[i-1].FrameCnt = fci
// if frame count = 0, save the duration to current frame's AccumDurationInSecond
if fci == float64(0) {
vfs[i].AccumDurationInSecond += dur
continue
}
fcr := fc - fci
// save the remaining frame count portion to current frame
if fcr > 0 {
vfs[i].FrameCntRemaining += fcr
}
}
}
//cat $(find . -maxdepth 1 -name '*.png' -print | sort | tail -10) | ffmpeg -framerate 25 -i - -vf format=yuv420p -movflags +faststart output.mp4
cmd := exec.Command("ffmpeg",
"-y", // Yes to all
"-f", "image2pipe",
"-r", strconv.Itoa(fps),
"-i", "pipe:0", // take stdin as input
"-an",
"-vf", "format=yuv420p",
"-vsync", "1",
"-movflags", "+faststart",
outputFile, // output
)
cmd.Stderr = os.Stderr // bind log stream to stderr
stdin, err := cmd.StdinPipe() // Open stdin pip
if err != nil {
return err
}
err = cmd.Start() // Start a process on another goroutine
if err != nil {
return err
}
total := 0
for _, vf := range vfs {
//fmt.Printf("frame %d, data %d, duration %v,\t\tdurationAcc %v,\t\tframeCnt %v\t\tframeCntR %v\n", j, len(vf.Data), vf.DurationInSecond, vf.AccumDurationInSecond, vf.FrameCnt, vf.FrameCntRemaining)
if vf.FrameCnt > 0 {
for i := int64(0); i < int64(vf.FrameCnt); i++ {
_, err = stdin.Write(vf.Data)
if err != nil {
return err
}
total++
}
}
}
fmt.Printf("totalFrameCnt: %v\n\n", total)
err = stdin.Close()
if err != nil {
return err
}
err = cmd.Wait() // wait until ffmpeg finish
if err != nil {
return err
}
return nil
}