Table of Contents

Class RecordVideo<TObs, TAct>

Namespace
Gym.Wrappers
Assembly
Gym.dll

Records videos of episodes from the env's render, as Gymnasium's RecordVideo does: a recording starts when the episode or step trigger fires (by default episodes 0, 1, 8, 27, ..., 729, then every 1000th), lasts the episode or video_length frames, and is written through an IVideoWriter as {video_folder}/{name_prefix}-episode-{i} or -step-{i}.

public class RecordVideo<TObs, TAct> : Wrapper<TObs, TAct>, IDynamicAttributes, IWrapper, IEnv, IDisposable, IRecordConstructorArgs

Type Parameters

TObs

The observation type, passed through.

TAct

The action type, passed through.

Inheritance
Env<TObs, TAct>
Wrapper<TObs, TAct, TObs, TAct>
Wrapper<TObs, TAct>
RecordVideo<TObs, TAct>
Implements
Inherited Members
Extension Methods

Remarks

Gymnasium encodes with moviepy; here the encoder is pluggable (DR-25): PngVideoWriter by default (a folder of PNG frames per video, no external program) or FfmpegVideoWriter for mp4. The triggers, names, lengths and frame rate are Gymnasium's; a video's frames are the env's renders, so they equal Gymnasium's.

Every reset and step renders the env while recording, costing a frame's drawing each, and the frames stay in memory until the video is written. Closing (or disposing) the wrapper writes a video still recording; Python only warns from __del__ about one left unsaved. disable_logger is kept for Gymnasium's signature: the writers print no progress. gc_trigger left null is Gymnasium's default (collect after every video); pass _ => false for Python's None.

Reference: gymnasium/wrappers/rendering.py.

Constructors

RecordVideo(Env<TObs, TAct>, RecordVideoOptions)

Builds the instance from its options record: the properties set, the constructor's defaults for the rest.

public RecordVideo(Env<TObs, TAct> env, RecordVideoOptions options)

Parameters

env Env<TObs, TAct>

The env to wrap.

options RecordVideoOptions

The options.

Exceptions

ArgumentNullException

Thrown when options is null.

TypeError

Thrown when a required argument is unset, with Python's missing-argument message.

RecordVideo(Env<TObs, TAct>, string, Func<long, bool>?, Func<long, bool>?, long, string, double?, bool, Func<long, bool>?, IVideoWriter?)

Wraps env, creating videoFolder.

public RecordVideo(Env<TObs, TAct> env, string videoFolder, Func<long, bool>? episodeTrigger = null, Func<long, bool>? stepTrigger = null, long videoLength = 0, string namePrefix = "rl-video", double? fps = null, bool disableLogger = true, Func<long, bool>? gcTrigger = null, IVideoWriter? videoWriter = null)

Parameters

env Env<TObs, TAct>

The env; it must render images (rgb_array or a *_list mode).

videoFolder string

The folder of the videos, created if needed (a warning when it exists).

episodeTrigger Func<long, bool>

Whether to start recording at an episode (by number from 0); with no trigger at all, CappedCubicVideoSchedule(long).

stepTrigger Func<long, bool>

Whether to start recording at a step (counted across episodes from 0).

videoLength long

The frames per video; 0 records whole episodes.

namePrefix string

The prefix of the video names.

fps double?

The frame rate; null takes metadata["render_fps"], else 30.

disableLogger bool

moviepy's logger switch, kept for Gymnasium's signature.

gcTrigger Func<long, bool>

Whether to collect garbage after a video of an episode; null always does.

videoWriter IVideoWriter

The encoder; null for PngVideoWriter.

Exceptions

ValueError

Thrown when the env renders no images (no render mode, human or ansi), with Gymnasium's message.

ArgumentNullException

Thrown when videoFolder is null.

TypeError

Thrown when env is null.

Properties

DisableLogger

moviepy's logger switch (disable_logger); the writers print nothing either way.

public bool DisableLogger { get; set; }

Property Value

bool

EpisodeId

The number of the current episode, from 0 (episode_id; -1 before the first reset).

public long EpisodeId { get; }

Property Value

long

EpisodeTrigger

When to start recording at an episode (episode_trigger).

public Func<long, bool>? EpisodeTrigger { get; set; }

Property Value

Func<long, bool>

FramesPerSec

The videos' frame rate (frames_per_sec).

public double FramesPerSec { get; set; }

Property Value

double

GcTrigger

When to collect garbage after a video (gc_trigger).

public Func<long, bool>? GcTrigger { get; set; }

Property Value

Func<long, bool>

NamePrefix

The prefix of the video names (name_prefix).

public string NamePrefix { get; set; }

Property Value

string

RecordedFrames

The frames of the video being recorded (recorded_frames), owned by the wrapper.

public List<NDArray> RecordedFrames { get; }

Property Value

List<NDArray>

Recording

Whether a video is being recorded (recording).

public bool Recording { get; }

Property Value

bool

SavedKwargs

The recorded constructor arguments (Python's _saved_kwargs).

[PyName("_saved_kwargs")]
public Info SavedKwargs { get; }

Property Value

Info

StepId

The number of the last step, across episodes, from 0 (step_id; -1 before the first).

public long StepId { get; }

Property Value

long

StepTrigger

When to start recording at a step (step_trigger).

public Func<long, bool>? StepTrigger { get; set; }

Property Value

Func<long, bool>

VideoFolder

The absolute folder of the videos (video_folder).

public string VideoFolder { get; }

Property Value

string

VideoLength

The frames per video, infinite for whole episodes (video_length).

public double VideoLength { get; set; }

Property Value

double

VideoName

The current video's name without extension (_video_name), or null.

[PyName("_video_name")]
public string? VideoName { get; }

Property Value

string

VideoWriter

The encoder the videos go through.

public IVideoWriter VideoWriter { get; set; }

Property Value

IVideoWriter

Methods

Close()

Closes the env, then writes the video still being recorded.

public override void Close()

Dispose(bool)

Releases the frames still recorded and the history, then closes (writing a video still recording) and disposes as every wrapper does.

protected override void Dispose(bool disposing)

Parameters

disposing bool

true from Dispose().

Render()

The env's render; in a *_list mode the frames rendered while capturing come first, and while recording the returned frames join the video too.

public override RenderResult Render()

Returns

RenderResult

The render, which the caller owns.

Reset(BigInteger?, IReadOnlyDictionary<string, object?>?)

Resets the env, ends a whole-episode recording, and starts one when the episode trigger fires.

public override ResetResult<TObs> Reset(BigInteger? seed = null, IReadOnlyDictionary<string, object?>? options = null)

Parameters

seed BigInteger?

The seed, passed on.

options IReadOnlyDictionary<string, object>

The options, passed on.

Returns

ResetResult<TObs>

The env's reset.

Exceptions

Error

Thrown for a negative seed.

StartRecording(string)

Starts a video (start_recording), first writing the one being recorded.

public void StartRecording(string videoName)

Parameters

videoName string

The new video's name without extension.

Step(TAct)

Steps the env, starts a recording when the step trigger fires, and captures a frame while recording.

public override StepResult<TObs> Step(TAct action)

Parameters

action TAct

The action, passed on.

Returns

StepResult<TObs>

The env's transition.

StopRecording()

Writes the video being recorded and stops (stop_recording); a video without frames is skipped with a warning.

public void StopRecording()

Exceptions

AssertionError

Thrown when nothing is being recorded: stop_recording was called, but no recording was started.