Skip to content

[Bug]: VecVideoRecorder overwrites previous video at each save #2061

Closed
@curtiscjohnson

Description

@curtiscjohnson

🐛 Bug

Due to changes that happened with commit #1837 in vec_video_recorder.py, the video_name is no longer updated at each recording, which leads to the previous video being overwritten as self.video_name every time.

To Reproduce

import gymnasium as gym
from stable_baselines3.common.vec_env import VecVideoRecorder, DummyVecEnv
from stable_baselines3 import PPO
import glob


def test_video_save():
    env_id = "CartPole-v1"
    video_folder = "videos"
    video_length = 100

    vec_env = DummyVecEnv([lambda: gym.make(env_id, render_mode="rgb_array")])

    # Record the video starting at the first step
    vec_env = VecVideoRecorder(vec_env,
                               video_folder,
                               record_video_trigger=lambda x: x % 1024 == 0,
                               video_length=video_length,
                               name_prefix=f"agent-{env_id}")

    vec_env.reset()

    model = PPO("MlpPolicy", vec_env, verbose=0, device="cpu")

    model.learn(total_timesteps=2048)

    #print all videos in video_folder, should be multiple step 0-100, step 1024-1124
    video_files = glob.glob(f"{video_folder}/*.mp4")

    # Clean up
    vec_env.close()

    print(f"All video files saved in {video_folder}: {video_files}")


if __name__ == '__main__':
    test_video_save()

Relevant log output / Error message

Moviepy - Building video /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4.
Moviepy - Writing video /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4

Moviepy - Done !                                                                                        
Moviepy - video ready /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4
Saving video to /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4
Moviepy - Building video /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4.
Moviepy - Writing video /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4

Moviepy - Done !                                                                                        
Moviepy - video ready /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4
Saving video to /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4
Moviepy - Building video /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4.
Moviepy - Writing video /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4

Moviepy - Done !                                                                                        
Moviepy - video ready /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4
Moviepy - Building video /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4.
Moviepy - Writing video /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4

Moviepy - Done !                                                                                        
Moviepy - video ready /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4
All video files saved in videos: ['videos/agent-CartPole-v1-step-0-to-step-100.mp4']

System Info

  • OS: Linux-5.15.0-124-generic-x86_64-with-glibc2.29 # 134~20.04.1-Ubuntu SMP Tue Oct 1 15:27:33 UTC 2024
  • Python: 3.8.10
  • Stable-Baselines3: 2.4.0
  • PyTorch: 2.4.1+cu121
  • GPU Enabled: True
  • Numpy: 1.24.3
  • Cloudpickle: 3.1.0
  • Gymnasium: 1.0.0

Checklist

  • My issue does not relate to a custom gym environment. (Use the custom gym env template instead)
  • I have checked that there is no similar issue in the repo
  • I have read the documentation
  • I have provided a minimal and working example to reproduce the bug
  • I've used the markdown code blocks for both code and stack traces.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions