browser

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddAudioToVideo

func AddAudioToVideo(ctx context.Context, videoPath, audioPath, outputPath string) error

AddAudioToVideo combines video with audio track

func PadVideoToDuration

func PadVideoToDuration(ctx context.Context, videoPath string, targetDurationMs int, outputPath string) error

PadVideoToDuration extends video to a specific duration using the last frame

Types

type CaptureConfig

type CaptureConfig struct {
	// Mode determines capture strategy
	Mode CaptureMode

	// OutputDir is where captured frames/video are stored
	OutputDir string

	// Width is the capture width in pixels
	Width int

	// Height is the capture height in pixels
	Height int

	// FrameRate is frames per second
	FrameRate int

	// Quality is the output quality (0-100, higher is better)
	Quality int

	// Format is the output video format (mp4, webm)
	Format string
}

CaptureConfig configures video capture settings

func DefaultCaptureConfig

func DefaultCaptureConfig() CaptureConfig

DefaultCaptureConfig returns a default capture configuration

type CaptureMode

type CaptureMode string

CaptureMode defines how video is captured

const (
	// CaptureModeScreenshot captures screenshots and stitches them into video
	CaptureModeScreenshot CaptureMode = "screenshot"
	// CaptureModeVideo captures continuous video (requires headed browser)
	CaptureModeVideo CaptureMode = "video"
)

type Capturer

type Capturer struct {
	// contains filtered or unexported fields
}

Capturer handles screenshot capture and video generation

func NewCapturer

func NewCapturer(config CaptureConfig) (*Capturer, error)

NewCapturer creates a new capturer

func (*Capturer) Cleanup

func (c *Capturer) Cleanup() error

Cleanup removes temporary frame files

func (*Capturer) GenerateVideo

func (c *Capturer) GenerateVideo(ctx context.Context, outputPath string) error

GenerateVideo stitches captured frames into a video file

func (*Capturer) GenerateVideoWithDurations

func (c *Capturer) GenerateVideoWithDurations(ctx context.Context, outputPath string, durations map[int]int) error

GenerateVideoWithDurations generates video with per-frame durations durations is a map of frame index to duration in milliseconds This is useful when steps have minimum durations for voiceover sync

func (*Capturer) GetFrameCount

func (c *Capturer) GetFrameCount() int

GetFrameCount returns the current frame count

func (*Capturer) GetFramePath

func (c *Capturer) GetFramePath(index int) string

GetFramePath returns the path for a specific frame

func (*Capturer) SaveFrame

func (c *Capturer) SaveFrame(data []byte) (string, error)

SaveFrame saves a screenshot frame

type Recorder

type Recorder struct {
	// contains filtered or unexported fields
}

Recorder handles browser automation and video capture

func NewRecorder

func NewRecorder(config RecorderConfig) (*Recorder, error)

NewRecorder creates a new browser recorder

func (*Recorder) Cleanup

func (r *Recorder) Cleanup() error

Cleanup removes temporary files but keeps the final video

func (*Recorder) Close

func (r *Recorder) Close() error

Close releases browser resources

func (*Recorder) GenerateVideo

func (r *Recorder) GenerateVideo(ctx context.Context) (string, error)

GenerateVideo creates the final video from captured frames

func (*Recorder) GetResults

func (r *Recorder) GetResults() []StepResult

GetResults returns all step results

func (*Recorder) GetTimingData

func (r *Recorder) GetTimingData() *TimingData

GetTimingData returns the timing data for the recording

func (*Recorder) GetVideoPath

func (r *Recorder) GetVideoPath() string

GetVideoPath returns the path to the generated video

func (*Recorder) Launch

func (r *Recorder) Launch() error

Launch starts the browser

func (*Recorder) Navigate

func (r *Recorder) Navigate(url string) error

Navigate navigates to a URL and creates an initial page

func (*Recorder) RecordSteps

func (r *Recorder) RecordSteps(ctx context.Context, steps []Step) ([]StepResult, error)

RecordSteps executes steps and captures video/screenshots

func (*Recorder) SaveTimingData

func (r *Recorder) SaveTimingData() error

SaveTimingData saves timing data to a JSON file

type RecorderConfig

type RecorderConfig struct {
	// Width is the browser viewport width
	Width int

	// Height is the browser viewport height
	Height int

	// OutputDir is where recordings are stored
	OutputDir string

	// FrameRate is the capture frame rate
	FrameRate int

	// Headless runs the browser without UI
	Headless bool

	// DefaultTimeout is the default step timeout in milliseconds
	DefaultTimeout int

	// CaptureEveryStep captures a screenshot after every step
	CaptureEveryStep bool

	// UserAgent overrides the browser user agent
	UserAgent string

	// DeviceScaleFactor sets the device pixel ratio
	DeviceScaleFactor float64
}

RecorderConfig configures the browser recorder

func DefaultRecorderConfig

func DefaultRecorderConfig() RecorderConfig

DefaultRecorderConfig returns a default recorder configuration

type ScrollBehavior

type ScrollBehavior string

ScrollBehavior defines how scrolling is animated

