summaryrefslogtreecommitdiff
path: root/yt.sh
blob: 16f7247d1cb8557a211e166fa303f9a67865d34d (plain) (blame)
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
#!/bin/sh
## Do things with a YouTube URL. Assumes a valid YouTube URL. Doesn't do any
## grabbing or scraping, use urlview for that.

## Config:
TERMINAL=alacritty
DOWNLOAD_DIR=~/videos/youtube

## Exit codes
NO_URL=3
NO_ACTION=4

## Get action for a single URL
get_action() {
	action=$(dmenu -p "YouTube" << EOF
1. watch 
2. video download
3. audio download
EOF
	)
	echo "$action" | cut -d'.' -f1
}

## Get title, description, and duration of URL
toast_info() {
	info=$(youtube-dl --get-title --get-description --get-duration "$1")
	title=$(echo "$info" | head -n 1)
	description=$(echo "$info" | sed '1d;$d')
	duration=$(echo "$info" | tail -n 1)
	notify-send "($duration) $title" "$description"
}


## Download video
video_download() {
	mkdir -p "$DOWNLOAD_DIR"
	youtube-dl \
		-q \
		--add-metadata \
		-o "$DOWNLOAD_DIR/%(title)s_%(id)s.%(ext)s" \
		"$1" && notify-send "Video download done." &
	notify-send "Downloading video:"
}


## Download audio
audio_download() {
	mkdir -p "$DOWNLOAD_DIR"
	youtube-dl \
		-q \
		--add-metadata \
		-o "$DOWNLOAD_DIR/%(title)s_%(id)s.%(ext)s" \
		-x \
		--audio-format flac \
		--audio-quality 0 \
		"$1" && notify-send "Audio download done." &
	notify-send "Downloading audio:"
}


## Play video
play() {
	mpv --no-terminal --geometry=25%-10-40 --title="Streaming from YouTube" "$1" &
	notify-send "Playing:" 
}


## Main
[ -n "$1" ] || exit NO_URL
toast_info "$1" &

case "$(get_action)" in
	1)
		play "$1"
		;;
	2)
		video_download "$1"
		;;
	3)
		audio_download "$1"
		;;
	*)
		exit $NO_ACTION
esac