#!/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