Play music in the terminal on Linux/MacOS
Lately, I’ve been vibing to some synthwave tunes, and YouTube is my go-to music source.
Since I spend a lot of time in the macOS terminal, I didn’t want to drain my battery by keeping a browser open just to listen to music.
So, I wrote a short script that uses yt-dlp and mpv:
#!/usr/bin/env 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
Make the script executable:
chmod +x pm.sh
Then alias it in your shell profile for convenience, for example:
alias pm='source ~/.config/scripts/pm.sh "@"'
Now you can run it like this:
pm synthwave 80
Enjoy the tunes! 🎶
--no-video
to your mpv config so you don’t have to pass it each time.
Comments