aproeme / youtube

A Sphinx extension to embed videos from YouTube

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sphinxcontrib.youtube

license PyPi version

Overview

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:: dQw4w9WgXcQ
.. vimeo:: 148751763

Usage

The referenced video will be embedded into HTML and Latex outputs, the behaviour will be slighly different for obvious reasons.

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%

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

..  youtube:: dQw4w9WgXcQ
    :privacy_mode:

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

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:: 148751763
   :url_parameters: "#t=0m43s"

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. 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.

About

A Sphinx extension to embed videos from YouTube

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Python 100.0%