Two weeks ago I wrote about saving timestamps in YouTube links. It turned out that there is a much, much better solution to the problem I was having. First of all, the famous youtube-dl (or more precisely, its fork yt-dlp, which seems to be much faster) not only can download videos from various sites, but also stream them. Just install it and run mpv <link>
to use it under the hood. Also, mpv
allows saving the position you were on and resume playing from there. All you need to do is to use S-q
(that is, shift-q
) to quit and the position will be remembered for you. And it gets even better. Open the file ~/.config/mpv/mpv.conf
in your Emacs and put the line saying save-position-on-quit
there. Then the position is saved even if you quit with q
. (You can use the option --no-resume-playback
to start playback from the beginning again.)
Now the only thing that’s left is to make Org mode (or rather Emacs) use this knowledge. It turns out that Org mode delegates the task of actually opening the link to Emacs, although this is very hard to find. Looking at the variable org-link-parameters
reveals nothing, since it uses compiled anonymous functions to open http
and https
links (and also several more). I grepped the Org-mode sources to find this piece of code.
(dolist (scheme '("ftp" "http" "https" "mailto" "news")) (org-link-set-parameters scheme :follow (lambda (url arg) (browse-url (concat scheme ":" url) arg))))
So it seems that the best course of action is to make sure browse-url
knows how to handle YouTube links. And that in turn means checking out browse-url-handlers
.
(require 'mpv) (defun youtube-url-p (url) "Return t if URL points to YouTube." (string-match-p "https://\\(www.\\)?youtube.com\\|https://youtu.be" url)) (defun browse-youtube-url-with-mpv (url) "Open URL using mpv." (mpv-start url "--full-screen")) (push '(youtube-url-p . browse-youtube-url-with-mpv) browse-url-handlers)
(If you don’t want to install the mpv
package, you can just use e.g. start-process
to launch mpv
.)
That’s it! I can now enjoy my YouTube links in mpv, and resume them where I last stopped viewing – and all that with hardly leaving Emacs!