const (
	// ScrollBehaviorAuto uses instant scrolling (default)
	ScrollBehaviorAuto ScrollBehavior = "auto"
	// ScrollBehaviorSmooth uses smooth animated scrolling
	ScrollBehaviorSmooth ScrollBehavior = "smooth"
)

type ScrollMode

type ScrollMode string

ScrollMode defines how scroll coordinates are interpreted

const (
	// ScrollModeRelative scrolls by the specified delta (default)
	ScrollModeRelative ScrollMode = "relative"
	// ScrollModeAbsolute scrolls to the specified position
	ScrollModeAbsolute ScrollMode = "absolute"
)

type Step

type Step struct {
	// Action is the type of action to perform
	Action StepAction `json:"action" yaml:"action"`

	// Selector is the CSS selector or XPath for element targeting
	Selector string `json:"selector,omitempty" yaml:"selector,omitempty"`

	// Value is used for input actions (text to type) or select actions (option value)
	Value string `json:"value,omitempty" yaml:"value,omitempty"`

	// URL is used for navigate actions
	URL string `json:"url,omitempty" yaml:"url,omitempty"`

	// Duration is used for wait actions (milliseconds)
	Duration int `json:"duration,omitempty" yaml:"duration,omitempty"`

	// Script is JavaScript code for evaluate actions
	Script string `json:"script,omitempty" yaml:"script,omitempty"`

	// Key is for keypress actions (e.g., "Enter", "Tab", "Escape")
	Key string `json:"key,omitempty" yaml:"key,omitempty"`

	// Voiceover is the text to speak during this step
	Voiceover string `json:"voiceover,omitempty" yaml:"voiceover,omitempty"`

	// MinDuration ensures the step takes at least this long (milliseconds)
	// Useful for ensuring voiceover completes before next step
	MinDuration int `json:"minDuration,omitempty" yaml:"minDuration,omitempty"`

	// Description is a human-readable description of the step
	Description string `json:"description,omitempty" yaml:"description,omitempty"`

	// ScrollX and ScrollY are pixel amounts for scroll actions
	ScrollX int `json:"scrollX,omitempty" yaml:"scrollX,omitempty"`
	ScrollY int `json:"scrollY,omitempty" yaml:"scrollY,omitempty"`

	// ScrollMode determines if scroll is relative (delta) or absolute (position)
	// Default is "relative" for backward compatibility
	ScrollMode ScrollMode `json:"scrollMode,omitempty" yaml:"scrollMode,omitempty"`

	// ScrollBehavior determines if scroll is instant ("auto") or animated ("smooth")
	// When "smooth", the step automatically waits for the scroll animation to complete
	ScrollBehavior ScrollBehavior `json:"scrollBehavior,omitempty" yaml:"scrollBehavior,omitempty"`

	// Text enables click-by-text-content instead of selector
	// When set, finds elements containing this text and clicks them
	Text string `json:"text,omitempty" yaml:"text,omitempty"`

	// TextScope restricts text search to elements matching this selector
	// Example: ".sidebar" to only search within sidebar elements
	TextScope string `json:"textScope,omitempty" yaml:"textScope,omitempty"`

	// TextMatch determines how text matching is performed
	// "contains" (default), "exact", or "regex"
	TextMatch TextMatch `json:"textMatch,omitempty" yaml:"textMatch,omitempty"`

	// Timeout overrides the default step timeout (milliseconds)
	Timeout int `json:"timeout,omitempty" yaml:"timeout,omitempty"`
}

Step represents a single browser automation step

func (*Step) GetEffectiveTimeout

func (s *Step) GetEffectiveTimeout(defaultTimeout int) time.Duration

GetEffectiveTimeout returns the timeout to use for this step

func (*Step) Validate

func (s *Step) Validate() error

Validate checks if the step is valid

type StepAction

type StepAction string

StepAction defines the type of browser action

const (
	// ActionNavigate navigates to a URL
	ActionNavigate StepAction = "navigate"
	// ActionClick clicks an element
	ActionClick StepAction = "click"
	// ActionInput enters text into an element
	ActionInput StepAction = "input"
	// ActionScroll scrolls the page or element
	ActionScroll StepAction = "scroll"
	// ActionWait waits for a duration
	ActionWait StepAction = "wait"
	// ActionWaitFor waits for an element to appear
	ActionWaitFor StepAction = "waitFor"
	// ActionScreenshot captures a screenshot
	ActionScreenshot StepAction = "screenshot"
	// ActionEvaluate executes JavaScript
	ActionEvaluate StepAction = "evaluate"
	// ActionHover hovers over an element
	ActionHover StepAction = "hover"
	// ActionSelect selects an option from a dropdown
	ActionSelect StepAction = "select"
	// ActionKeypress sends keyboard input
	ActionKeypress StepAction = "keypress"
)

type StepResult

type StepResult struct {
	// Step is the original step definition
	Step Step `json:"step"`

	// Index is the step's position in the sequence
	Index int `json:"index"`

	// StartTime is when the step began execution
	StartTime time.Time `json:"startTime"`

	// EndTime is when the step completed
	EndTime time.Time `json:"endTime"`

	// Duration is the actual execution time in milliseconds
	Duration int `json:"durationMs"`

	// Screenshot is the path to the screenshot taken after this step
	Screenshot string `json:"screenshot,omitempty"`

	// Error contains any error message from execution
	Error string `json:"error,omitempty"`

	// Success indicates whether the step completed successfully
	Success bool `json:"success"`
}

