RegisterLog In/Log OutView Cart
O'Reilly
web.oreilly.com
BooksSafari BookshelfConferencesO'Reilly NetworkO'Reilly GearLearning Lab
 
advertisement




Scot Hacker's Top 10 Tips for MP3

by Scot Hacker
03/01/2000

1) Set Up an "MP3-on-Demand" Server. There are two kinds of streaming MP3 servers: True streaming and "on demand," or "pseudo-streamers." Genuine MP3 streaming, where your server acts like a radio broadcast station (users hear what you're playing at any given moment) requires quite a bit of setup and configuration, plus special software. MP3-on-Demand, on the other hand, is very simple to set up and can be run from any standard web server. With this method, users will click a link in their browsers and the link will be passed to their preferred MP3 player. The player will handle the download and start playing the file or files as soon as it has enough bits to work with. In addition, a collection of files pre-established by you will appear in their playlists, so they can skip around between songs at will (in other words, pseudo-streaming is asynchronous rather than synchronous).

To set up MP3-on-Demand, you must first have a running web server--any platform and any server software will do. Now, place some legal MP3 files on your site and create a text file listing the full URLs to these files. For example, your text file might look like this:


http://www.mysite.com/mp3/Foolish_Love.mp3
http://www.mysite.com/mp3/Fly_in_the_Buttermilk.mp3
http://www.mysite.com/mp3/Jumping_the_Broom.mp3

Save this file into the site with an .m3u extension, e.g. Blues_Playlist.m3u. M3U stands for "MPEG URL." Finally, check your server's MIME configuration database (consult your server's documentation if you're not sure what this means) to see that the following MIME types are present:


audio/x-mpeg .mp3

audio/x-mpegurl .m3u

Related Reading

MP3: The Definitive Guide
By Scot Hacker

You may need to restart the server to have it recognize the new MIME type. Now, place a link on your site to the M3U file. Users accessing the file should see it passed automatically to their favorite MP3 player, which will display all the files in the list, start playing the first one automatically, and let them switch among files with a single click.

Of course, if you do this, you will need to make sure you have plenty of bandwidth. Whereas real MP3 streaming (covered in detail in MP3: The Definitive Guide) is capable of downsampling files to be bandwidth friendly, this method does not. The bitrate and frequency with which they're encoded is the bitrate and frequency at which they'll be served.

2) Always Use ID3 Tags: Every MP3 file has the ability to store "meta data" within itself in "ID3 tags." With ID3v1, you can store artist, song name, album name, genre, year, and comments. Each of these fields is limited to 32 characters. With ID3v2, you can store up to 256MBs of arbitrary meta-data. ID3v1 is found at the end of the file, while ID3v2 is at the beginning. While ID3v2 is arguably superior, it's still rare, and most MP3 files have ID3v1 data only.

Because MP3 collections grow quickly, you'll soon want to sift and sort your collection--or build playlists--on criteria such as genre, year, artist, etc. Unfortunately, many downloaded MP3 files have no ID3 data at all. If you want to keep a well-organized collection that will work nicely with database solutions you may adopt in the future, the time to give your files ID3 tags is when you download them, or after you've listened to them and decided to keep them. If you wait for the future, you may find yourself facing the daunting task of tagging thousands of files at once.

There are many ways to add or edit ID3 tags. Most graphical MP3 players include a built-in tag editor for this purpose that can be accessed with a hotkey. There are also dozens of dedicated tools and utilities available for this purpose. The best ones allow you to operate in batch mode. This allows you to select a bunch of files by the same artist and give them all the same artist tag at once. If you prefer to work from the command line, get a copy of id3ren, which has two functions: 1) Setting or editing ID3 tags, and 2) Renaming files according to a "template" specified by you. Command line tools like id3ren are especially useful because they can be used from within scripts to operate on hundreds or thousands of files at once.

3) Dealing with Stubborn MP3 Downloads: Every now and then, you'll click a link to an MP3 file and find that your screen fills with what appears to be random ASCII garbage. What's going on here is a failure to communicate. Either the server's MIME database is not configured correctly, or your browser is not configured to handle the MIME type the server is sending, or both.

The first thing to do is to check your browser's preferences for the MPEG Audio filetype. Make sure the type audio/x-mpeg is associated with MP3 files, and that MP3 files are set either to be downloaded or played with your favorite MP3 player.

