blob: 97a889d5063c7d6712c6f098f03a036f7ca29cd5 (
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
|
#!/bin/sh
# Play a youtube song via yt-dlp, jq, fzf and play it with mpv or mpd.
tsv="/tmp/ytsearch.tsv"
json="/tmp/ytsearch.json"
# This is the preview code for fzf
if [ "$1" = "-p" ]; then
grep "^$2\s" "$tsv" |
cut -f 4,5,6 | tr '\t' '\n' |
while read -r channel; do
read -r timestamp
read -r views
printf '\033[2m%s\033[0m\n' "$channel"
printf '%s views\n' "$(numfmt --to=iec "$views")"
printf '\033[3m%s\033[0m\n' "$(date -d @"$timestamp" +'released on %d/%m/%y at %T')"
done
exit
fi
# Whether to pass th --no-video flag to mpv
if [ "$1" = "-n" ]; then
noVideoFlag=1
shift
fi
# Whether to add the url to mpd
if [ "$1" = "-m" ]; then
toMPDFlag=1
shift
fi
q="$@"
if [ -z "$q" ]; then
>&2 printf 'Usage: %s [-nm] query\n' "$0"
exit 1
fi
>&2 printf 'Searching..\n'
yt-dlp -j "ytsearch5:$q" | jq > "$json"
jq -r '[.id,.title,.original_url,.channel,.timestamp,.view_count] | @tsv' "$json" > "$tsv"
choice="$(cut -f 1,2,6 "$tsv" |
sort -t ' ' -k 3 |
cut -f 1,2 |
column -t -s ' ' |
fzf --with-nth "2.." --preview "$0"' -p {1}')"
[ "$choice" ] || exit 1
choice="${choice%% *}"
url="$(grep "^$choice " "$tsv" | cut -f 3)"
[ "$url" ] || exit 2
rm -f "$tsv" "$json"
if [ "$toMPDFlag" ]; then
>&2 printf 'Adding to mpd..\n'
mpc --no-resume-playback add "$(yt-dlp --prefer-insecure -g -f140 "$url")"
elif [ "$noVideoFlag" ]; then
mpv --no-resume-playback --no-video "$url"
else
mpv --no-resume-playback "$url"
fi
>&2 printf 'Done.\n'
|