~smlavine/scripts

ref: 5af0022d2df522079823605c7340e26ab9847202 scripts/src/watchyt -rwxr-xr-x 1.7 KiB
5af0022dSebastian LaVine dotfiles -> scripts 10 months ago
                                                                                
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
#!/bin/sh
# Copyright (c) 2020-2021 Sebastian LaVine <mail@smlavine.com>
# Licensed under the GNU GPLv3. See GPLv3.txt for details.
#
# File:        watchyt
# Description: Watch the given YouTube video in mpv, using youtube-dl.
# Options:     -c        Use the clipboard value as the video URL.
#              -d        Open the video description in less. (don't launch mpv)
# Arguments:   URL of the video to watch

usage()
{
	printf "USAGE: watchyt [-cd] [url]\n"
}

while getopts "cdh" o; do
	case "$o" in
		c) url="$(xclip -selection c -o)" ;;
		d) description="True" ;;
		*) usage; exit ;;
	esac
done

# I didn't know this existed until I read the getopts man page. It's useful!
shift $((OPTIND - 1))

# if there isn't already a url and there is no argument, get a url through a
# dmenu prompt.
if [ "$url" ]; then
	# if there is already a url, then it came from the xclip clipboard.
	# Firefox removes text clipped from it if it closes, so re-write url to
	# the xclip clipboard to counteract this.
	printf "%s" "$url" | xclip -selection c -i
else
	if [ "$#" -eq 0 ]; then
		URL_PREFIX="ytsearch:"
		url="$(dmenu -p 'Search for:' </dev/null)"
		[ "$url" ] && url="$URL_PREFIX$url"
	else
		url="$*"
	fi
fi

# Remove any chars that are not letters, numbers, or punctuation from the url.
url="$(printf "%s" "$url" | tr -cd '[:alnum:][:punct:]' )"

if [ -z "$url" ]; then
	usage
	exit

elif [ "$description" ]; then
	descfile="$(mktemp watchyt.XXXXXXXX -p /tmp)"
	{
		echo "$url"
		youtube-dl --get-title "$url"
		echo "----------------"
		youtube-dl --get-description "$url"
	} | fold -s >> "$descfile"
	st -e less "$descfile"
	rm "$descfile" # Once less is closed, then delete file.

else
	mpv --ytdl-format="bestvideo[height<=?1080]+bestaudio/best" "$url"

fi