Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
add frame resizing
EnzoFanAccounte629eec48094
diff --git a/browser_use/browser/video_recorder.py b/browser_use/browser/video_recorder.pyindex 5d260fe66..2445cfde0 100644--- a/browser_use/browser/video_recorder.py+++ b/browser_use/browser/video_recorder.py@@ -2,13 +2,18 @@import base64import logging+import math+import subprocessfrom pathlib import Pathfrom typing import Optional+import numpy as np+from browser_use.browser.profile import ViewportSizetry:import imageio.v2 as iio+ import imageio_ffmpegfrom imageio.core.format import FormatIMAGEIO_AVAILABLE = True@@ -18,6 +23,13 @@ except ImportError:logger = logging.getLogger(__name__)+def _get_padded_size(size: ViewportSize, macro_block_size: int = 16) -> ViewportSize:+ """Calculates the dimensions padded to the nearest multiple of macro_block_size."""+ width = int(math.ceil(size['width'] / macro_block_size)) * macro_block_size+ height = int(math.ceil(size['height'] / macro_block_size)) * macro_block_size+ return ViewportSize(width=width, height=height)++class VideoRecorderService:"""Handles the video encoding process for a browser session using imageio.@@ -41,6 +53,7 @@ class VideoRecorderService:self.framerate = framerateself._writer: Optional['Format.Writer'] = Noneself._is_active = False+ self.padded_size = _get_padded_size(self.size)def start(self) -> None:"""@@ -57,13 +70,14 @@ class VideoRecorderService:try:self.output_path.parent.mkdir(parents=True, exist_ok=True)+ # The macro_block_size is set to None because we handle padding ourselvesself._writer = iio.get_writer(str(self.output_path),fps=self.framerate,codec='libx264',quality=8, # A good balance of quality and file size (1-10 scale)pixelformat='yuv420p', # Ensures compatibility with most players- macro_block_size=16, # Recommended for h264+ macro_block_size=None,)self._is_active = Truelogger.debug(f'Video recorder started. Output will be saved to {self.output_path}')@@ -73,10 +87,8 @@ class VideoRecorderService:def add_frame(self, frame_data_b64: str) -> None:"""- Decodes a base64-encoded PNG frame and appends it to the video.-- This method is designed to be fast and non-blocking. It will- gracefully handle corrupted frames.+ Decodes a base64-encoded PNG frame, resizes it, pads it to be codec-compatible,+ and appends it to the video.Args:frame_data_b64: A base64-encoded string of the PNG frame data.@@ -86,21 +98,47 @@ class VideoRecorderService:try:frame_bytes = base64.b64decode(frame_data_b64)- # imageio reads bytes directly and converts to a numpy array- # The format is auto-detected from the bytes.- img_array = iio.imread(frame_bytes)-- # Ensure frame dimensions match video dimensions- h, w, _ = img_array.shape- if w != self.size['width'] or h != self.size['height']:- # This can happen if the viewport changes mid-recording.- # A more robust solution could involve resizing, but that is non-trivial.- # For now, the video size must be the same as the viewport- logger.warning(- f'Frame size ({w}x{h}) does not match video size '- f'({self.size["width"]}x{self.size["height"]}). Skipping frame.'- )- return++ # Build a filter chain for ffmpeg:+ # 1. scale: Resizes the frame to the user-specified dimensions.+ # 2. pad: Adds black bars to meet codec's macro-block requirements,+ # centering the original content.+ vf_chain = (+ f'scale={self.size["width"]}:{self.size["height"]},'+ f'pad={self.padded_size["width"]}:{self.padded_size["height"]}:(ow-iw)/2:(oh-ih)/2:color=black'+ )++ output_pix_fmt = 'rgb24'+ command = [+ imageio_ffmpeg.get_ffmpeg_exe(),+ '-f',+ 'image2pipe', # Input format from a pipe+ '-c:v',+ 'png', # Specify input codec is PNG+ '-i',+ '-', # Input from stdin+ '-vf',+ vf_chain, # Video filter for resizing and padding+ '-f',+ 'rawvideo', # Output format is raw video+ '-pix_fmt',+ output_pix_fmt, # Output pixel format+ '-', # Output to stdout+ ]++ # Execute ffmpeg as a subprocess+ proc = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)+ out, err = proc.communicate(input=frame_bytes)++ if proc.returncode != 0:+ err_msg = err.decode(errors='ignore').strip()+ if 'deprecated pixel format used' not in err_msg.lower():+ raise IOError(f'ffmpeg error during resizing/padding: {err_msg}')+ else:+ logger.debug(f'ffmpeg warning during resizing/padding: {err_msg}')++ # Convert the raw output bytes to a numpy array with the padded dimensions+ img_array = np.frombuffer(out, dtype=np.uint8).reshape((self.padded_size['height'], self.padded_size['width'], 3))self._writer.append_data(img_array)except Exception as e:diff --git a/docs/customize/browser-settings.mdx b/docs/customize/browser-settings.mdxindex dbec43905..defae8b76 100644--- a/docs/customize/browser-settings.mdx+++ b/docs/customize/browser-settings.mdx@@ -354,13 +354,31 @@ window_position: dict | None = {"width": 0, "height": 0}Window position from top-left corner.-#### `save_recording_path`+#### `record_video_dir`++```python+record_video_dir: str | Path | None = None+```++(alias: `save_recording_path`)++Directory to save video recordings. If a path is provided, a video of the session will be recorded to an MP4 file in this directory.++#### `record_video_size`++```python+record_video_size: dict | None = None+```++The frame size (width, height) of the video recording. If not set, it will default to the browser's viewport size. Example: `{"width": 1280, "height": 720}`++#### `record_video_framerate````python-save_recording_path: str | None = None+record_video_framerate: int = 30```-Directory path for saving video recordings.+The framerate for the video recording. Defaults to 30 frames per second.#### `trace_path`@@ -779,32 +797,6 @@ Client certificates to be used with requests.Note: Browser Use also provides some of our own recording-related options not listed below (see above).-#### `record_video_dir`--<a name="record-video-dir"></a>-<a name="save-recording-path"></a>--```python-record_video_dir: str | Path | None = None-```--Directory to save `.webm` video recordings. [Playwright Docs: `record_video_dir`](https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch-persistent-context-option-record-video-dir)--<Note>- This parameter also has an alias `save_recording_path` for backwards- compatibility with past versions, but we recommend using the standard- Playwright name `record_video_dir` going forward.-</Note>--#### `record_video_size`--```python-record_video_size: dict | None = None. [Playwright Docs: `record_video_size`](https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch-persistent-context-option-record-video-size)--```--Video size. Example: `{"width": 1280, "height": 720}`-#### `record_har_path`<a name="record-har-path"></a>diff --git a/examples/features/video_recording.py b/examples/features/video_recording.pyindex 0a3f90783..87c7e1e10 100644--- a/examples/features/video_recording.py+++ b/examples/features/video_recording.py@@ -11,7 +11,7 @@ async def main():browser_session = BrowserSession(browser_profile=video_profile)agent = Agent(- task='Go to github.com/trending then navigate to the first trending repository.',+ task='Go to github.com/trending then navigate to the first trending repository and report how many commits it has.',llm=ChatOpenAI(model='gpt-4.1-mini'),browser_session=browser_session,)