-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo.go
More file actions
123 lines (102 loc) · 2.73 KB
/
video.go
File metadata and controls
123 lines (102 loc) · 2.73 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
package w3pilot
import (
"context"
"encoding/json"
"fmt"
)
// VideoOptions configures video recording.
type VideoOptions struct {
// Dir is the directory to save videos to. Defaults to a temp directory.
Dir string `json:"dir,omitempty"`
// Size specifies the video dimensions. Defaults to viewport size.
Size *VideoSize `json:"size,omitempty"`
}
// VideoSize specifies video dimensions.
type VideoSize struct {
Width int `json:"width"`
Height int `json:"height"`
}
// Video represents an ongoing or completed video recording.
type Video struct {
client *BiDiClient
context string
// FilePath is the file path where the video will be saved.
FilePath string `json:"path,omitempty"`
}
// StartVideo starts recording video of the page.
// The video is saved when StopVideo is called or the browser closes.
func (p *Pilot) StartVideo(ctx context.Context, opts *VideoOptions) (*Video, error) {
if p.closed {
return nil, ErrConnectionClosed
}
browsingCtx, err := p.getContext(ctx)
if err != nil {
return nil, err
}
params := map[string]interface{}{
"context": browsingCtx,
}
if opts != nil {
if opts.Dir != "" {
params["dir"] = opts.Dir
}
if opts.Size != nil {
params["size"] = map[string]int{
"width": opts.Size.Width,
"height": opts.Size.Height,
}
}
}
result, err := p.client.Send(ctx, "vibium:video.start", params)
if err != nil {
return nil, fmt.Errorf("failed to start video: %w", err)
}
var resp struct {
Path string `json:"path"`
}
if err := json.Unmarshal(result, &resp); err != nil {
return nil, fmt.Errorf("failed to parse video response: %w", err)
}
return &Video{
client: p.client,
context: browsingCtx,
FilePath: resp.Path,
}, nil
}
// StopVideo stops video recording and returns the video path.
func (p *Pilot) StopVideo(ctx context.Context) (string, error) {
if p.closed {
return "", ErrConnectionClosed
}
browsingCtx, err := p.getContext(ctx)
if err != nil {
return "", err
}
params := map[string]interface{}{
"context": browsingCtx,
}
result, err := p.client.Send(ctx, "vibium:video.stop", params)
if err != nil {
return "", fmt.Errorf("failed to stop video: %w", err)
}
var resp struct {
Path string `json:"path"`
}
if err := json.Unmarshal(result, &resp); err != nil {
return "", fmt.Errorf("failed to parse video response: %w", err)
}
return resp.Path, nil
}
// Path returns the path where the video will be saved.
// The file may not exist until StopVideo is called.
func (vid *Video) Path() string {
return vid.FilePath
}
// Delete deletes the video file.
func (vid *Video) Delete(ctx context.Context) error {
params := map[string]interface{}{
"path": vid.FilePath,
}
_, err := vid.client.Send(ctx, "vibium:video.delete", params)
return err
}