summaryrefslogtreecommitdiff
path: root/content/emacs/watching-youtube-with-emacs.md
blob: f4c36a4bd5cb9ebc981f85c546852b47a11528e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
+++
title = "Emacs: watching YouTube with Yeetube and mpv"
author = ["Michał Sapka"]
date = 2024-02-23T16:16:00+01:00
categories = ["emacs"]
draft = false
weight = 2007
abstract = "Let's use YouTube from the comfort of Emacs"
[menu]
  [menu.emacs-guides]
    weight = 2007
    identifier = "emacs-watching-youtube-with-yeetube-and-mpv"
    name = "Watching YouTube"
+++

I may hate YouTube as a service, but I even I can't deny the quality of vlogs there.
Even though I would strongly prefer to follow folks on PeerTube or Odysee, this will not happen anytime soon for most of the channels.
Luckily, with the popularity of YouTube comes quite a few ways to use it in a better way


## Yeetube {#yeetube}

_Yeetube_[^fn:1] is an Emacs wrapper around searching and viewing videos on Youtube.
The watching part happens via `mpv`[^fn:2].
You simply pass the video link (not the page) in the shell and `mpv` will handle the rest.
_Yeetube_ handles everything _before_ we have the actual file, and running `mpv`.

First, let's install it:

```emacs-lisp
(use-package yeetube)
```

And, assuming `mpv` is already installed, you are ready to go.
Run `yeetube-search`, type in whatever you want and press enter.
A table with top results will show up.
Now, select the one that interests you, run `yeetube-play`, wait a few seconds and mpv window will show.

{{< img-c "emacs-yeetube.png" >}}
Yeetube search for Emacs Elements
{{< /img-c >}}

_Yeetube_ also supports downloading videos via `yt-dl` and saving videos for future reference.

My full config, with evil keybindings looks like:

```emacs-lisp
(use-package yeetube
  :general
  (:states 'normal
           :keymaps 'yeetube-mode-map
           "RET" 'yeetube-play
           "d" 'yeetube-download-video
           "b" 'yeetube-play-saved-video
           "B" 'yeetube-save-video
           "x" 'yeetube-remove-saved-video
           "/" 'yeetube-search
           "0" 'yeetube-toggle-video
           ))
```

Note that this comes with no ads, and less tracking, but also less revenue to the creator.
Being a patron is a good way to feel better about it.


## Link handler {#link-handler}

This is nice, but we can make it _extra-nice_.
I subscribe to quite a few YouTube channels via RSS[^fn:3] and want to use _Yeetube_ fast.
We can write a very simple _elisp_ function:

```emacs-lisp
(defun mms-open-yt-under-point ()
  (interactive)
  (setq url (thing-at-point 'url))
  (string-match "youtube.com" url)
```

Now, move the pointer on a YT[^fn:4] link and call this function.
_Yeetube_ interface will open, so just play the top result.

My (current) full function in use is:

```emacs-lisp
(defun mms-open-link-under-point ()
  (interactive)
  (setq url (thing-at-point 'url))
  (cond
   ((string-match "youtube.com" url) (yeetube-search url))
   (t (eww url)))
  )
```

So it will use `eww` for anything that is not a YT link, and I can expand it further whenever I want.

Then, just add a simple keyboard navigation, and you're done

```emacs-lisp
(mms-leader-keys
  "RET RET" '(lambda () (interactive) (mms-open-link-under-point) :wk "follow link"))
```

[^fn:1]: ["yeetube | Emacs Front-End for YouTube"](https://thanosapollo.org/post/yeetube/) blog post from the author
[^fn:2]: [mpv official website](https://mpv.io/)
[^fn:3]: The secret URL: `https://www.youtube.com/feeds/videos.xml?channel_id=<channel_id>`.
    You can find the channel ID using different online services, as it is not as straight forward as it should be.
[^fn:4]: Only if the package registers itself as a provider for `thing-at-point`.
    In Elfeed it will for main item URL, but not for items embedded in the body.
    We need to use `eww` to open the page and we can get the URL from there.