Class RecordVideo<TObs, TAct>
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
TObsThe observation type, passed through.
TActThe 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
envEnv<TObs, TAct>The env to wrap.
optionsRecordVideoOptionsThe options.
Exceptions
- ArgumentNullException
Thrown when
optionsis 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
envEnv<TObs, TAct>The env; it must render images (
rgb_arrayor a*_listmode).videoFolderstringThe folder of the videos, created if needed (a warning when it exists).
episodeTriggerFunc<long, bool>Whether to start recording at an episode (by number from 0); with no trigger at all, CappedCubicVideoSchedule(long).
stepTriggerFunc<long, bool>Whether to start recording at a step (counted across episodes from 0).
videoLengthlongThe frames per video; 0 records whole episodes.
namePrefixstringThe prefix of the video names.
fpsdouble?The frame rate; null takes
metadata["render_fps"], else 30.disableLoggerboolmoviepy's logger switch, kept for Gymnasium's signature.
gcTriggerFunc<long, bool>Whether to collect garbage after a video of an episode; null always does.
videoWriterIVideoWriterThe encoder; null for PngVideoWriter.
Exceptions
- ValueError
Thrown when the env renders no images (no render mode,
humanoransi), with Gymnasium's message.- ArgumentNullException
Thrown when
videoFolderis null.- TypeError
Thrown when
envis null.
Properties
DisableLogger
moviepy's logger switch (disable_logger); the writers print nothing either way.
public bool DisableLogger { get; set; }
Property Value
EpisodeId
The number of the current episode, from 0 (episode_id; -1 before the first reset).
public long EpisodeId { get; }
Property Value
EpisodeTrigger
When to start recording at an episode (episode_trigger).
public Func<long, bool>? EpisodeTrigger { get; set; }
Property Value
FramesPerSec
The videos' frame rate (frames_per_sec).
public double FramesPerSec { get; set; }
Property Value
GcTrigger
When to collect garbage after a video (gc_trigger).
public Func<long, bool>? GcTrigger { get; set; }
Property Value
NamePrefix
The prefix of the video names (name_prefix).
public string NamePrefix { get; set; }
Property Value
RecordedFrames
The frames of the video being recorded (recorded_frames), owned by the wrapper.
public List<NDArray> RecordedFrames { get; }
Property Value
Recording
Whether a video is being recorded (recording).
public bool Recording { get; }
Property Value
SavedKwargs
The recorded constructor arguments (Python's _saved_kwargs).
[PyName("_saved_kwargs")]
public Info SavedKwargs { get; }
Property Value
StepId
The number of the last step, across episodes, from 0 (step_id; -1 before the first).
public long StepId { get; }
Property Value
StepTrigger
When to start recording at a step (step_trigger).
public Func<long, bool>? StepTrigger { get; set; }
Property Value
VideoFolder
The absolute folder of the videos (video_folder).
public string VideoFolder { get; }
Property Value
VideoLength
The frames per video, infinite for whole episodes (video_length).
public double VideoLength { get; set; }
Property Value
VideoName
The current video's name without extension (_video_name), or null.
[PyName("_video_name")]
public string? VideoName { get; }
Property Value
VideoWriter
The encoder the videos go through.
public IVideoWriter VideoWriter { get; set; }
Property Value
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
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
seedBigInteger?The seed, passed on.
optionsIReadOnlyDictionary<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
videoNamestringThe 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
actionTActThe 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.