ELisp: Embed Youtube Vid

By Xah Lee. Date: .

This command turns a YouTube URL into a embedded YouTube video on your blog.

(defun xah-html-youtube-linkify ()
  "Make the current line of youtube url into a embeded video.

The line can be any of

 https://www.youtube.com/watch?v=RhYNu6i_uY4
 https://www.youtube.com/watch?v=RhYNu6i_uY4?t=198
 https://youtu.be/RhYNu6i_uY4
 https://www.youtube.com/embed/RhYNu6i_uY4
 RhYNu6i_uY4

Here's sample result:

<figure>
<iframe width=\"640\" height=\"480\" src=\"https://www.youtube.com/embed/RhYNu6i_uY4\" allowfullscreen></iframe>
<figcaption>
</figcaption>
</figure>

URL `http://xahlee.info/emacs/emacs/elisp_embed_youtube_vid.html'
Version 2020-08-27 2021-06-05"
  (interactive)
  (let ( $p1 $p2 $inputStr $id $timeStamp )
    (re-search-backward "[ \n]")
    (forward-char )
    (setq $p1 (point))
    (re-search-forward "[ \n]" )
    (setq $p2 (point))
    (setq $inputStr (buffer-substring-no-properties $p1 $p2))
    (setq $timeStamp
          (if (or
               (string-match "t=\\([0-9]+\\)" $inputStr )
               (string-match "time_continue=\\([0-9]+\\)" $inputStr ))
              (match-string 1 $inputStr)
            ""))
    (setq $id (xah-html-get-youtube-id $inputStr))
    (delete-region $p1 $p2)
    (insert
     (format "

<figure>
<iframe width=\"640\" height=\"480\" src=\"https://www.youtube.com/embed/%s%s\" allowfullscreen></iframe>
<figcaption>
</figcaption>
</figure>

"
             $id
             (if (string-equal $timeStamp "")
                 ""
               (concat "?start=" $timeStamp))))
    (search-backward "</figcaption>" )
    (backward-char 1)))