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

Solution 1:

(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>
""")

Solution 2:

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

Solution 3:

Use a markdown cell:

<video controls src="path/to/video.mp4" />

Citation: Jupyter Notebook » Docs » Examples » Markdown Cells

Solution 4:

An easier way:

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

Solution 5:

@Atcold's comment saved me today ;) so I'm posting this as an answer with more detail.

I had a cell with video capture command like this:

!sudo ffmpeg -t 5 -s 320x240 -i /dev/video0 /home/jovyan/capture.mp4

captured file was saved in a location out of git repository to manage disk usage.

for jupyter notebook, a file needs to be on the same directory as the .ipynb file.

# run this before calling Video()
! ln -sf "/home/jovyan/capture.mp4" ./capture.mp4
from IPython.display import Video

Video("capture.mp4")

voila! Thank you everyone for the wonderful answers and comments.