StepResult contains execution results for a step

type StepSequence

type StepSequence struct {
	// Name identifies this step sequence
	Name string `json:"name,omitempty"`

	// URL is the starting URL for the sequence
	URL string `json:"url"`

	// Steps is the ordered list of steps to execute
	Steps []Step `json:"steps"`

	// DefaultTimeout is the default timeout for steps (milliseconds)
	DefaultTimeout int `json:"defaultTimeout,omitempty"`
}

StepSequence represents a sequence of steps with metadata

func LoadStepSequence

func LoadStepSequence(path string) (*StepSequence, error)

LoadStepSequence loads a step sequence from a JSON file

func (*StepSequence) SaveToFile

func (seq *StepSequence) SaveToFile(path string) error

SaveStepSequence saves a step sequence to a JSON file

func (*StepSequence) Validate

func (seq *StepSequence) Validate() error

Validate checks if the step sequence is valid

type StepTiming

type StepTiming struct {
	// Index is the step's position in the sequence
	Index int `json:"index"`

	// Action is the step action type
	Action StepAction `json:"action"`

	// Description provides context for the step
	Description string `json:"description,omitempty"`

	// Voiceover is the text spoken during this step
	Voiceover string `json:"voiceover,omitempty"`

	// StartMs is the start time relative to session start (milliseconds)
	StartMs int `json:"startMs"`

	// EndMs is the end time relative to session start (milliseconds)
	EndMs int `json:"endMs"`

	// DurationMs is the step duration (milliseconds)
	DurationMs int `json:"durationMs"`

	// StartFrame is the first frame index for this step
	StartFrame int `json:"startFrame"`

	// EndFrame is the last frame index for this step
	EndFrame int `json:"endFrame"`

	// Screenshot is the path to the step's screenshot (if captured)
	Screenshot string `json:"screenshot,omitempty"`
}

StepTiming contains timing information for a single step

type TextMatch

type TextMatch string

TextMatch defines how text content is matched

const (
	// TextMatchContains matches if element text contains the search text
	TextMatchContains TextMatch = "contains"
	// TextMatchExact matches if element text equals the search text exactly
	TextMatchExact TextMatch = "exact"
	// TextMatchRegex matches if element text matches the regex pattern
	TextMatchRegex TextMatch = "regex"
)

type TimingData

type TimingData struct {
	// SessionID uniquely identifies this recording session
	SessionID string `json:"sessionId"`

	// URL is the starting URL
	URL string `json:"url"`

	// StartTime is when the session began
	StartTime time.Time `json:"startTime"`

	// EndTime is when the session completed
	EndTime time.Time `json:"endTime"`

	// TotalDuration is the session duration in milliseconds
	TotalDuration int `json:"totalDurationMs"`

	// Steps contains timing for each step
	Steps []StepTiming `json:"steps"`

	// FrameCount is the total number of frames captured
	FrameCount int `json:"frameCount"`

	// FrameRate is the capture frame rate
	FrameRate int `json:"frameRate"`
}

TimingData contains timing information for a recorded browser session

func LoadTimingData

func LoadTimingData(path string) (*TimingData, error)

LoadTimingData loads timing data from a JSON file

func NewTimingData

func NewTimingData(sessionID, url string, frameRate int) *TimingData

NewTimingData creates a new timing data container

func (*TimingData) AddStepTiming

func (td *TimingData) AddStepTiming(result *StepResult, startFrame, endFrame int)

AddStepTiming adds timing data for a step

func (*TimingData) AdjustForTTSDuration

func (td *TimingData) AdjustForTTSDuration(stepIndex int, ttsDurationMs int)

AdjustForTTSDuration adjusts step timing to accommodate TTS audio duration If the TTS audio is longer than the step duration, extend the step

func (*TimingData) Finalize

func (td *TimingData) Finalize(frameCount int)

Finalize completes the timing data

func (*TimingData) GetStepAtFrame

func (td *TimingData) GetStepAtFrame(frame int) *StepTiming

GetStepAtFrame returns the step active at a given frame

func (*TimingData) GetStepAtTime

func (td *TimingData) GetStepAtTime(ms int) *StepTiming

GetStepAtTime returns the step active at a given time (milliseconds from start)

func (*TimingData) SaveToFile

func (td *TimingData) SaveToFile(path string) error

SaveToFile saves timing data to a JSON file

func (*TimingData) ToTranscriptSegments

func (td *TimingData) ToTranscriptSegments() []TranscriptSegment

ToTranscriptSegments converts timing data to transcript-compatible segments This is useful for generating voiceover timing from browser recording

type TranscriptSegment

type TranscriptSegment struct {
	Text    string `json:"text"`
	StartMs int    `json:"startMs"`
	EndMs   int    `json:"endMs"`
}

TranscriptSegment represents a segment for transcript generation

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL