LINUXUSER Command Line: Tools

Music tools for the command line ROCKROCK AROUNDAROUND THETHE SHELLSHELL

You don’t always need a GUI- based tool to play MP3 and tracks. In this month’s col- umn, we look at tools for rocking from the shell. BY HEIKE JURZIK www.sxc.hu

f you have a fast machine, players [2] player. There is no difference mat for streaming applications, Ogg pro- such as or JuK are not only between the major features of these vides a container for codecs such as Vor- Ia treat for your ears – they also give tools, so the options we will be looking bis and FLAC. The Ogg Vorbis codec for you lots of eye candy. But even if you at apply equally to both. lossy audio compression [3] also offers have an older machine, you don’t need The simplest way of calling the pro- superior quality to MP3. The vorbis-tools to do without music while you work. gram is to type mpg123 file. or program package, which most distribu- Thanks to small footprint command line mpg321 file.mp3. While happily output- tions now include by default, has a use- players for MP3 and Ogg Vorbis, a text- ting sound through your speakers, the ful selection of tools: based console is all you need. program writes additional information • ogg123: A command line player, The other advantage these programs on the console (Listing 1). Besides pro- which works in a similar way to offer is that they integrate nicely with gram version information, the output mpg123 or mpg321. other shell commands. You can search includes information such as the title, • oggdec: A simple decoder that con- your disk for sound files and automati- artist, and album (assuming the ID3 tags verts Ogg Vorbis files to WAV. cally feed the results into a . And are set correctly), as well as the MP3 file • oggenc: Converts WAV and AIFF files if you are converting to WAV format – to quality. If this is too much information to Ogg Vorbis streams. put a collection of tracks on an audio for your liking, you can prevent this out- • ogginfo: Displays information about CD, for example – the command line is put by setting the -q flag. Ogg files at the command line. definitely preferable to any GUI. To interrupt the player, press [Ctrl]+ • vcut: Cuts Ogg Vorbis files. [Z] to send the process to the back- • vorbiscomment: Supports editing of Light Music with mpg123 ground. Typing fg moves the process Vorbiscomment tags. and mpg321 back to the foreground and continues The ogg123 player is all you need to play mpg123 [1] is a command line tool that where you left off. To quit the command an Ogg Vorbis file. Listing 2 shows sam- goes back quite a long way, having line player, press [Ctrl]+[]. ple output. Again, the normal process served for years on both and Unix control commands apply: [Ctrl]+[Z] platforms. As mpg123 was not released Talking about Ogg allows you to pause the output, fg con- under the GPL, Joe Drew developed a Ogg is an alternative to the MP3 format. tinues playing, and [Ctrl]+[C] quits the completely free alternative – the mpg321 As a completely open, non-patented for- player. Just like mpg123 and mpg321,

86 ISSUE 56 JULY 2005 WWW.LINUX - MAGAZINE.COM Command Line: Music Tools LINUXUSER

