Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I play a local video in my IPython notebook?

I've got a local video file (an .avi, but could be converted) that I would like to show a client (ie it is private and can't be published to the web), but I can't figure out how to play it in IPython notebook.

After a little Googling it seems that maybe the HTML5 video tag is the way to go, but I don't know any html and can't get it to play.

Any thoughts on how I can embed this?

like image 990
Chris Avatar asked Aug 02 '13 14:08

Chris


People also ask

How do you run a video in a Jupyter Notebook?

To display local video files in the Jupyter notebook, we should use an Html video tag ( <video... ), and set the base64 encoded video file content to the src attribute of the Html video tag ( <video ..... src='data:video/x-m4v;base64,...... ' ). So we should use IPython.

How do I run IPython notebook locally?

Launch a Notebook To launch a Jupyter notebook, open your terminal and navigate to the directory where you would like to save your notebook. Then type the command jupyter notebook and the program will instantiate a local server at localhost:8888 (or another specified port).


2 Answers

(updated 2019, removed unnecessarily costly method)

Just do:

from IPython.display import Video  Video("test.mp4") 

If you get an error No video with supported format or MIME type found, just pass embed=True to the function: Video("test.mp4", embed=True).

Or if you want to use the HTML element:

from IPython.display import HTML  HTML("""     <video alt="test" controls>         <source src="test.mp4" type="video/mp4">     </video> """) 
like image 198
Viktor Kerkez Avatar answered Sep 24 '22 07:09

Viktor Kerkez


Play it as an HTML5 video :]

from IPython.display import HTML

HTML(""" <video width="320" height="240" controls>   <source src="path/to/your.mp4" type="video/mp4"> </video> """) 

UPDATE

Additionally, use a magic cell:

%%HTML <video width="320" height="240" controls>   <source src="path/to/your.mp4" type="video/mp4"> </video> 

and the same applies for audio too

%%HTML <audio controls>   <source src="AUDIO-FILE.mp3"> </audio> 

enter image description here

like image 28
Aziz Alto Avatar answered Sep 21 '22 07:09

Aziz Alto