Creating mp4 from vnd.openai.ansivid?


#1

I’d like to create mp4 files from the json’s that get created when doing text base environments.

From:

{"height": 6, "stdout": [[0.5, "\u001b[2J\u001b[1;1H\r\n\u001b[41mS\u001b[0mFFF\r\nFHFH\r\nFFFH\r\nHFFG\r\n"], [0.5, "\u001b[2J\u001b[1;1H  (Down)\r\n\u001b[41mS\u001b[0mFFF\r\nFHFH\r\nFFFH\r\nHFFG\r\n"], [0.5, "\u001b[2J\u001b[1;1H  (Left)\r\n\u001b[41mS\u001b[0mFFF\r\nFHFH\r\nFFFH\r\nHFFG\r\n"], [0.5, "\u001b[2J\u001b[1;1H  (Right)\r\nSFFF\r\n\u001b[41mF\u001b[0mHFH\r\nFFFH\r\nHFFG\r\n"], [0.5, "\u001b[2J\u001b[1;1H  (Up)\r\nSFFF\r\n\u001b[41mF\u001b[0mHFH\r\nFFFH\r\nHFFG\r\n"], [0.5, "\u001b[2J\u001b[1;1H  (Up)\r\nSFFF\r\n\u001b[41mF\u001b[0mHFH\r\nFFFH\r\nHFFG\r\n"], [0.5, "\u001b[2J\u001b[1;1H  (Up)\r\nSFFF\r\n\u001b[41mF\u001b[0mHFH\r\nFFFH\r\nHFFG\r\n"], [0.5, "\u001b[2J\u001b[1;1H  (Down)\r\nSFFF\r\n\u001b[41mF\u001b[0mHFH\r\nFFFH\r\nHFFG\r\n"], [0.5, "\u001b[2J\u001b[1;1H  (Left)\r\nSFFF\r\n\u001b[41mF\u001b[0mHFH\r\nFFFH\r\nHFFG\r\n"], [0.5, "\u001b[2J\u001b[1;1H  (Up)\r\nSFFF\r\n\u001b[41mF\u001b[0mHFH\r\nFFFH\r\nHFFG\r\n"], [0.5, "\u001b[2J\u001b[1;1H  (Left)\r\nSFFF\r\n\u001b[41mF\u001b[0mHFH\r\nFFFH\r\nHFFG\r\n"], [0.5, "\u001b[2J\u001b[1;1H  (Left)\r\nSFFF\r\n\u001b[41mF\u001b[0mHFH\r\nFFFH\r\nHFFG\r\n"], [0.5, "\u001b[2J\u001b[1;1H  (Down)\r\nSFFF\r\nF\u001b[41mH\u001b[0mFH\r\nFFFH\r\nHFFG\r\n"]], "title": "gym VideoRecorder episode", "env": {}, "version": 1, "duration": 6.5, "command": "-", "width": 15}

To something like https://gym.openai.com/envs/FrozenLake8x8-v0

Any ideas how to do this?


#2

So far I’m doing it by installing asciinema:

sudo pip3 install asciinema

Then uploading the results as:

from subprocess import check_output

out = check_output(["asciinema", "upload", env.videos[-1][0]])

Then parsing the process output with:

out = out.decode("utf-8").replace('\n', '').replace('\r', '')
castid = out.split('/')[-1]

Finally inserting into the Notebook with:

from IPython.display import HTML

html_tag = """
<script type="text/javascript" 
    src="https://asciinema.org/a/{0}.js" 
    id="asciicast-{0}" 
    async data-autoplay="true" data-size="big">
</script>
"""
html_tag = html_tag.format(castid)
HTML(data=html_tag)

If there is another way of doing this, please let me know.


#3

could you mind explaining more?


#4

What questions exactly do you have?


#5

So arguably there are a finite set of positions on the board, you can make a png of each of the ‘possible positions’ and then just copy and name the ones that match the json in order, then ffmpeg to convert list of pngs to mp4. Only dependency there is ffmpeg.

Ohhh also you can write ‘text’ on an image using pillow, so you could just write the json values to a png and ffmpeg, instead of hand create like I mentioned in my previous paragraph


#6

I don’t have another way, but wanted to say thanks for posting this solution. As a newbie, I just wanted anything to show the animation, and this helped a lot.