If you've done this and are still getting screens of garbage, the server is incorrectly configured. To force a download of the file, hold down the Shift key when clicking the link. This will cause the filetype associations to be bypassed and a file panel to appear so you can specify a download directory manually.

You may want to write to the site's webmaster to request that they add the audio/x-mpeg MIME type to their server.

4) Batch Downloads from Multiple Sites: Since downloading lots of MP3 files simultaneously in the background will probably swamp your connection, you'll probably find yourself spending more time waiting for downloads to finish than surfing for files. A better way to go about this is to queue up all your files and let them download while you sleep, or while you go to lunch.

If you're using the BeOS or a Unix-based OS, find yourself a copy of wget. With wget installed in your path, create a text file listing the full URLs to the files you want to download. Save the text file as, say, "get_these". Now, cd to the directory into which you want the files downloaded and type:


wget get_these

wget will attempt to download each file in the list, in order. If you're using Windows, there are a number of download managers you can install. Two of the best are Monica and ReGet, both of which can be found at any software library.

The best way to get the complete URLs to the files you want is to right-click a link and choose "Copy Link Location" from the context menu. You can then toggle to your editor or download manager and paste in the URL.

5) Encode at a Decent Bitrate: The quality of an MP3 file is determined by many factors, but one of the biggest is the bitrate--the number of bits per second allocated to describing the audio in question. The higher the bitrate, the better the quality and the larger the file. Lower bitrates get you smaller files and lower quality sound.

The de facto standard on the Internet is 128kbps, which seems to strike a fairly good balance between reasonable file sizes and decent quality. However, most audiophiles (and this author) feel that 128kbps isn't good enough to get you CD quality audio.

The de facto standard on the Internet is 128kbps, which seems to strike a fairly good balance between reasonable file sizes and decent quality. However, most audiophiles (and this author) feel that 128kbps isn't good enough to get you CD quality audio.

If you're encoding your own files and aren't intending to distribute them over the Internet (and you shouldn't be doing so unless you control the copyright), consider encoding your tracks at a higher bitrate. I recommend 160kbps for most purposes and 192kbps when you want to be sure you're getting optimal quality. The size difference isn't that onerous, and quality difference can be dramatic, depending on the kind of music you're working with.

Because you won't be listening to MP3s on your computer forever (you'll probably be listening on your home stereo before long), make sure you're getting as much fidelity as possible. What sounds fine on your computer will probably sound sub-par on your home stereo.


Scot Hacker's book, MP3: The Definitive Guide, includes many other tips on creating quality MP3 files.

6) Consider Variable Bitrate (VBR): In standard MP3 encoding, the same number of bits is dedicated to every second of audio, regardless the complexity of the passage in question. This is called "Constant Bitrate," or CBR. However, most music contains passages of both great complexity and relative simplicity, or constancy. Because less complex passages don't require as much data to be encoded accurately, this can result in a waste of storage space. And when things do get complex, the codec (compressor/decompressor -- the program code used to turn raw audio into compressed MP3) may find itself starved trying to put too much information into too small a space.

The solution is Variable Bitrate, or VBR. With VBR encoding enabled, the coder will dynamically alter the bitrate for each frame of the MP3 file, so that fewer bits can be allocated to simple passages and more bits can be allocated to more complex passages. VBR gives you the ability to optimize the quality/storage ratio with an efficiency not possible with CBR.

The drawbacks to VBR are that some older MP3 players can't handle VBR files, and that timing issues may not be handled properly (the time readout on the player may not display accurately). However, these problems are becoming non-issues as players become more sophisticated.

Take a look in your encoder's preferences panel for VBR options, and do some experiments. You should have a quality slider similar to those found in JPEG Save dialogs with which you can control the thresholds of tolerance for VBR ratios. Compare CBR and VBR files for size and quality and there's a good chance you'll decide it's worth considering VBR for use on all files.

7) Automate Test Encodings: Everyone's ears, equipment, and quality requirements are different. If you're trying to determine the best bitrate for your encodings, you'll want to encode the same track or excerpt many times at different bitrates in order to compare quality and file sizes.

Because the process of creating a large number of test encodings can be tedious, I've included an automation script you can use if you're running UNIX/Linux or BeOS. The use of this script assumes you have the LAME encoder somewhere in your path. This script should be very easy to modify to work with other encoders. A DOS version is also provided.


