Displayed video in Jupyter Notebook is unplayable

Viewed 593

I'm trying to embed a video on my local drive in Jupyter Notebook.

The file name is "openaigym.video.6.7524.video000000.mp4" and it is in a folder "gym-results".

Using the following code produces nothing whatsoever:

from IPython.display import Video
Video("./gym-results/openaigym.video.4.7524.video000000.mp4",embed =True)

If I try to use HTML directly (which I got from here), it produces an unplayable video:

from base64 import b64encode
def video(fname, mimetype):
    from IPython.display import HTML
    video_encoded = b64encode(open(fname, "rb").read())
    video_tag = '<video controls alt="test" src="data:video/{0};base64,{1}">'.format(mimetype, video_encoded)
    return HTML(data=video_tag)

path= f"./gym-results/openaigym.video.6.7524.video000000.mp4"
video(path, "mp4")

That is, it produces the following:

enter image description here

Which cannot be started. How do I solve this?

1 Answers

First method: it worked with me!

enter image description here

Second method: You could try this as well:

from ipywidgets import Video
Video.from_file("./play_video_test.mp4", width=320, height=320)

enter image description here

Third method: you should change the type of the cell from code to Markdown

<video controls src="./play_video_test.mp4">animation</video>

If all solutions do not work with you, I recommend you to update your jupyter notebook by conda update jupyter or pip install -U jupyter and go through each solution one more again.

Related