def close(self):
#frame_duration = float(1) / self.frames_per_sec
frame_duration = .5
# Turn frames into events: clear screen beforehand
# https://rosettacode.org/wiki/Terminal_control/Clear_the_screen#Python
# https://rosettacode.org/wiki/Terminal_control/Cursor_positioning#Python
clear_code = six.b("%c[2J\033[1;1H" % (27))
# Decode the bytes as UTF-8 since JSON may only contain UTF-8
events = [ (frame_duration, (clear_code+frame.replace(six.b('\n'),six.b('\r\n'))).decode('utf-8')) for frame in self.frames ]
# Calculate frame size from the largest frames.
# Add some padding since we'll get cut off otherwise.
height = max([frame.count(six.b('\n')) for frame in self.frames]) + 1
width = max([max([len(line) for line in frame.split(six.b('\n'))]) for frame in self.frames]) + 2
data = {
"version": 1,
"width": width,
"height": height,
"duration": len(self.frames)*frame_duration,
"command": "-",
"title": "gym VideoRecorder episode",
"env": {}, # could add some env metadata here
"stdout": events,
}
with open(self.output_path, 'w') as f:
json.dump(data, f)
评论列表
文章目录