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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
# ytfzf
A posix script that helps you find Youtube videos (with out API) and opens/downloads using mpv/youtube-dl.
* Thumbnails
* History support
* Download support
* Format selection (and default formats)
* Queue multiple tracks (using fzf multiselection)
Initially this used to be a single line script. But for portability and extensibility I am breaking my vow. If you still are here for the memes then use the line below.
```sh
#!/bin/sh
[ -z "$*" ] || curl "https://www.youtube.com/results" -s -G --data-urlencode "search_query=$*" | pup 'script' | grep "^ *var ytInitialData" | sed 's/^[^=]*=//g;s/;$//' | jq '..|.videoRenderer?' | sed '/^null$/d' | jq '.title.runs[0].text,.longBylineText.runs[0].text,.shortViewCountText.simpleText,.lengthText.simpleText,.publishedTimeText.simpleText,.videoId'| sed 's/^"//;s/"$//;s/\\"//g' | sed -E -n "s/(.{60}).*/\1/;N;s/\n(.{30}).*/\n\1/;N;N;N;N;s/\n/\t|/g;p" | column -t -s "$(printf "\t")" | fzf --delimiter='\|' --nth=1,2 | sed -E 's_.*\|([^|]*)$_https://www.youtube.com/watch?v=\1_' | xargs -r -I'{}' mpv {}
```
# Update log
- Now ytfzf can queue videos using fzf multiselect option. Press tab to select a video. All the videos will be lined up in mpv. Use `>` and `<` to traverse them.
- Make continuous queries with `-s`
- Thumbnails! Using Ueberzug. Inspired by [fontpreview-ueberzug](https://github.com/OliverLew/fontpreview-ueberzug).
- added MacOS support
- Stdin can be taken by using `ytfzf -`, for both fzf and external menu.
# Usage
![Gif](ytfzf.gif)
```
Usage: ytfzf <search query>
-h, --help Show this help text
-t, --thumbnails Show thumbnails (requires ueberzug)
Doesn't work with -H -D
-D, --ext-menu Use external menu(default dmenu) instead of fzf
-H, --choose-from-history Choose from history
-x, --clear-history Delete history
-m, --audio-only <search-query> Audio only (for music)
-d, --download <search-query> Download to current directory
-f <search-query> Show available formats before proceeding
-a, --auto-play <search-query> Auto play the first result, no selector
-r --random-play <search-query> Auto play a random result, no selector
-n, --link-count= <link-count> To specify number of videos to select with -a or -r
-l, --loop <search-query> Loop: prompt selector again after video ends
-s <search-query> After the video ends make another search
-L, --link-only <search-query> Prints the selected URL only, helpful for scripting
Use - instead of <search-query> for stdin
```
* To use dmenu with a custom width
```sh
YTFZF_EXTMENU_LEN=250 ytfzf -D
```
* Videos can be selected using fzf, dmenu or rofi.
## Examples
> Watch to find videos (with out thumbnails)
ytfzf <query>
> Watch to find videos with thumbnails
ytfzf -t <query>
* You can use multiple options together, here are some examples
> Steam audio (music), and prompt as the music finishes
ytfzf -ml <query>
> Download a video from your history
ytfzf -dH
> Open using dmenu in a certain format
ytfzf -fD
If you started watching a video and you wish to change format then
first hit Q to save position and quit mpv, then choose your format using
ytfzf -faH
## Useful mpv key bindings
* Use `f` for full screen
* Use `J` for subtitles (also works with audio, if the music video has subtitles)
* Use `L` for single-loop
# Dependencies
* mpv
* [youtube-dl](https://github.com/ytdl-org/youtube-dl)
* [fzf](https://github.com/junegunn/fzf) (Optional) - for menu
* [jq](https://github.com/stedolan/jq) - to parse json
* [ueberzug](https://github.com/seebye/ueberzug) (Optional) - for thumbnails
> Fzf is optional, you can use external menu (like dmenu) with the `-D` option (no thumbnail support).
> Thumbnails only work with fzf and Ueberzug as of now.
### Arch based
sudo pacman -S jq mpv youtube-dl fzf
> For thumbnails
sudo pacman -S ueberzug
### Debian based
sudo apt install jq mpv youtube-dl fzf
> For thumbnails
pip install ueberzug
> Note youtube-dl is usually outdated in debian repos, I suggest getting it from
* [youtube-dl github](https://github.com/ytdl-org/youtube-dl)
### MacOS
brew install jq mpv youtube-dl fzf
# Installation
```sh
git clone https://github.com/pystardust/ytfzf
cd ytfzf
```
Install with the Makefile
```sh
sudo make install
```
Uninstall with the Makefile
```sh
sudo make uninstall
```
Arch users can install ytfzf from the [AUR](https://aur.archlinux.org/packages/ytfzf-git/)
yay -S ytfzf-git
# Defaults
Defaults can be set by environmental variables.
> Environment variables can be set in many ways. You can add them to you ~/.bashrc if you don't want to mention them every time
For one time settings you can specify the variables as shown
```sh
YTFZF_HIST=0 YTFZF_PREF="bestvideo[height<=?1080]+bestaudio/best" ytfzf <query>
```
> This will not include this video in your history and display it in a resolution no more than 1080p.
## Format
If you prefer to watch Youtube videos in certain option with out the prompting every single time.
```sh
export YTFZF_PREF="22"
```
[Documentation for ytdl formats](https://github.com/ytdl-org/youtube-dl#format-selection)
If the preferred format is not available then, it will go back to auto selection.
## History
On by default. If you don't want history.
```sh
export YTFZF_HIST=0
```
> 0: history off, 1: history on
* This history will be stored in the cache directory as `ytfzf_hst`
You can modify the file location by changing the cache directory
```sh
export YTFZF_CACHE=~/.cache/ytfzf
```
## Loop menu prompt
Off by default. Can be turned on using option `-l`. Or setting
```sh
export YTFZF_LOOP=1
```
This would return you to the video selection prompt after the video is exited/ends.
> To quit the script you can press `ESC` or `^C` in the video selection prompt.
## External menu command (dmenu / rofi)
To use an external menu you will need to pass in the `-D` option
```sh
ytfzf -D
```
By default the external menu is set to dmenu `dmenu -i -l 30`. You can modify to this to rofy by
```sh
export YTFZF_EXTMENU=' rofi -dmenu -fuzzy -width 1500'
```
> I don't use rofi much, I would love to hear from any rofi user on better defaults.
You also may need to modify the width of the output that is being piped into external menu.
Depending on you screen resolution and font size this may need to be modified.
```sh
export YTFZF_EXTMENU_LEN=180
```
or
```sh
YTFZF_EXTMENU_LEN=180 ytfzf -D
```
> WARNING : dmenu doesn't behave well with some fonts. Expect it to be slow with fonts you don't have.
## Currently Playing
On by default. Stores the details of the currently playing track. Empty when nothing is playing. This could be used in status bar modules.
To disable it
```sh
export YTFZF_CUR=0
```
It will be stored in the ytfzf cache directory as `ytfzf_cur`
## Custom Player
By default, ytfzf uses `mpv`. custom player should have the ability to launch youtube links.
``` sh
# example: using devour
export FZF_PLAYER="devour mpv"
export YTFZF_PLAYER_FORMAT="devour mpv --ytdl-format="
```
# Todo
* [ ] Playlists
* [ ] Comments
* [x] Icons
## Bugs
* dwm with swallow patch: Images don't render when looped (ie, option -l)
|