sphinxcontrib-youtube (A Sphinx extension to embed videos from YouTube and vimeo)

Installation

poetry add sphinxcontrib-youtube
make update ou poetry export -f requirements.txt --without-hashes  > requirements.txt

Configuration

# https://sphinxcontrib-youtube.readthedocs.io/en/latest/usage.html#configuration
extensions.append("sphinxcontrib.youtube")

Example

..  youtube:: CWSmP3LSILo

Getting started

Demo

This module provides support for including YouTube and Vimeo videos in Sphinx rst documents.

This module defines directives, youtube and vimeo which insert videos from the respective platforms.

They take a single, required argument, wich is the video ID:

..  youtube:: CWSmP3LSILo
.. vimeo:: 148751763

Usage

The referenced video will be embedded into HTML and LaTeX outputs, the behaviour will be different as LaTeX cannot display videos.

Installation

The package is avalaible on pipy and can be installed with pip:

pip install sphinxcontrib-youtube

Configuration

Add « sphinxcontrib.youtube » to the extensions list in conf.py. For example:

extensions = [
    'sphinx.ext.intersphinx',
    'sphinxcontrib.youtube'
]

HTML

By default, the embedded video will be sized for 720p content.

To control this, the parameters aspect, width, and height may optionally be provided:

..  youtube:: dQw4w9WgXcQ
   :width: 640
   :height: 480
..  youtube:: dQw4w9WgXcQ
   :aspect: 4:3
..  youtube:: dQw4w9WgXcQ
   :width: 100%
..  youtube:: dQw4w9WgXcQ
   :height: 200px

To set the alignment of the embedded video’s iframe in the HTML output, an optional align parameter can be specified, similar to the rst image directive:

..  youtube:: dQw4w9WgXcQ
   :align: center

For YouTube « privacy mode », use the directive option :privacy_mode: (and for vimeo, :url_parameters: ?dnt=1):

..  youtube:: dQw4w9WgXcQ
   :privacy_mode:

To start the video at a specific time the parameter « url_parameters » may be used (quotes required for Vimeo videos):

..  youtube:: dQw4w9WgXcQ
   :url_parameters: ?start=43
.. vimeo:: 486557682
   :url_parameters: "#t=0m43s"

When generating the EPUB output, the videos will not be embedded. Instead, a link to the video will be added.

LaTeX

In LaTeX output, the following code will be emitted for the videos:

\sphinxcontribyoutube{https://youtu.be/}{dQw4w9WgXcQ}{?start=43}
\sphinxcontribvimeo{https://player.vimeo.com/video/}{148751763}{"#t=0m43s"}

The user may customise the rendering of the URL by defining this command in the preamble. The captions will be downloaded to the latex folder and can thus be used as images in the .pdf document.

We are using the Vumbnail (vimeo) and get-youtube-thumbnail (youtube) web services to retrieve them. Here is an example of custom command for both the vimeo and the yoututbe output.

This needs to be added in the conf.py file:

# conf.py
# ...
# -- Option for LaTeX output ---------------------------------------------------

# create a custom sphinx output for the youtube and vimeo video
youtube_cmd = r"\newcommand{\sphinxcontribyoutube}[3]{\begin{figure}\sphinxincludegraphics{{#2}.jpg}\caption{\url{#1#2#3}}\end{figure}}" + "\n"
vimeo_cmd = r"\newcommand{\sphinxcontribvimeo}[3]{\begin{figure}\sphinxincludegraphics{{#2}.jpg}\caption{\url{#1#2#3}}\end{figure}}" + "\n"

latex_elements = {"preamble": youtube_cmd + vimeo_cmd}

This example will show the video as a figure using the thumbnail as image and the url as caption (clickable link). This is the one we use for this very documentation. remember that the argument of your command are the following:

  • #1: the platform url

  • #2: the video ID (it’s also the name of the image: #2.jpg

  • #3: the options of the url

If no custom command is set in conf.py, then the default definition is used:

\newcommand{\sphinxcontribyoutube}[3]{\begin{quote}\begin{center}\fbox{\url{#1#2#3}}\end{center}\end{quote}}

This prints a simple link to the video, enclosed in a box. LaTeX support for Vimeo is similar, except that the macro is named sphinxcontribvimeo.