Remotely control your tunes with MPD and ncmpc.
Linux has several good music players (RealPlayer, Totem, Banshee), some great music players (XMMS, Rhythmbox), and one awesomely fantastic music player that beats those found on any other operating system: Amarok. Those are all very cool programs, with one thing in common: a GUI.
So what if you don’t want or need a GUI? What if you want to control the music your computer is playing from across the room? What if you want the ultimate in speed and stability? Then you want a music player that was built for the command-line. You want the one-two, knock-out combination of MPD (Music Player Daemon) and ncmpc.
Start by installing MPD. You can get the source code from http://www.musicpd.org/files.shtml, or via your distro’s repository. Debian and K/Ubuntu users, for instance, can simply use apt-get install mpd.
Once MPD is installed, you need to create a configuration file. If you control your machine, you’ll want it to end up at /etc/mpd.conf; if you can’t create that file, or you’d rather have it in your home directory, create ~/.mpdconf. The easiest way to create the file is to copy mpdfconf.example to the new location and then edit the new copy. The example configuration file is very well commented, so you should be able to figure things out. Nonetheless, here’s a useful /etc/mpd.conf file:
music_directory "/home/scott/music"
playlist_directory "/home/scott/.mpd/playlists"
db_file "/home/scott/.mpd/mpd.db"
log_file "/home/scott/.mpd/mpd.log"
error_file "/home/scott/.mpd/mpd.error"
pid_file "/home/scott/.mpd/mpd.pid"
state_file "/home/scott/.mpd/mpdstate"
user "scott"
bind_to_address "any"
port "6600"
MPD first scans your music collection and then creates a database listing every file that it found, hence the db_file parameter. The log_file and error_file options are obvious, while the pid_file makes it easier to kill the program if it wigs out. The state_file saves your current playlist, song, and location within that song so that all that can restored when you restart MPD.
To create and store playlists, specify the playlist_directory; be sure to manually create that directory, or MPD wonÃt start like it should the very first time. Specify the user so that if MPD is started by root, it wonÃt run as root, and will instead run as the user you choose. The bind_to_address and port are the defaults, and are used by the programs that connect to MPD and actually play the music indexed by MPD.
Finally, the first parameter, music_directory, points to the location of your music collection. In the case of /home/scott/music, this is actually a symbolic link pointing to an external drive. If you have multiple drives for you collection, simply include a soft link on music pointing to the other drive. MPD happily follows that link to index the songs found on that drive as well.
The first time you run MPD — by typing mpd, naturally — you need to wait while the program indexes all of your music. If you have a large music collection, this could take an hour or more. Once itÃs done, you need to make sure that MPD re-indexes your tunes on a regular basis to discover additions. The command to update the MPD database is mpc update, and if you just added or deleted some music, go ahead and type that in, and then wait. If you want something a bit more regular, add /usr/bin/mpc update as a cron job to your computerÃs nightly task list.
You actually have quite a few choices when it comes to a client to connect to MPD and play music. A nice list can be found at http://www.musicpd.org/clients.shtml. The best CLI client is ncmpc, which can be installed using your package manager or can be downloaded as source from http://www.musicpd.org/ncmpc.shtml.
Start ncmpc with ncmpc and the program should open. If you changed the host and port in your MPD conf file, though, type man ncmpc and read the instructions for connecting. Press the number 1 if you need Help.
Press 3 to browse your musicÃs directory structure. When you find a song you want to hear, use your arrow keys to go to it and press Enter. You should immediately hear it start playing. To add a song to the playlist, press Space instead of Enter. To play the tunes youÃve selected, press 2 to view your playlist and then press Enter. To pause, press Shift-P.
MPD and ncmpc arenÃt for everyone, but the combination beautifully meets the needs of many people. In fact, even if you love Amarok, you may find yourself using ncmpc instead, simply because itÃs faster and simpler, and because you can change the music thatÃs playing without vacating the couch.
Scott Granneman teaches at Washington University in St. Louis, consults for WebSanity, and writes for SecurityFocus and
Linux Magazine. His latest book,
Linux Phrasebook is in stores now. You can reach him at
scott@granneman.com.
Comments on "Music on the Command-Line"
MPD is also in Mandriva’s repositories.
Why not just SSH into another machine and run mplayer?
I hate how software written by amateurs doesn’t abide by good engineering principles — namely KISS.
If you don’t know/haven’t heard of KISS, and you code, please stop right now and give up.
Anything for Solaris guys?
A letter of condolence?
even better — use ncmpcpp — a fork of ncmpc with more features and such
Whoo, *more* features. Must be better then.
In reality, the so-called extra features are just an excuse to justify re-writing in C++ (blergh!).
Even better, use moc instead of mpd. Works more like a typical music player, just on the command-line. No need to set up music repositories and use relative paths from there.
Why? Because running an X app across the network is godforsakenly less efficient than sending back a stream of audio. Because you can’t connect from multiple locations to one playing stream. Because running mplayer across a network requires the user to have a shell acount on the machine. Because it would require X on the user’s machine, which most Windows users won’t have. And yes, this project has Windows clients availabl.
It’s called “this software is more complex and uses better software engineering practices than you apparently understand”. If you haven’t heard about it before, and you code, you should be ashamed of yourself for showing your ass in public like this.