🎵 Play music in the terminal on Linux/MacOS
tl;dr: use a tiny script with yt dlp and mpv to stream audio from youtube right in the terminal; quick to run, no browser needed.
Lately, I've been vibing to some synthwave tunes. YouTube is my go-to music source. Since I spend a lot of time in the MacOS terminal, I did not want to drain my battery by opening a web browser just to listen to music.
In order to do so I've wrote a short script that uses yt-dlp and mpv
#!/usr/env/bin bash
if [ $# -eq 0 ]; then
echo "Usage: pm <SONG_NAME>"
return 1
fi
query="$*"
s_index=1
while true; do
yt-dlp -f bestaudio ytsearch$s_index:"$query" -I $s_index -o - 2>/dev/null | mpv --no-video -
s_index=$((s_index + 1))
done
Don't forget to make the script executable with chmod +x pm.sh and optionally, alias it to your shell profile for convenience, ex:
alias pm='source ~/.config/scripts/pm.sh "@"'
You can then use it simply by running
pm synthwave 80
Enjoy the tunes! 🎶
tip: if you prefer audio only always, add
--no-videoto your mpv config so you do not have to pass it each time.