Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Farama Foundation Gymnasium RecordVideo

From Leeroopedia
Knowledge Sources
Domains Reinforcement_Learning, Visualization
Last Updated 2026-02-15 03:00 GMT

Overview

Concrete tool for recording environment episode videos provided by the Gymnasium library.

Description

The RecordVideo wrapper captures frames from the environment's render() method and saves them as MP4 video files using MoviePy. It supports episode-triggered and step-triggered recording with configurable video length. The wrapper requires render_mode="rgb_array" on the environment.

Usage

Wrap environments with RecordVideo during evaluation to capture agent behavior. Requires moviepy (installable via pip install "gymnasium[other]").

Code Reference

Source Location

  • Repository: Gymnasium
  • File: gymnasium/wrappers/rendering.py
  • Lines: L162-320

Signature

class RecordVideo(gym.Wrapper[ObsType, ActType, ObsType, ActType]):
    def __init__(
        self,
        env: gym.Env[ObsType, ActType],
        video_folder: str,
        episode_trigger: Callable[[int], bool] | None = None,
        step_trigger: Callable[[int], bool] | None = None,
        video_length: int = 0,
        name_prefix: str = "rl-video",
        fps: int | None = None,
        disable_logger: bool = True,
        gc_trigger: Callable[[int], bool] | None = lambda episode: True,
    ):
        """Records videos of environment episodes.

        Args:
            env: The environment (must have render_mode="rgb_array").
            video_folder: Folder to store recorded videos.
            episode_trigger: Function(episode_id) -> bool to control recording.
            step_trigger: Function(step_id) -> bool to control recording.
            video_length: Fixed video length (0 = full episodes).
            name_prefix: Video filename prefix.
            fps: Frame rate (default from env metadata or 30).
            disable_logger: Disable MoviePy logger output.
            gc_trigger: Garbage collection trigger function.
        """

Import

from gymnasium.wrappers import RecordVideo

I/O Contract

Inputs

Name Type Required Description
env gym.Env Yes Environment with render_mode="rgb_array"
video_folder str Yes Directory to save videos
episode_trigger Callable or None No Function controlling which episodes to record
step_trigger Callable or None No Function controlling recording by step count
video_length int No Fixed video length (0 = full episode)
fps int or None No Video frame rate

Outputs

Name Type Description
video files .mp4 files Saved in video_folder with name_prefix

Usage Examples

Record Every 10th Episode

import gymnasium as gym
from gymnasium.wrappers import RecordVideo

env = gym.make("LunarLander-v3", render_mode="rgb_array")
env = RecordVideo(
    env,
    video_folder="./videos",
    episode_trigger=lambda ep: ep % 10 == 0,
    disable_logger=True,
)

for episode in range(50):
    obs, info = env.reset(seed=123)
    terminated, truncated = False, False
    while not (terminated or truncated):
        action = env.action_space.sample()
        obs, reward, terminated, truncated, info = env.step(action)

env.close()

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment