#!/bin/sh -e
#
# Usage:
# ytpl song title
# ytpl < titles
#
go() {
echo "Playing $1"
# Force TTY input for controls even if titles are read from input
echo https://youtu.be/ > tmp1.txt
youtube-dl --get-id "ytsearch:$1" > tmp2.txt
youtube-dl --get-title "ytsearch:$1" > tmp4.txt
paste -d" " tmp1.txt tmp2.txt > tmp3.txt
paste -d" " tmp3.txt tmp4.txt > tmp5.txt
sed 's/ //' tmp5.txt > tmp6.txt
cat tmp6.txt
mpv --vid=no "$(youtube-dl -g "ytsearch:$1" | tail -1)" < /dev/tty
rm tmp*.txt
}
# All arguments as a single space separated string
if [ -n "$*" ]; then
go "$*"
exit
fi
# Read one title per line
while read title; do
go "$title"
done