you can suppress the informational out- might like to call find to locate those ply play the tracks in sequence but uses put by specifying the -q parameter. audio files: a random play (shuffle) mode instead. As mpg123 and mpg321 understand the At One Fell Swoop mpg321 $(find ~ -name "*.mp3") same parameters, this trick also works To play multiple files back to back with for MP3 files. mpg123, mpg321 or ogg123, simply This find command starts in your home specify the filenames when calling the directory (indicated by the tilde), and Converted command: searches all sub-directories for files with To store your MP3 collection on a nor- the .mp3 extension. The dollar sign and mal audio CD, which you can then play mpg321 U the parentheses tell bash to pass the out- on your stereo, for example, you first 05_Lily_Of_The_Valley.mp3 U put from this command as an argument need to convert the compressed sound 08_Stone_Cold_Crazy.mp3 U to mpg321. files to WAV format. mpg123 and 09_Dear_Friends.mp3 Things start to get more tricky if your mpg321 give you the -w option for this. files include blanks or non-standard Note that you need to specify the output or characters; the typical result is a storm file before the input file: of protest in the form of error messages. ogg123 U But a simple shell trick will help set $ mpg321 -w file.wav file.mp3 01_Barenaked_Ladies_-U things right: ... _Celebrity.ogg 02_U [3:47] Decoding of U Barenaked_Ladies_-U find ~ -name "*.ogg" U file.mp3 finished. _Maybe_Katie.ogg ... | ogg123 -@ - -z As you can only process a single file in Of course you can use typical command The find command runs first; any files it this way, it is a good idea to call mpg321 line shortcuts and specify things like finds are piped to the ogg123 call, which within a for loop, thus automating the mpg321 *.mp3 to play all MP3 files (or reads from standard input (-) and edits conversion procedure for multiple files. ogg123 *.ogg for all Ogg Vorbis files) the list of files as a playlist (as specified To convert all the in a directory to below the current directory. Pressing by the -@ option.) The final parameter, WAV, you would type: [Ctrl]+[C] interrupts the current track -z, makes sure that ogg123 does not sim- and allows you to skip to the next track. for i in *.mp3; do mpg321 -w U To quit the program in this case, you Listing 1: mpg123 Output `basename $i .mp3`.wav $i; done need to press [Ctrl]+[C] twice in quick succession. 01 $ mpg321 01_Keep_ This call assigns every file with the .mp3 If the audio files on your system are Yourself_Alive.mp3 extension to the i variable, and then runs not neatly organized below your home mpg321 -w against the variable. The first 02 High Performance MPEG 1.0/2.0/ directory but are instead spread across parameter is again the output file (made 2.5 Audio Player for Layer 1, subdirectories all over your disk, you up of the base name without the .mp3 2, and 3. extension, and with the .wav extension GLOSSARY 03 Version 0.59q (2002/03/ instead); the second parameter is the 23). Written and copyrights by output file $i. ID3: Audio files (including MP3, Ogg Joe Drew. The command for converting Ogg Vorbis, WMA, and AAC) store meta- information in so-called ID3-Tags. The 04 Uses code from various people. Vorbis files to WAV format is not as long- ID3 tag standard describes two variants: See 'README' for more! winded: the oggdec program automati- cally replaces file extensions, and simply ID3v1 and ID3v2.x. ID3v1 tags restrict 05 THIS SOFTWARE COMES WITH ABSOL expects the file to be decoded: the meta-data to a 128 byte block with a UTELY NO WARRANTY! USE AT YOUR fixed structure at the end of the file. In OWN RISK! contrast to its predecessor, ID3v2 (ver- $ oggdec sunrise.ogg sion number 2.4 is current, and thus the 06 Title : Keep Yourself Alive OggDec 1.0 full name is ID3v2.4) no longer places Artist: Queen Decoding "sunrise.ogg" U the tags in the last few bytes, but pre- 07 Album : Queen to "sunrise.wav" ceding the audio data. The information Year : 1973 [100.0%] can take up to 256MBytes and is orga- nized as a series of frames (which can 08 Comment: be up to 16MBytes.) ID3v2 tags can store Genre : Rock To convert all files at a single pass, sim- a lot more information (even images), ply type oggdec *.ogg. 09 and additionally support the Unicode character set. The two ID3 standards are 10 Playing MPEG stream from 01_ Terminal Ghetto Blaster not mutually exclusive; an application Keep_Yourself_Alive.mp3 ... You may have noticed that the command accessing the meta-information will 11 MPEG 1.0 layer III, 128 kbit/ line tools we have looked at thus far lack simply choose the tag variant it under- s, 44100 Hz joint-stereo interactive controls. If you need an stands. application that gives you these controls

WWW.LINUX - MAGAZINE.COM ISSUE 56 JULY 2005 87 LINUXUSER Command Line: Music Tools