#!/bin/sh
FileName="$1"

# Start a loop and encode the sample  at each bitrate specified
for i in 64 96 128 160 192 256; do
      echo Creating "$FileName".$i.mp3
      lame -ms -b$i "$FileName".wav "$FileName".$i.mp3
      echo
done

If you're on a DOS/Windows machine, here's a batch file that will achieve the same effect:


Echo off
rem example
set filename=%1

if not exist %filename%.wav goto error

rem The following must go on a single line --
rem We had to wrap  it to two lines for this book
for %%B in (64 96 128 160 192 256) do lame -ms -b%%B %filename%.wav
      %filename%-%%B.mp3 goto exit :error    echo File %filename%.wav does not exist    goto exit :exit

Whether you're running the bash or the DOS version, save the script as a file in your system's path with a filename like "TestEnc." Then run:


TestEnc filename

This assumes that your input file is called filename.wav.

8) Uncooking Bad Transfers: Every so often, you may end up with an MP3 file that's been corrupted during transfer, usually because it was transferred in ASCII mode rather than binary. If this happens to you, get a copy of one of the following programs from a shareware site on the Web: uncook.exe, Uncook95, or OK Uncook. All three do essentially the same thing: repair the damage that was done to the file during transfer and make it playable again. Linux and BeOS users may want to keep a copy of mp3asm around for the same purpose.

This problem should never occur if you're using the latest version of either Netscape or IE. However, it's nice to know there are tools available to correct the situation if it does.

9) One-Click Master List: How would you like to be able to double-click a single icon on your Desktop, have it scan your system for MP3 files, generate a master playlist, and launch that playlist into WinAmp or another MP3 player? A batch file (that's DOS-ese for shell script) like the one below can make this easy. To accomplish this, you will need to invoke the ancient masters of your early DOS training.

In order to Generate a playlist from the DOS command line, you must first construct a command that will find all files ending in .mp3 in a specified directory, and all subdirectories. You want to do this without generating extra data and without listing directory names themselves. Here's what you want to do:


dir c:\data\mp3\*.mp3 /s/b/a-d > c:\data\playlists\thislist.m3u

If you scatter MP3 files all over your system (which wouldn't be very good housekeeping), you can change the initial path to simply C:\. In case you're curious about those switches, /s tells dir to recurse down through subdirectories. /b tells dir to output only a simple list of pathnames, without file sizes or foreshortened filenames. /a tells dir to handle special attributes, as specified in its subsequent arguments. d is one of the attributes /a can handle, and since we precede it with a (instead of :), we're telling dir to ignore directory names themselves (directory names in playlists will simply be ignored by most players, but they're still messy). The > symbol is a redirect that tells DOS to dump the output of the command to a new file. Of course, you'll need to insert the path to your collection of MP3 files, as well as the preferred path to, and the name of, your generated playlist file.

You can also automate this process in a batch file by creating something like this:


@echo off
echo.
echo Creating playlist from d:\data\mp3 ...
REM :: Remember to edit the paths below to match
REM :: those on your machine! The second line is optional,
REM :: and shows how you can have this batch file scan through
REM :: multiple drives and directories and append the results 
REM :: to the same playlist file. Note that if any of your directory
REM :: or filenames are longer than 8.3 you'll need to surround
REM :: them in quotes so DOS doesn't get confused.
dir d:\data\mp3\*.mp3 /s/b/a-d > "d:\data\playlists\alltracks.m3u"
dir e:\hold\music\mp3\*.mp3 /s/b/a-d >> "d:\data\playlists\alltracks.m3u"
echo.
echo Launching the new list in WinAmp...
REM :: The next line can be the full path to any MP3 player
REM :: on your system that accepts a playlist as a command line
REM :: argument. WinAmp handles this just fine.
start "C:\Program Files\Winamp\winamp.exe" "d:\data\playlists\alltracks.m3u"
exit

To test your new procedure, double-click the batch file in Explorer. Did it work? Good. Unfortunately, you will have noticed that a DOS shell is launched automatically when you run the batch file. The shell doesn't close itself when you close WinAmp. The trick to fixing this annoying behavior is:

A: Click the small Properties icon in the DOS shell's menu bar.

B: Check the "Close on exit" checkbox (you may also want to select "Run: minimized" to keep the DOS window from flashing briefly on screen).

C: Close the Properties panel, and a .PIF file will be created in the same directory as the batch file, appearing as a standard Shortcut file.

D: Drag this shortcut to your Desktop, or wherever you want it to live. From now on you can click the icon to launch your script. The DOS shell will close as soon as WinAmp launches.

10) Building Logical Directory Structures: Users intent on building up a large collection--especially collections including complete or partial albums--may want to create a directory/folder structure that makes it easy to group and organize artists and their albums. For example:


