Code Self Study Forum

How to play music in the terminal with MPlayer and mpv

Here’s a quick guide on how to play music in the terminal with MPlayer or mpv.

MPlayer

Install MPlayer

Something like this should work on Debian-based GNU/Linux distros:

$ sudo apt update
$ sudo apt install mplayer

I’m not sure how to install it on other operating systems (brew on Mac?), but the website has a downloads page.

Play Music

cd into the ~/Music directory (or wherever you keep your music) and type something like:

$ mplayer some_directory/*

or the partial name of an artist with wildcards, like this for Soft Machine:

$ mplayer *Soft*

Playlists

A playlist is just a text file with a list of music files to play. List your media files in a .txt file and then run it like this:

$ mplayer -playlist some_playlist.txt

Controls

and move back and forward in a track.

< and > go to the previous and next tracks.

For more documentation, type this:

$ man mplayer

mpv

Update: I’ve discovered another similar command line music player called mpv.

To play files, pass mpv a list of files, for example, a directory that contains the music files you want to play:

$ mpv some_genre/*

Like in mplayer, playlists are just text files with file names. The format of the command is just slightly different than mplayer’s:

$ mpv --playlist=some_playlist.txt

mpv keybindings

The commands are similar to mplayer’s.

  • 9 and 0 — volume down and up
  • and move back and forward in a track.
  • < and > go to the previous and next tracks.
  • space — toggle pause

The manual is here. A list of configurable keybindings is here.

Tips for Creating Playlists

In both programs, a playlist is just a list of file names to play.

To create playlists quickly, you can use a command like this to list the files of a directory in a text file:

$ ls -1 some_genre/ > some_playlist.txt

Once you have the filenames in a text file, you can rearrange them in the desired order.

Here’s how the command works:

  • ls — list a directory
  • -1 — the number 1 tells the command to only list the file names
  • some_genre/ — this can be the name of a directory that contains your music files
  • > some_playlist.txt — this sends the output of the command into a file

If you want your playlist to use full paths, try something like this instead:

$ ls -1d $PWD/some_genre/* > some_playlist.txt

Explanation:

  • $PWD — this prints the working directory in the output (the full file path to the current directory)
  • -d — this lists the target directory itself

Other Terminal-Based Media Players

There are some other command line players with more features, but I’ve settled on mplayer at the moment, because it’s simple.

2 Likes

I’ve updated the top post with information about a mplayer alternative called mpv.