without the overhead of a GUI, mp3blaster [4] may be just what you are looking for. As the name suggests, the player can handle MP3s, WAV, and Ogg Vorbis files; the player is included with most distributions. mp3blaster does not require a GUI environment; thanks to Ncurses, it is quite happy to run in a terminal window and gives you push button controls. To launch the player, type mp3blaster at the command line. The available key- board shortcuts are displayed at the top right of the window; [+] and [-] browse to the right or left in the list to allow you to view the other commands. mp3blaster gives you really informative output when playing audio files, including tech- nical details about the file itself, the play Figure 1: The integrated help feature describes the major functions. sequence, and the next song on the play- list. You can type a question mark (?) to To play a file, use the arrow keys to automatically adds all the songs on the display or hide the integrated help navigate to that file and then press Enter. list to your current playlist. (Figure 1). If you would like to select multiple It is just as easy to set up a new collec- Pressing [F1] takes you to file manager tracks and place them in a playlist tion. To store the current selection of mode, where you can select files to play. instead, press the space key to select a files as a playlist, first press [F4], then This also changes the keyboard short- track (the track entry is highlighted), type a name for the playlist (adding the cuts at the top of the window. The mid- and then press [F1] to add your selec- file extension .lst or .m3u) and press dle of the window shows the files and tions to the playlist. You can select a [Enter] to confirm. If you need to tidy up directories in the folder where you whole directory quickly by inverting the or sort the list before you start, you can launched the player, and you can use the selection ([F2]); this option automati- press [D] to delete a song from the list. arrow keys or [Pg Up] and [Pg Dn] to cally selects all the tracks in that direc- Pressing [M] or [Shift]+[M] changes the navigate. To change to a directory, sim- tory, allowing you to press [F1] to add order of the tracks on the list. And you ply press the enter key; selecting the ../ them to your playlist. can completely clear a playlist by press- entry takes you up one level. Press the [5] key to start playing. ing [C]. mp3blaster displays the current track Listing 2: ogg123 Output and the next entry on the list (Figure 2). Off to a Good Start The other keyboard shortcuts are [5] mp3blaster has a few command line 01 $ ogg123 01_Barenaked_Ladies_ (pause), [4] (previous track), [6] (next options that allow you to control the -_Celebrity.ogg track), [2] (stop), [1] (rewind) and [3] way the player behaves when it 02 Audio Device: Advanced Linux (fast forward). The [F6] key enables the launches. To automatically load a play- Sound Architecture (ALSA) out repeat function; [F7] selects a random list on launching the player, and to start put play mode (shuffle). The integrated playing back immediately, specify the -a mixer is hidden in the bottom right-hand 03 corner of the window; pressing [T] tog- GLOSSARY 04 Playing: 01_Barenaked_Ladies_ gles between the various devices. You Ncurses: A free C program library, -_Celebrity.ogg can press [<] (quieter) and [Shift]+[<] which includes keyboard and mouse 05 Ogg Vorbis stream: 2 channel, (louder) to set the volume – a percent- input handling, along with multiple win- 44100 Hz age display gives you more information dowing 06 Title: Celebrity on the current volume. And finally, 07 Artist: Barenaked Ladies pressing [Q] quits the program. INFO 08 Genre: Rock Sorting Magic [1] mpg123 homepage: http:// www. mpg123. de/ 09 Date: 2003 If you press [F1] to change to selection [2] mpg321 website: 10 Album: Everything to Everyone mode, the various audio formats are http:// mpg321. sourceforge. net/ 11 Track number: 01 displayed in green by default. And mp3blaster displays , which typ- [3] Ogg Vorbis website: 12 Time: 02:00,12 [01:27,62] of 0 ically have extensions such as .m3u or http:// www. vorbis. com/ 3:27,73 (110,7 kbps) Output .lst, in yellow. If you select a playlist file [4] mp3blaster console player: Buffer 96,9% by pressing the Enter key, mp3blaster wwwwhttp:// www. stack. nl/ ~brama/

88 ISSUE 56 JULY 2005 WWW.LINUX - MAGAZINE.COM Command Line: Music Tools LINUXUSER

option and supply the name of the play- list.

mp3blaster -a queen.lst

To load a list without starting to play, specify -l instead. To enable an infinite loop for the playlist, you can specify -R (for “repeat”):

mp3blaster -a queen.lst -R

Besides passing a playlist to the player, you can also specify multiple filenames. After playing these songs, mp3blaster will automatically quit. To prevent this from happening, you can add the -q parameter. Figure 2: mp3blaster displaying the next track on the playlist. Fully Automatic You can modify mp3blaster’s appearance located in /usr/ share/ mp3blaster/ , how- to enable a feature. Besides the pro- and behavior through the hidden config- ever, Debian users will need to look for a gram’s general behavior, you can rede- uration file in your own home directory packed file in /usr/ share/ doc/ fine the color scheme or even change the (~/ .mp3blasterrc.) The program is sup- mp3blaster/ examples/ . keyboard shortcuts. Check out the man- plied with a sample file that you can The text file has a list of commented page for the player (man mp3blaster) for modify with any text editor. The sample. entries, each line starting with a pound more tips and tricks on designing your mp3blasterrc template file is normally sign (#.) You can remove the pound sign own ~/.mp3blasterrc. ■

WWW.LINUX - MAGAZINE.COM ISSUE 56 JULY 2005 89