Tuesday 26 April 2016

Music from terminal with afplay on Mac

So you want to play music on your Mac but you don't want to use iTunes. Open the terminal app (Launchpad => Other => Terminal) cd to the folder of music you want to play, and copy and paste the following:

find * | grep "mp3$" | while read; do echo "$REPLY"; afplay "$REPLY"; wait; done

This will play all mp3s in the folder (and subfolders) it is run in. find lists all the files in the folder and subfolders, grep selects those files ending in mp3, the last piece of code plays them one after the other using afplay.

Shuffling


If you don't want to play them in order, you'll need something to shuffle the lines of output from grep. There's a stackoverflow question on this, but I decided to uses shuf. To get shuf using homebrew it's brew install coreutils. On mac by default it's run as gshuf:

find * | grep "mp3$" | gshuf | while read; do echo "$REPLY"; afplay "$REPLY"; wait; done

And there you have it. A simple way to play your mp3s using the terminal. Now I just need a way to pause and play again ...

No comments:

Post a Comment