Skip to content

Instantly share code, notes, and snippets.

@christopherlovell
Last active November 18, 2023 22:22
Show Gist options
  • Save christopherlovell/e3e70880c0b0ad666e7b5fe311320a62 to your computer and use it in GitHub Desktop.
Save christopherlovell/e3e70880c0b0ad666e7b5fe311320a62 to your computer and use it in GitHub Desktop.
display youtube video in jupyter notebook
from IPython.display import HTML
# Youtube
HTML('<iframe width="560" height="315" src="https://www.youtube.com/embed/S_f2qV2_U00?rel=0&amp;controls=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe>')
# Vimeo
HTML('<iframe src="https://player.vimeo.com/video/26763844?title=0&byline=0&portrait=0" width="700" height="394" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe><p><a href="https://vimeo.com/26763844">BAXTER DURY - CLAIRE (Dir Cut)</a> from <a href="https://vimeo.com/dannysangra">Danny Sangra</a> on <a href="https://vimeo.com">Vimeo</a>.</p>')
@bulbuntu
Copy link

bulbuntu commented Mar 14, 2018

If you want to display the video in a separate cell(a magic cell) then the following would work

%HTML
<iframe width="560" height="315" src="https://www.youtube.com/embed/S_f2qV2_U00?rel=0&amp;controls=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe>

See docs for magic cell here.

@Timerise
Copy link

Really good post. Was looking for a video about it and found another interesting method.
You can use it to play music files and showcase images as well.

code below:

from IPython.display import Audio,Image, YouTubeVideo
id='ER4WhBxM8DY '
YouTubeVideo(id=id,width=600,height=300)

Here are two links. The first link sends you to a page explaining all the methods mention previously by all users in this discussion.
The second link is a youtube video of a few of the methods. Thanks again for posting was able to find the resources after viewing your comment.

link 1
https://ipython.org/ipython-doc/3/api/generated/IPython.display.html

link 2
https://www.youtube.com/watch?v=ER4WhBxM8DY

@AlkisPis
Copy link

AlkisPis commented Aug 23, 2018

If I create a PY file with these 2 lines < from IPython.lib.display import YouTubeVideo > and < YouTubeVideo('b_pTxGu2L04') > nothing happens. Which means, something else is needed. I suspect that it works only in Jupiter Notebook files ... Anyway, can someone tell me how to make YouTubeVideo() work?

@AlkisPis
Copy link

Re My previous message: OK, I managed to create an .ipynb file and include the code. It works fine!

@youkaichao
Copy link

when I set the src to be a relative path of a video file in my conputer, it opens a download window.

any idea how to display local video?

@dejanu
Copy link

dejanu commented Oct 20, 2018

Nice

@FarFromBoston
Copy link

FarFromBoston commented Nov 29, 2018

from IPython.display import YouTubeVideo
YouTubeVideo('x4dNIfx6HVs')

when importing the specific video it will appear something like this:
https://www.youtube.com/watch?v=x4dNIfx6HVs
Everything beyond the '=' sign is the video ID that you put into the brackets

Source:
https://developers.arcgis.com/python/sample-notebooks/chennai-floods-analysis/

@willianadamczyk
Copy link

Does anyone know how can I align the video to the center?

@janjagusch
Copy link

A little method that summarizes what has been discussed here:

from IPython.display import YouTubeVideo

def display_yotube_video(url, **kwargs):
    """
    Displays a Youtube video in a Jupyter notebook.
    
    Args:
        url (string): a link to a Youtube video.
        **kwargs: further arguments for IPython.display.YouTubeVideo
    
    Returns:
        YouTubeVideo: a video that is displayed in your notebook.
    """
    id_ = url.split("=")[-1]
    return YouTubeVideo(id_, **kwargs)

display_yotube_video("https://www.youtube.com/watch?v=SFqna5ilqig", width=800, height=600)

@emarquesdelima
Copy link

Quite nice, but how could I do that if I'm running node.js on my Jupyter notebook?

@YogeshMedz
Copy link

Thank you bro :)

@cboychinedu
Copy link

Thank you for this code.

@johnklee
Copy link

johnklee commented Sep 8, 2020

Useful. Thanks!

@andrea-vandin
Copy link

from IPython.display import Audio,Image, YouTubeVideo
id='ER4WhBxM8DY '
YouTubeVideo(id=id,width=600,height=300)

Fantastic, thank you!

@AtomicNess123
Copy link

Im looking for a way to insert a youtube video or any local/remote video into the cells in Jupyter Notebook as part of a presentation (Slide mode). Is this possible?

@Niroznak
Copy link

many thanks

@egy1st
Copy link

egy1st commented Jul 9, 2022

Thank you. Simple and working perfect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment