Originally Published: Monday, 8 October 2001 Author: Michael & Melinda Petruzziello
Published to: learn_articles_firststep/General Page: 2/3 - [Printable]

Notes from the Command-Line Commando: Multimedia

Linux.com irregulars Michael & Melinda Petruzziello return this week with another installment of Command Line Commando, your regular view of working with the Linux command line especially for newer users. This week we take a look at playing music and video on your Linux box, even streaming MP3s from, well, the command line, of course!

mpg123  << Page 2 of 3  >>

Getting Started With mpg123

Mpg123 is a great utility for playing MP3 music files and streaming audio. Mpg123 supports MPEG Layer 1, 2 and 3, and will play Layer 3 in stereo. Layer 2.5 may also play, but has not been heavily tested. This program allows you to speed up or slow down your mp3s, mix down stereo streams (to use less bandwidth), and even supports playlists.

First, let's check and see if you have mpg123 installed on your system. To do this, go to your command prompt and type:

rpm -q mpg123 <Enter>

If mpg123 is installed, you should see something similar to the following:

mpg123-0.59r-8

If not, you might see:

package mpg123 is not installed

If the reply comes back "package not installed," then you will need to install it. Remember, we are using RedHat 7.1. For us, mpg123 was included on the installation CD. If you are using a different distribution, your mileage may vary. Check your CD, or take a quick look on the web by going to http://www.mpg123.de/. Our instructions should, at least, put you on the right track. ("right track," like a CD, get it? Nevermind...)

Playing With mpg123

The most basic way to play an MP3 file with mpg123 is to go to your command line and type this:

mpg123 songname.mp3 <Enter>

Voila! Music! (And perhaps you thought it couldn't be done!)

Say you want to hear more than one song. To play two songs in a row, type this:

mpg123 songname1.mp3 songname2.mp3 <Enter>

Isn't this fun? If you want to hear 20 songs in a row, just type them out on your command line, every time you want to hear them! (If you think this sounds highly annoying, pay close attention to the next section.)

Playlists

Remember how we mentioned that mpg123 supports playlists? We weren't kidding you. Here's how you do it. Use your favorite text editor (pico, vi, emacs, et al) to create a file with the song files you want to hear, listing one file per a line. Here's sample instructions for making a playlist file using vi:

vi mytunes.m3u <Enter>

<i>

/home/username/mp3z/myfav/JohnTesh1.mp3 <Enter>
/home/username/mp3z/myfav/JohnTesh2.mp3 <Enter>
/home/username/mp3z/myfav/JohnTesh3.mp3 <Enter>
<Esc> :wq <Enter>

A few things to note: You must include the entire path when you specify files in your playlist. We chose to use the .m3u extension for our playlist file because other MP3 players such as xmms support that extension for their playlist files. (Portability is what it's all about, right?) To use your playlist, type this:

mpg123 -@ mytunes.m3u <Enter>

mpg123 will now happily play every file in your playlist until it finishes playing the last one. It is worth noting that if you start up mpg123 and you are not in the directory where you keep your playlist, you will need to specify the path to the directory on the command line, like this:

mpg123 -@ /home/username/mp3z/lists/mytunes.m3u <Enter>

Now, say you have all your MP3 files in a single directory, and you have about 300 or so of them. You're inwardly groaning at the thought of having to type out every single one of those file names into a list. Your mind is racing, trying to think whether you can get grep and awk to do all the work for you... Don't sweat it. If you want mpg123 to play every MP3 in a certain directory, just type this:

mpg123 /home/username/mp3z/*.mp3 <Enter>

Whew! Saved by a wildcard!

Random Play

If you, like many, get tired of hearing all 300 of your MP3s play in the same order all the time, shuffle things around a bit by using the "-z" option, or use the "-Z" option for full random play. Now that you're happily listening to your MP3s from your command line, you can use your <Alt> and function keys to switch over to a fresh command line window and keep right on hackin'. (The fun kind, not the illegal kind!)

There are many mpg123 front-end utilities (wrappers) out there that allow you to visually manipulate your playlists and view song titles and other information about the current file being played. Visual displays, and you don't even need an X server! If you're curious and want to learn more about these wrappers, hop on over to your favorite search engine.





mpg123  << Page 2 of 3  >>