How to render .bk2 files on Windows.
This tutorial is an follow-up on Docker Settings tutorial:
After creating and running our local environment we want to see the results of our agent playing the game.
I am not sure if there is another possibility playing .bk2 files on Windows than through python script. I’ve tried different .bk2 players or codecs but none of them seemed to be working.
After I tried to follow this guide on playback and rendering video : https://github.com/openai/retro
import retro movie = retro.Movie('SonicTheHedgehog-Genesis-GreenHillZone.Act1-0000.bk2') movie.step() env = retro.make(game=movie.get_game(), state=retro.STATE_NONE, use_restricted_actions=retro.ACTIONS_ALL) env.initial_state = movie.get_state() env.reset() while movie.step(): keys =  for i in range(env.NUM_BUTTONS): keys.append(movie.get_key(i)) _obs, _rew, _done, _info = env.step(keys)
and calling with script
python scripts/playback_movie.py SonicTheHedgehog-Genesis-GreenHillZone.Act1-0000.bk2
This solution didn’t really work.
I can guess that this solution could work on mac or Linux.
After some research i found this tutorial:
This solution I guess would work on mac, but if you are a Windows user we get this error:
assert not pass_fds, "pass_fds not supported on Windows." AssertionError: pass_fds not supported on Windows.
The thing is we actually don’t need to export it to .mp4 file. We just can run the script and run it as playback.
So… what works?
Copy this script and call it “render.py”
#!/usr/bin/python import sys import retro from os import listdir from os.path import isfile, join, isdir def render(file): movie = retro.Movie(file) movie.step() env = retro.make(game=movie.get_game(), state=retro.STATE_NONE, use_restricted_actions=retro.ACTIONS_ALL) env.initial_state = movie.get_state() env.reset() frame = 0 framerate = 2 while movie.step(): if frame == framerate: env.render() frame = 0 else: frame += 1 keys =  for i in range(env.NUM_BUTTONS): keys.append(movie.get_key(i)) _obs, _rew, _done, _info = env.step(keys) env.close() if isdir(sys.argv): onlyfiles = [f for f in listdir(sys.argv) if isfile(join(sys.argv, f))] onlyfiles.sort() for file in onlyfiles: if ".bk2" in file : print('playing', file) render(sys.argv+file) else: print('playing', sys.argv) render(sys.argv)
Put it in scripts folder.
And than call this in your bash:
python ./scripts/render.py ./results/bk2/SonicTheHedgehog-Genesis-GreenHillZone.Act1-0001.bk2
PS: You get the results folder and .bk2 files after you run your local evaluation.
If everything works fine you get your script running:
This way we rendered only one file.
If you want to render whole folder on one run just run it this way:
python ./scripts/render.py ./results/bk2/
The script is running on frame rate with value 2. If you want to make the playback slower or faster just change the frame rate value to 1 (slower) or 5 (faster).
Is there a possibility to render the .bk2 files into .mp4 files to upload on YouTube?
I didn’t find the solution yet. But I can guess that if you edit this script:
and find a way to render it without using pass_fds than you can render the .bk2 files to .mp4 or other format.
That’s all for rendering .bk2 files with windows. Thanks for reading!