Capturing & Video
Screen/element capture and video recording, ported from FlaUI.Core.Capturing. Capture
provides static helpers that return a CaptureImage (savable via to_file and usable as a
context manager). VideoRecorder records the screen to a file and requires ffmpeg
(see VideoRecorder.download_ffmpeg).
Settings
CaptureSettings and VideoRecorderSettings are Pydantic models: values are validated in
Python (for example output_scale must be positive and frame_rate at least 1) before they
reach FlaUI. cs_object builds the C# settings object and from_cs reads one back, so
CaptureImage.settings returns the typed model the image was captured with.
| C# property | Python field | Default |
|---|---|---|
CaptureSettings.OutputWidth |
output_width |
-1 (keep aspect ratio) |
CaptureSettings.OutputHeight |
output_height |
-1 (keep aspect ratio) |
CaptureSettings.OutputScale |
output_scale |
1.0 |
VideoRecorderSettings.ffmpegPath |
ffmpeg_path |
None |
VideoRecorderSettings.FrameRate |
frame_rate |
5 |
VideoRecorderSettings.TargetVideoPath |
target_video_path |
None |
VideoRecorderSettings.UseCompressedImages |
use_compressed_images |
True |
VideoRecorderSettings.VideoFormat |
video_format |
VideoFormat.x264 |
VideoRecorderSettings.VideoQuality |
video_quality |
0 |
VideoRecorderSettings.EncodeWithLowPriority |
encode_with_low_priority |
False |
VideoRecorderSettings.LogMissingFrames |
log_missing_frames |
True |
Overlays
MouseOverlay and InfoOverlay are built from the image they draw on and passed straight to
CaptureImage.apply_overlays. Raw C# ICaptureOverlay objects are still accepted, and the two
can be mixed in one call.
from flaui.core.capturing import Capture, InfoOverlay, InfoOverlayPosition, MouseOverlay
from flaui.lib.system.drawing import Color
with Capture.element(window) as image:
info = InfoOverlay(
image,
overlay_string_format="{dt:HH:mm:ss} / {name}", # time and machine name
overlay_position=InfoOverlayPosition.BottomRight,
overlay_text_color=Color.Yellow,
)
image.apply_overlays(MouseOverlay(image), info).to_file(str(tmp_path / "annotated.png"))
using (var image = Capture.Element(window))
{
var info = new InfoOverlay(image)
{
OverlayStringFormat = "{dt:HH:mm:ss} / {name}",
OverlayPosition = InfoOverlayPosition.BottomRight,
OverlayTextColor = Color.Yellow,
};
image.ApplyOverlays(new MouseOverlay(image), info).ToFile(@"C:\temp\annotated.png");
}
Durations are datetime.timedelta on the Python side: VideoRecorder.record_time_span and
InfoOverlay.record_time_span (the {rt} placeholder) both use it. The overlay's font stays a
C# System.Drawing.Font.
Recording video
The frame callback receives the running recorder, so each frame can stamp the elapsed time:
from flaui.core.capturing import (
Capture,
CaptureImage,
CaptureSettings,
InfoOverlay,
MouseOverlay,
VideoFormat,
VideoRecorder,
VideoRecorderSettings,
)
settings = VideoRecorderSettings(
ffmpeg_path=r"C:\tools\ffmpeg.exe", # or VideoRecorder.download_ffmpeg(folder)
target_video_path=str(tmp_path / "run.mp4"),
frame_rate=10,
video_format=VideoFormat.xvid, # bigger file, less CPU than x264
)
def frame(recorder: VideoRecorder) -> CaptureImage:
"""Capture one frame with the cursor and the elapsed recording time ({rt}) drawn on it."""
image = Capture.main_screen(CaptureSettings(output_scale=0.5))
info = InfoOverlay(image, overlay_string_format="{rt:hh\\:mm\\:ss}", record_time_span=recorder.record_time_span)
return image.apply_overlays(info, MouseOverlay(image))
# with VideoRecorder.start(settings, frame) as recorder: # needs ffmpeg at settings.ffmpeg_path
# ... # drive the application; leaving the block stops and encodes the video
var settings = new VideoRecorderSettings
{
ffmpegPath = @"C:\tools\ffmpeg.exe",
TargetVideoPath = @"C:\temp\run.mp4",
FrameRate = 10,
VideoFormat = VideoFormat.xvid,
};
using (var recorder = new VideoRecorder(settings, r =>
{
var image = Capture.MainScreen(new CaptureSettings { OutputScale = 0.5 });
var info = new InfoOverlay(image) { OverlayStringFormat = @"{rt:hh\:mm\:ss}", RecordTimeSpan = r.RecordTimeSpan };
return image.ApplyOverlays(info, new MouseOverlay(image));
}))
{
// drive the application
}
Reference
flaui.core.capturing.Capture
Static screen/element capture helpers (mirrors FlaUI.Core.Capturing.Capture).
element(element, settings=None)
staticmethod
Capture a single automation element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element
|
'AutomationElement'
|
The element to capture. |
required |
settings
|
Optional[CaptureSettings]
|
Optional output sizing settings. |
None
|
Returns:
| Type | Description |
|---|---|
CaptureImage
|
The captured image. |
Source code in flaui/core/capturing.py
element_rectangle(element, rectangle, settings=None)
staticmethod
Capture a rectangle (relative to the element) inside an element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element
|
'AutomationElement'
|
The element whose region to capture. |
required |
rectangle
|
Rectangle
|
The rectangle relative to the element's top-left corner. |
required |
settings
|
Optional[CaptureSettings]
|
Optional output sizing settings. |
None
|
Returns:
| Type | Description |
|---|---|
CaptureImage
|
The captured image. |
Source code in flaui/core/capturing.py
main_screen(settings=None)
staticmethod
Capture the primary screen.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
Optional[CaptureSettings]
|
Optional output sizing settings. |
None
|
Returns:
| Type | Description |
|---|---|
CaptureImage
|
The captured image. |
Source code in flaui/core/capturing.py
rectangle(bounds, settings=None)
staticmethod
Capture a desktop-relative rectangle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bounds
|
Rectangle
|
The screen region to capture. |
required |
settings
|
Optional[CaptureSettings]
|
Optional output sizing settings. |
None
|
Returns:
| Type | Description |
|---|---|
CaptureImage
|
The captured image. |
Source code in flaui/core/capturing.py
screen(screen_index=-1, settings=None)
staticmethod
Capture a screen (or the whole virtual desktop when screen_index is out of range).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
screen_index
|
int
|
Zero-based monitor index; |
-1
|
settings
|
Optional[CaptureSettings]
|
Optional output sizing settings. |
None
|
Returns:
| Type | Description |
|---|---|
CaptureImage
|
The captured image. |
Source code in flaui/core/capturing.py
screens_with_element(element, settings=None)
staticmethod
Capture every screen the given element overlaps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element
|
'AutomationElement'
|
The element to locate across screens. |
required |
settings
|
Optional[CaptureSettings]
|
Optional output sizing settings. |
None
|
Returns:
| Type | Description |
|---|---|
CaptureImage
|
The captured image. |
Source code in flaui/core/capturing.py
flaui.core.capturing.CaptureImage
Bases: BaseModel
A captured image (mirrors FlaUI.Core.Capturing.CaptureImage).
Wraps the C# result of a :class:Capture call. Use :meth:to_file to persist it, or use it as a
context manager to dispose the underlying bitmap automatically.
bitmap
property
Return the underlying C# Bitmap.
bitmap_image
property
Return the image as a C# WPF System.Windows.Media.Imaging.BitmapImage.
Each access encodes the bitmap again, so keep the result if you need it more than once.
original_bounds
property
Return the desktop-relative bounding rectangle this image was captured from.
settings
property
Return the settings the image was captured with, or None when none were given.
__enter__()
__exit__(*exc_info)
Dispose the image on context-manager exit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc_info
|
Any
|
Standard |
()
|
apply_overlays(*overlays)
Draw the given overlays onto the image, in order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
overlays
|
Union['ICaptureOverlay', Any]
|
Python overlays (:class: |
()
|
Returns:
| Type | Description |
|---|---|
'CaptureImage'
|
This :class: |
Source code in flaui/core/capturing.py
dispose()
to_file(file_path)
Save the image to a file; the extension selects the format (defaults to PNG).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Destination path ( |
required |
Source code in flaui/core/capturing.py
flaui.core.capturing.CaptureSettings
Bases: BaseModel
Output sizing options for a capture (mirrors FlaUI.Core.Capturing.CaptureSettings).
output_width / output_height take precedence over output_scale; leave one of them at
-1 to keep the aspect ratio.
cs_object
property
Return the underlying C# CaptureSettings built from this model.
from_cs(cs_settings)
classmethod
Build a model from a C# CaptureSettings instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cs_settings
|
Any
|
The C# |
required |
Returns:
| Type | Description |
|---|---|
'CaptureSettings'
|
The equivalent :class: |
Source code in flaui/core/capturing.py
flaui.core.capturing.ICaptureOverlay
Bases: BaseModel
Wraps any C# FlaUI.Core.Capturing.ICaptureOverlay (something that draws onto a capture).
Use it directly to pass a custom C# overlay through the typed API, or use the concrete
:class:MouseOverlay / :class:InfoOverlay.
draw(graphics)
Draw the overlay onto a C# System.Drawing.Graphics surface.
:meth:CaptureImage.apply_overlays calls this for you; call it directly only when drawing
onto your own Graphics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graphics
|
Any
|
The C# |
required |
Source code in flaui/core/capturing.py
flaui.core.capturing.OverlayBase(capture_image=None, /, **data)
Bases: ICaptureOverlay
Base for overlays bound to a :class:CaptureImage (mirrors FlaUI.Core.Capturing.OverlayBase).
Subclasses are constructed from the image they will be drawn on, e.g. MouseOverlay(image).
An existing C# overlay can be wrapped with MouseOverlay(raw_overlay=cs_overlay).
Create the C# overlay for capture_image, or wrap raw_overlay when given.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
capture_image
|
Optional[CaptureImage]
|
The image the overlay will be drawn on. |
None
|
data
|
Any
|
Model fields; pass |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither |
Source code in flaui/core/capturing.py
capture_image
property
Return the image this overlay draws on.
flaui.core.capturing.MouseOverlay(capture_image=None, /, **data)
Bases: OverlayBase
Draws the mouse cursor onto a capture (mirrors FlaUI.Core.Capturing.MouseOverlay).
Example::
with Capture.main_screen() as image:
image.apply_overlays(MouseOverlay(image)).to_file("screen.png")
Source code in flaui/core/capturing.py
flaui.core.capturing.InfoOverlay(capture_image=None, /, **data)
Bases: OverlayBase
Draws a text bar (time, machine name, CPU/RAM, ...) onto a capture.
Mirrors FlaUI.Core.Capturing.InfoOverlay. Options may be passed as keyword arguments, e.g.
InfoOverlay(image, overlay_position=InfoOverlayPosition.BottomRight). The text comes from
:attr:overlay_string_format; FlaUI fills placeholders such as {dt} (now), {rt} (the
:attr:record_time_span), {name} (machine name), {cpu} and {mem.p.used}.
Create the overlay and apply any option keyword arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
capture_image
|
Optional[CaptureImage]
|
The image the overlay will be drawn on. |
None
|
data
|
Any
|
|
{}
|
Source code in flaui/core/capturing.py
overlay_background_color
property
writable
Get the background color of the text bar.
overlay_position
property
writable
Get where the text bar is drawn.
overlay_string_format
property
writable
Get the text template drawn by the overlay.
overlay_text_color
property
writable
Get the text color.
overlay_text_font
property
writable
Get the text font (a C# System.Drawing.Font).
record_time_span
property
writable
Get the recording time shown by the {rt} placeholder.
flaui.core.capturing.InfoOverlayPosition
Bases: Enum
Where :class:InfoOverlay draws its text bar (mirrors FlaUI.Core.Capturing.InfoOverlayPosition).
flaui.core.capturing.CaptureUtilities
Scaling and cursor helpers used by captures (mirrors FlaUI.Core.Capturing.CaptureUtilities).
capture_cursor()
staticmethod
Capture the current mouse cursor.
Returns:
| Type | Description |
|---|---|
Tuple[Any, Point]
|
|
Source code in flaui/core/capturing.py
delete_object(handle)
staticmethod
Delete a GDI object handle (wraps the Win32 DeleteObject).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handle
|
int
|
The GDI handle (e.g. from |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in flaui/core/capturing.py
get_scale(original_bounds, settings=None)
staticmethod
Return the scale factor settings applies to a capture of original_bounds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original_bounds
|
Rectangle
|
The bounds of the area being captured. |
required |
settings
|
Optional[CaptureSettings]
|
The capture settings; |
None
|
Returns:
| Type | Description |
|---|---|
float
|
The scale factor. |
Source code in flaui/core/capturing.py
scale_according_to_settings(value, settings=None, original_bounds=None)
staticmethod
Scale a point or rectangle the way settings scales a capture.
A :class:~flaui.lib.system.drawing.Rectangle is scaled against itself. A point (a
:class:~flaui.lib.system.drawing.Point or an (x, y) tuple) needs the
original_bounds of the capture it belongs to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Union[Point, Rectangle, Tuple[int, int]]
|
The point or rectangle to scale. |
required |
settings
|
Optional[CaptureSettings]
|
The capture settings; |
None
|
original_bounds
|
Optional[Rectangle]
|
Bounds of the captured area (required for points). |
None
|
Returns:
| Type | Description |
|---|---|
Union[Point, Rectangle]
|
The scaled point or rectangle (same kind as |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a point is given without |
Source code in flaui/core/capturing.py
flaui.core.capturing.VideoRecorder
Bases: BaseModel
Records the screen (or a custom frame source) to a video file.
Mirrors FlaUI.Core.Capturing.VideoRecorder. Start a recording with :meth:start, then call
:meth:stop (or use it as a context manager). Requires ffmpeg — see the module note.
record_time_span
property
Return how long the recorder has been recording (feed it to :attr:InfoOverlay.record_time_span).
target_video_path
property
Return the output video file path.
__enter__()
__exit__(*exc_info)
Stop and dispose the recorder on context-manager exit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc_info
|
Any
|
Standard |
()
|
dispose()
download_ffmpeg(target_folder)
staticmethod
Download a static ffmpeg build into target_folder (blocking).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_folder
|
str
|
Folder to download ffmpeg into. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The path to the downloaded ffmpeg executable. |
Source code in flaui/core/capturing.py
start(settings, capture_method=None)
classmethod
Start recording.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
VideoRecorderSettings
|
Recorder settings (must include |
required |
capture_method
|
Optional[Callable[['VideoRecorder'], CaptureImage]]
|
Optional callback |
None
|
Returns:
| Type | Description |
|---|---|
'VideoRecorder'
|
The running :class: |
Source code in flaui/core/capturing.py
flaui.core.capturing.VideoRecorderSettings
Bases: BaseModel
Settings for :class:VideoRecorder (mirrors FlaUI.Core.Capturing.VideoRecorderSettings).
cs_object
property
Return the underlying C# VideoRecorderSettings built from this model.
from_cs(cs_settings)
classmethod
Build a model from a C# VideoRecorderSettings instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cs_settings
|
Any
|
The C# |
required |
Returns:
| Type | Description |
|---|---|
'VideoRecorderSettings'
|
The equivalent :class: |
Source code in flaui/core/capturing.py
flaui.core.capturing.VideoFormat
Bases: Enum
Video codec used by :class:VideoRecorder (mirrors FlaUI.Core.Capturing.VideoFormat).