Reputation: 600
I want to record a video of my rollouts of OpenAIs gym. I use the Monitor class, but other solutions are also appreciated. This is a minimal example I created, that runs without exceptions or warnings:
import gym
from gym.wrappers import Monitor
env = Monitor(gym.make('CartPole-v0'), './video', force=True)
state = env.reset()
done = False
while not done:
action = env.action_space.sample()
state_next, reward, done, info = env.step(action)
env.close()
This saves a video and some metadata to the './video'
folder. However, the mp4-file that is saved there has always just a size of 262 byte and the "file contains no playable steams". So it seems it is a 'video' but made up of 0 frames? I don't understand what the problem is and how to debug from here.
Upvotes: 3
Views: 10731
Reputation: 6829
Please ensure you have FFmpeg first.
brew install ffmpeg
One way is to run the code in an Xvfd
virtual display.
The reason for this is video recording, which is done by taking screenshots of the window drawn by the environment. Some of the environments use OpenGL
to draw its picture, so the graphical mode with OpenGL
needs to be present.
This could be a problem for a virtual machine in the cloud, which physically doesn't have a monitor and graphical interface running. To overcome this, there is a special "virtual" graphical display, called Xvfb
(X11 virtual framebuffer
), which basically starts a virtual graphical display on the server and forces the program to draw inside it.
To start your program in the Xvfb
environment, you need to have it installed on your machine (this usually requires installing the xvfb package).
Some resources to download and set it up:
Then run xvfd-run
with the python file and the video should be saved to the directory you specified:
xvfb-run -s "-screen 0 640x480x24" python cartpole_monitor.py
There are other ways such as running the code in an X11 session with the OpenGL extension(GLX)
or using X11
forwarding in an SSH
connection.
Upvotes: 2