Endless Parentheses

Concise ramblings on Emacs productivity.

Embedding Youtube videos with org-mode links

The Power of Org-links post series

If you’re a frequent reader, no doubt you noticed an embedded Youtube video on a post a couple of weeks ago. Youtube makes it pretty simple to embed videos, they give you the entire iframe HTML code to use, but this wouldn’t really be Emacs if we couldn’t make things just a little bit easier. Just add the snippet below to your init file, and you’re good to go.

(defvar yt-iframe-format
  ;; You may want to change your width and height.
  (concat "<iframe width=\"440\""
          " height=\"335\""
          " src=\"https://www.youtube.com/embed/%s\""
          " frameborder=\"0\""
          " allowfullscreen>%s</iframe>"))

(org-add-link-type
 "yt"
 (lambda (handle)
   (browse-url
    (concat "https://www.youtube.com/embed/"
            handle)))
 (lambda (path desc backend)
   (cl-case backend
     (html (format yt-iframe-format
                   path (or desc "")))
     (latex (format "\href{%s}{%s}"
                    path (or desc "video"))))))

To use this, just write your org links in the following way (optionally adding a description).

[[yt:A3JAlWM8qRM]]

When you export to HTML, this will produce that same inlined snippet that Youtube specifies. The advantage (over simply writing out the iframe) is that this link can be clicked in org-mode, and can be exported to other formats as well.

Tags: org-mode, init.el, emacs

Say thanks on Gratipay
comments powered by Disqus