Skip to content

Instantly share code, notes, and snippets.

@mcthomas
Created August 22, 2024 04:08
Show Gist options
  • Save mcthomas/f51f7235baae83daa7ce53f49f6cf635 to your computer and use it in GitHub Desktop.
Save mcthomas/f51f7235baae83daa7ce53f49f6cf635 to your computer and use it in GitHub Desktop.
Search for and play YouTube videos from your terminal. Requires ddgr and mpv.
yt (){
#!/bin/sh
if [ $1 = "q" ]; then
return
fi
query="'$*'"
isFound=0
url=""
output="$(ddgr -x --np --unsafe site:youtube.com/watch $query)\n"
clear
echo "$output"
echo -ne "Input # or search term (q to quit): "
read term
echo "$output" | while read line || [[ -n $line ]];
do
if [ $isFound -eq 1 ]; then
url=$line
break
fi
if [[ $line == *$term". "* ]]; then
isFound=1
fi
done
if [ $isFound = 0 ]; then
yt $term
else
mpv $url
yt $query
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment