And add it to your phone's music player
DISCLAIMER: Please note that the legality of this operation varies from country to country. In some places it is authorised under "fair use" rules, in others it is considered copyright infringement and/or an act of counterfeit. Please check local laws before proceeding.
The idea here is to start out with an audio CD and end up being able to listen to it on your mobile phone using your GNU/Linux computer. There are five simple steps involved:
- Extracting the audio data from the CD and saving each track in a file on the computer.
- Converting that audio data to MP3 files
- Adding ID3V2 tags to the MP3 files so that the phone can sort them by album, artist, genre and track number, and display album art in the music player.
- Copying the tagged files over to the phone.
- Refreshing the music player's library so that it indexes the files just copied.
If you think 5 steps is a lot to have to go through, then there's nothing stopping you from scripting the lot together to turn them into a single step. However, I'll not be doing that here because the steps really are simple. Very simple. Also, while the whole thing may appear long-winded at first sight, remember that most of this (installation of software, saving scripts etc…) only has to be done the first time round. Second and subsequent CD rips will be much faster because all of the infrastructure needed has already been set up.
Extracting the audio data
This is the second easiest step. All you need to do is bung the CD into your CD-ROM or DVD-ROM drive and run:
$ cdparanoia -B 1-number-of-tracksSo, taking the example of the 2002 release "The Best Of Peter Green's FLEETWOOD MAC" on which there are 20 tracks, that'd be:
$ cdparanoia -B 1-20If you're unsure about the number of tracks on your CD you can always use cdparanoia's "-Q" option to list them out:
$ cdparanoia -Q cdparanoia III release 10pre0 (August 29, 2006) (C) 2006 Monty <monty@xiph.org> and Xiph.Org Report bugs to paranoia@xiph.org http://www.xiph.org/paranoia/ Table of contents (audio tracks only): track length begin copy pre ch =========================================================== 1. 14412 [03:12.12] 0 [00:00.00] no no 2 2. 12975 [02:53.00] 14412 [03:12.12] no no 2 3. 28143 [06:15.18] 27387 [06:05.12] no no 2 4. 13405 [02:58.55] 55530 [12:20.30] no no 2 5. 13052 [02:54.02] 68935 [15:19.10] no no 2 6. 20700 [04:36.00] 81987 [18:13.12] no no 2 7. 12875 [02:51.50] 102687 [22:49.12] no no 2 8. 17728 [03:56.28] 115562 [25:40.62] no no 2 9. 12880 [02:51.55] 133290 [29:37.15] no no 2 10. 40920 [09:05.45] 146170 [32:28.70] no no 2 11. 15825 [03:31.00] 187090 [41:34.40] no no 2 12. 18748 [04:09.73] 202915 [45:05.40] no no 2 13. 13275 [02:57.00] 221663 [49:15.38] no no 2 14. 15642 [03:28.42] 234938 [52:12.38] no no 2 15. 24285 [05:23.60] 250580 [55:41.05] no no 2 16. 12575 [02:47.50] 274865 [61:04.65] no no 2 17. 10593 [02:21.18] 287440 [63:52.40] no no 2 18. 13272 [02:56.72] 298033 [66:13.58] no no 2 19. 14679 [03:15.54] 311305 [69:10.55] no no 2 20. 24497 [05:26.47] 325984 [72:26.34] no no 2 TOTAL 350481 [77:53.06] (audio only)You should now have a collection of 20 (or how ever many tracks there are on your CD) individual files in Microsoft .wav format named track01.cdda.wav, track02.cdda.wav etc…
Conversion to MP3
This is one of the areas where you must check local law. MP3 audio encoding is a patent minefield. To be within the law, either the user or the developer of software able to encode audio into MP3 format has to pay royalties to the Fraunhofer Institute.
Once that hurdle is behind you, you must decide what bitrate you want to use. A higher bitrate means better audio quality, but it also means a larger MP3 file after conversion. When dealing with stereo files, 160 kbps is a good starting point. If the original isn't very good to start with then there's little point in trying to conserve as much detail in the sound as possible if there's none to start with. In this case, 128 kbps should be enough. On the other hand, if your original has plenty of dynamic in it and you want to lose as little quality as possible then you can go for higher bitrates like 192 or 224 kbps. The human ear won't notice any change in quality at bitrates above that so there's no point in encoding at, say, 320 kbps.
Two of the many tools commonly used on GNU/Linux to encode into MP3 format are bladeenc and ffmpeg. bladeenc is probably easier to use since it is a stand-alone, self-contained MP3 converter while ffmpeg is more of a general-purpose audio/video conversion tool which relies on external libraries (libmp3lame in the case of MP3 audio conversion), but I'll describe how to do it using both tools. The instructions below assume you're using the 160 kbps bitrate. Obviously substitute as necessary.
Using bladeenc
$ bladeenc -160 track??.cdda.wavThat's all there is to it! bladeenc will batch process all the .wav files and produce a corresponding .mp3 for each one.
Using ffmpeg
This tool is a little more complicated to use since it cannot batch process files on its own, so we have to feed it each file individually in a loop. the "for ... in" loop in the shell's syntax is a real timesaver here:
$ for wavfile in track??.cdda.wav; do \ ffmpeg -i $wavfile -acodec libmp3lame -ab 160k ${wavfile%wav}mp3; \ doneThis loop simply iterates through all of the files identified by the track??.cdda.wav glob, assigning each filename in turn to the variable $wavfile. It then invokes ffmpeg saying "use $wavfile as your input" (-i $wavfile), "encode the audio to MP3 format using libmp3lame" (-acodec libmp3lame), and "use a bitrate of 160 kbps" (-ab 160k). We then construct the name of the output file using basic text manipulation functions in the shell. "${wavfile%wav}" means "take the value of the variable $wavfile and chop the 'wav' off the end", so this will leave us with track01.cdda., track02.cdda. etc… We simply tag "mp3" on the end of that to get our track01.cdda.mp3, track02.cdda.mp3 etc…
Regardless of the conversion tool used, we now have a collection of files called track01.cdda.mp3, track02.cdda.mp3 etc…
ID3V2 tagging
This step is what's required to get the music player on your phone to see the music tracks as anything other than "unknown". You will need id3lib to do the actual work and a front-end tool called id3v2, but before installing id3v2 you'll need to apply a patch that I wrote, without which id3v2 segfaults when trying to create a TRCK frame (track number) and cannot add an APIC frame (album art) to an ID3V2 tag.
First, create a simple text file listing out the details of each track on the album, the artist's name, the album name, the release year etc… We're going to feed this file into a loop that's going to tag the MP3 files and rename them all on the fly. Your album information file should look like this example, which we'll save as fw.alb:
Artist: Fleetwood Mac Album: The Best Of Peter Green's Year: 2002 Photo: cover.jpg Genre: 17 01 Albatross 02 Black Magic Woman 03 Need Your Love So Bad 04 My Heart Beat Like A Hammer 05 Rollin' Man 06 The Green Manalishi (With The Two Pronged Crown) 07 Man Of The World 08 Something Inside Of Me 09 Looking For Somebody 10 Oh Well (parts 1 and 2) 11 Rattlesnake Shake 12 Merry Go Round 13 I Loved Another Woman 14 Need Your Love Tonight 15 Worried Dream 16 Dragonfly 17 Stop Messin' Around 18 Shake Your Moneymaker 19 I'd Rather Go Blind (Chicken Shack) 20 Albatross (Chris Coco featuring Peter Green)The first 5 lines are common to all tracks. If you don't have the information for one of the lines (eg. you don't have the release year or you don't have an album cover) then simply leave that line out. There must be one space between the header name and colon and the content of that header, so:
Album: Parallel lines WRONG! (more than one space) Album:Parallel lines WRONG! (no spaces at all) Album Parallel lines WRONG! (colon missing) Album: Parallel lines RIGHT!The genres are numbered from 1 to about 150, although the Nokia music player doesn't seem to recognise all of them. Genre number 17 is "Rock". Run this to get a list of genres that id3v2 supports:
$ id3v2 -L 0: Blues 1: Classic Rock 2: Country 3: Dance 4: Disco 5: Funk 6: Grunge 7: Hip-Hop 8: Jazz 9: Metal ... ... 143: Salsa 144: Thrash Metal 145: Anime 146: Jpop 147: SynthpopOther headers you can use that are more commonly seen in classical music are "Conductor:" and "Composer:".
Each of the lines for a track is of the form:
nn track titleThe line starts with the track number over two digits, so with a leading '0' for the first nine tracks. Then there's a space, then there's the track name itself. The track name will be used to construct a new filename for the MP3 file, so don't use characters here that are not recognised both by your Linux filesystem and Microsoft FAT32 filesystems (used by the memory card or mass memory in your phone!). Avoid things like the colon ':', the semicolon ';', the ampersand '&', the exclamation mark '!', the question mark '?', the asterisk '*', the "less than" sign '<' and "greater than" sign '>', and any others you should be aware of. Spaces in the title name are converted into underscores for the final filename.
Now, copy & paste this script into a text editor and save it as "tagmusic.sh". Don't forget to "chmod +x" it to make it executable.
#!/bin/bash showusage() { cmd="`basename \"$0\"`" echo "$cmd: usage" > /dev/stderr echo -e "\t$cmd <album_information_file>" > /dev/stderr exit 1 } # If no album information file was given then bail out if [ "$#" != "1" ]; then showusage fi # If the file given can't be found then bail out if [ ! -f "$1" ]; then echo "$1: File cannot be found" > /dev/stderr exit 1 fi # Parse the file for the artist's name artist="" data="`grep -i \"^Artist:\" \"$1\"`" if [ "$?" == "0" ]; then artist="`echo \"$data\" | cut -d ' ' -f2-`" fi # Parse the file for the album title album="" data="`grep -i \"^Album:\" \"$1\"`" if [ "$?" == "0" ]; then album="`echo \"$data\" | cut -d ' ' -f2-`" fi # Parse the file for the album release year year="" data="`grep -i \"^Year:\" \"$1\"`" if [ "$?" == "0" ]; then year="`echo \"$data\" | cut -d ' ' -f2-`" fi # Parse the file for the cover art photo="" data="`grep -i \"^Photo:\" \"$1\"`" if [ "$?" == "0" ]; then photo="`echo \"$data\" | cut -d ' ' -f2-`" # cancel the photo inclusion if the file can't be found if [ ! -f "$photo" ]; then photo="" fi fi # Parse the file for the music genre genre="" data="`grep -i \"^Genre:\" \"$1\"`" if [ "$?" == "0" ]; then genre="`echo \"$data\" | cut -d ' ' -f2-`" fi # Parse the file for the composer composer="" data="`grep -i \"^Composer:\" \"$1\"`" if [ "$?" == "0" ]; then composer="`echo \"$data\" | cut -d ' ' -f2-`" fi # Parse the file for the conductor conductor="" data="`grep -i \"^Conductor:\" \"$1\"`" if [ "$?" == "0" ]; then conductor="`echo \"$data\" | cut -d ' ' -f2-`" fi # Now that we have the parameters, loop through each of the track??.cdda.mp3 files for mp3file in track??.cdda.mp3; do \ # Clear any pre-existing ID3V1 and ID3V2 tags id3v2 -D $mp3file # Isolate the track number trck=${mp3file:5:2} # Now grab the relevant track information from the album information file trkdata="`grep \"^$trck\" \"$1\"`" if [ "$?" != "0" ]; then continue fi # Isolate the title title="${trkdata:3}" # Construct the new filename filename="`echo \"$trkdata\" | tr ' ' '_'`.mp3" # Insert the track number and title id3v2 -T $trck -t "$title" $mp3file # Insert the artist's name if we know it if [ -n "$artist" ]; then id3v2 -a "$artist" $mp3file fi # Insert the album name if we know it if [ -n "$album" ]; then id3v2 -A "$album" $mp3file fi # Insert the genre code if we know it if [ -n "$genre" ]; then id3v2 -g "$genre" $mp3file fi # Insert the release year if we know it if [ -n "$year" ]; then id3v2 -y "$year" $mp3file fi # Insert the composer if we know the name if [ -n "$composer" ]; then id3v2 --TCOM "$composer" $mp3file fi # Insert the conductor if we know the name if [ -n "$conductor" ]; then id3v2 --TPE3 "$conductor" $mp3file fi # Insert the album art if there is any if [ -n "$photo" ]; then id3v2 --APIC "$photo" $mp3file fi # Finally, rename the file mv -v $mp3file "$filename" doneNow we have to feed this script the album information file fw.alb we created earlier and watch it do the work:
$ tagmusic.sh fw.alb Stripping id3 tag in "track01.cdda.mp3"...id3v1 and v2 stripped. `track01.cdda.mp3' -> `01_Albatross.mp3' Stripping id3 tag in "track02.cdda.mp3"...id3v1 and v2 stripped. `track02.cdda.mp3' -> `02_Black_Magic_Woman.mp3' ... ...If we now look at what we have:
$ ls -1s *.mp3; ls -1s *.wav 3800 01_Albatross.mp3 3428 02_Black_Magic_Woman.mp3 ... ... 33144 track01.cdda.wav 29840 track02.cdda.wav ... ...If you're not planning on doing anything else with the .wav files you can delete them:
$ rm -v track??.cdda.wav removed `track01.cdda.wav' removed `track02.cdda.wav' ... ...Copying the music to your phone
This bit's really easy. Start by connecting the phone to your computer via a USB cable in "Mass storage" mode. You already know how to do this if you've been copying photos and videos to/from the phone. Let's assume your phone is attached to a mount point called "/mnt/phone". Substitute in the following instructions as appropriate for your system.
Make sure there's a directory called "Music" in the storage space published by the phone and now mounted on /mnt/phone, and create a directory inside it that reflects the album you're copying over. The phone itself doesn't need the files to be organised this way (other than the files have to be in the "Music" directory), it's just so that we silly humans know what's there and where it is:
$ mkdir -p "/mnt/phone/Music/Fleetwood Mac/Best of Peter Green's"Copy the MP3 files to this newly-created directory (don't forget that tab auto-completion is your friend when typing in names of directories and files that already exist):
$ cp -v *.mp3 "/mnt/phone/Music/Fleetwood Mac/Best of Peter Green's" `01_Albatross.mp3' -> `/mnt/phone/Music/Fleetwood Mac/Best of Peter Green's/01_Albatross.mp3' `02_Black_Magic_Woman.mp3' -> `/mnt/phone/Music/Fleetwood Mac/Best of Peter Green's/02_Black_Magic_Woman.mp3' ... ...Unmount the phone from the filesystem and you're done.
Indexing the music
Now we have to tell the phone that the music is there and that it should list it in what's available to the music player. The way to do this varies from one phone model to another − the instructions given here apply to Nokia S60 phones such as the Nseries and Eseries devices. This is the easiest step and it's an N96 that I used here.
Fire up the media player and then press "Options" and select "Refresh library":
Once the indexing process is through you'll be notified how many tracks have been added:
Done!
Enjoy the album you've just added!
So, what needs to be done for the next CD?
- Rip the audio tracks to .wav files with cdparanoia
- Convert the tracks to MP3 format using bladeenc or ffmpeg
- Create an album information file and feed it to the tagmusic.sh script
- Delete the .wav files and copy the MP3 files to your phone
- Refresh the phone's music library
Other than the processing time in step 2, the lot should take you no more than 2 or 3 minutes. And if you're feeling creative you should be able to cobble together a script which takes care of absolutely everything except creating the album information file.
Powered by Apache /
Slackware Linux
Last update: 27-OCT-2013 08:27:40 UTC
This page has been served 9783
times since 10-DEC-2008