c:\data\mp3
c:\data\mp3\albums
c:\data\mp3\albums\Beatles
c:\data\mp3\albums\Beatles\Magical_Mystery_Tour
c:\data\mp3\albums\Beatles\Magical_Mystery_Tour\Blue_Jay_Way.mp3
c:\data\mp3\albums\Beatles\Magical_Mystery_Tour\I_Am_the_Walrus.mp3
c:\data\mp3\singles
c:\data\mp3\singles\Rolling_Stones--Paint_It_Black.mp3

These are DOS/Windows-style paths, but the same principle of organization applies equally to MacOS or BeOS/UNIX users. Note that in the example above, songs stored under the albums hierarchy are named after song titles only. However, songs stored in the singles directory are given both the artist and song name, since artist names in filenames are redundant if the directory structure is logically organized. In addition, some users may want to further subdivide their hierarchy into musical genres. For example:


c:\data\mp3\albums\rock\Beatles
c:\data\mp3\singles\jazz\Miles_Davis

After much experimentation with various organizational hierarchies, I have finally settled on a scheme that works well for me. Rather than separating out albums and singles, I realized that I usually only encoded portions of albums, and that as I gathered singles, a lot of them were by the same artists. My hierarchy now looks like this (BeOS/Linux-style paths used here):


~/mp3/artists
~/mp3/artists/Captain Beefheart/Trout Mask Replica
~/mp3/artists/Captain Beefheart/Trout Mask Replica/11 - China Pig.mp3
~/mp3/artists/Captain Beefheart/Trout Mask Replica/20 - Ant Man Bee.mp3
~/mp3/artists/Captain Beefheart/Lick My Decals Off/09 - Petrified Forest.mp3
~/mp3/artists/Hank Williams/.
~/mp3/encode
~/mp3/downloads
~/mp3/moonshine

Because each artist has a directory of their own, it doesn't matter whether I am storing singles or albums. When I want to hear a particular artist, I just drag their folder onto my MP3 player. I don't even bother making playlists for artists anymore. However, if your tendency is to not encode complete albums very often, it may not make sense to create album subdirectories under the artist names. You may want to include the album in the filename and put all tracks by a given artist into a single directory. Some users also store the source's format in the filename, designating whether the track came from an LP, CD, 7-inch single, etc.

The encode directory is used as a container for tracks currently being encoded in the background. Because I've configured my encoder to create artist and album subdirectories if they don't exist, I can easily drag the newly created folders into the artist hierarchy after I've listened to the encodings and determined that their quality is satisfactory. The downloads directory is used as a temporary holding bin where downloaded tracks live until I've decided whether to keep or delete them. Finally, a directory called moonshine is used to store miscellaneous tracks. As soon as there are two or more tracks by the same artist in the moonshine directory, I create an artist folder for them in the artists hierarchy.

However, it may not be necessary to organize things this tightly, since (as you'll in Chapter 4 of my book), you can always create custom playlists based on artist, genre, and other arbitrary criteria. Tools are even available to generate custom playlists on specific criteria by hoovering ID3 tag information out of your files, thus treating your entire collection as a database to query. Nevertheless, as your collection grows you will appreciate having a well-kept directory hierarchy.

In the end, how you choose to organize your collection is completely up to you and your personal preferences. If you're the type who stacks hundreds of CDs up in random piles throughout your house, you may be perfectly happy with a single directory containing thousands of individual MP3 files. If, on the other hand, you're in the habit of meticulously organizing your LP/CD collection by genre and artist, you'll probably want to do something similar for your digital music collection.

Scot Hacker is the author of O'Reilly's MP3: The Definitive Guide, Peachpit's "The BeOS Bible," and countless articles for print- and Web-based technology publications.




Sponsored by:



O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.