# Burning Audio CDs - Another Way

## wolvenwraith

I found that I needed to burn albums of mp3's many times, and I hated all the GUIs out there that do it (poor ATAPI support, bad layout, etc. etc.). I am also a big fan of the powerful Linux CLI. So I hacked something up in order to solve these problems. All you need to do is put all your mp3's into one directory and run this script (I called it mp3burn and copied it into /usr/bin so I can run it anytime, it's nice to have).

```

#!/bin/bash

 

for i in *.[Mm][Pp]3; do mv "$i" `echo $i | tr ' ' '_'`; done

for i in *.[Mm][Pp]3; do mv "$i" `echo $i | tr '[A-Z]' '[a-z]'`; done

for i in *.mp3; do lame --decode $i `basename $i .mp3`.wav; done

normalize *.wav

cdrecord dev=ATAPI:0,0,0 speed=44 -pad -audio -v -gracetime=2 *.wav

rm *.wav

```

Note that the cdrecord line may need to be modified in order to accomadate your burner (notably the dev=, and speed= lines) and gracetime can be modified as you see it as well.

Don't forget to run chmod a+x on the file so that it is executable!

Notes: Just improved it a little bit, it now normalizes (emerge normalize) the .wav files and removes them after they're burnt. Also fixed the [Mm][Pp] thing in the first line. This little script should be pretty easy to alter if it doesn't fit you perfectly. Keep in mind too, this will change the names of you mp3's if they have spaces or caps in them.Last edited by wolvenwraith on Mon Dec 29, 2003 8:40 pm; edited 1 time in total

----------

## pem725

What do you do with the wav files after burning?  

Also, you might find burncenter allows you a similar setup with a few more features.  Nice work though.  

Like you, I am a fan of the CLI and use abcde and burncenter as console apps - along with the usual straightforward calls to cdrecord.

----------

## d99ma

Perhaps you should check for [mM][pP] when removing spaces, normalize the wav files and remove them after burning. Then it would be perfect!

/Martin

----------

## wolvenwraith

Thanks for the feedback. I checked out burncenter and it seems pretty cool. However, it doesn't seem to support ATAPI burning out of the box (I didn't play with it much so I could be wrong). The other thing is I sorta like the one-liner style of this script, but that's just me  :Smile: .  All in all though burncenter seemed pretty nice.

Thanks to both of you for the suggestions, I made all the necessary changes to the script.

----------

## Elm0

Nice script, obviously going to be quicker than a GUI if you do this a lot, however well designed the GUI is. The beauty of linux I suppose, why be forced into one way when you can choose your path!

----------

## Munck

Thanks for the script. I've been looking for some easy way to do this, but I've been too lazy to write a script myself   :Smile: 

----------

## pengo

Hello there!  I'm new at this and have a fresh install of Gentoo.  What would I need to emerge for this script to work?

Thanks for the contribution!

----------

## MrNugget

Just "emerge cdrtools" and you'll be able to use cdrecord.

----------

## dr_strange

emerge cdrtools lame

----------

## BakaO

Hello,

your script is veyr cool, but I have 2 questions :

1-Is the 'normalize' really neccesary ? (I think if the dB levels are not the same, it is normal, no ?)

2-How can we add cd-text support ? I think it is related with the .inf files but how can we auto-generate them (form id-tag) or other method.

Thanks for all !

----------

## TechSmurf

I would just like to say, Thanks.  :Smile: 

This little file saved me alot of time and work.

Keep up the good work.   :Very Happy: 

(it worked 'right out of the box' )

----------

## TheEternalVortex

What's the point of renaming all the files?

----------

## metalh34d

doesn't converting mp3 into a wav cause lower quality sound, or is this just the way all mp3 burning is done?

----------

## icefox13

 *metalh34d wrote:*   

> doesn't converting mp3 into a wav cause lower quality sound, or is this just the way all mp3 burning is done?

 

Every compressed audio file (mp3, ogg) has to be converted to wav if you make an audio-cd (not mp3-cd), because wav is the format the tracks are stored on the disc.

(But you sometimes can't see this when burning an audio-cd because the mp3s are decoded and recorded nearly simultaneously.)

----------

## metalh34d

I tried using the script and it didn't work very well. It only burned 2 tracks in the folder I had set to burn to cd. Also it took a very long time and it will only play in my car. Then again I'm having problems with burned cds playing on my cdroms anyway. Any help? Please?

----------

## MighMoS

Could it be added to rename the files back to their originals?  Or at least how would I create a script that would remove the underscores?  (I have very little programming experience)

----------

## diegs

No renaming:

```
  GNU nano 1.3.2           File: /home/diegs/music/burn.sh

                                                                                

#!/bin/bash

                                                                                

for i in *.[Mm][Pp]3; do lame --decode "$i" "`basename "$i" .mp3`.wav"; done

normalize *.wav

cdrecord dev=ATAPI:0,0,0 speed=24 -pad -audio -v -gracetime=0 *.wav

rm *.wav

```

----------

## Spack

This will handle ogg vorbis files aswell:

```
#!/bin/bash

for i in *.[Mm][Pp]3; do mv "$i" `echo $i | tr ' ' '_'`; done

for i in *.[Mm][Pp]3; do mv "$i" `echo $i | tr '[A-Z]' '[a-z]'`; done

for i in *.[Oo][Gg][Gg]; do mv "$i" `echo $i | tr ' ' '_'`; done

for i in *.[Oo][Gg][Gg]; do mv "$i" `echo $i | tr '[A-Z]' '[a-z]'`; done

for i in *.mp3; do lame --decode $i `basename $i .mp3`.wav; done

for i in *.ogg; do oggdec $i; done

normalize *.wav

cdrecord dev=ATAPI:0,0,0 speed=44 -pad -audio -v -gracetime=2 *.wav

rm *.wav
```

----------

## fireboy1919

 *metalh34d wrote:*   

> doesn't converting mp3 into a wav cause lower quality sound, or is this just the way all mp3 burning is done?

 

wav is an uncompressed, lossless temporal format - the analog of bmp files in graphics.  

mp3, on the other hand, is a compressed, lossy format, and whenever you convert to it, you lose information.  Even if you do not compress at all you'll degrade the signal some when you convert to mp3 because it converts it to discrete cosine space (similar to doing a discrete fourier transform), and there's some loss of signal just from converting to it and back.  You always have to convert back because you we listen to audio in temporal, not frequency space.  Might as well do it by converting it to wav first.

Most people cannot hear the artifacts caused by the conversion;  most people can't even hear the artifacts caused by removing a chunk of the signal after this conversion.

The real thing that you have to worry about is lower sample rates, which can cause aliasing if you sample at lower than twice the frequency of the highest frequency in the signal.  As long as that doesn't happen (and it's not likely if you keep the audio at CD quality - 48kHz), you probably won't have problems.  Well...there are a few more possible problems, but they're a lot less likely.

----------

## freebit50

I love this little script!!! I was going to install k3b, but it was going to install a bunch of junk that I didn't want to wait for. This script works great, however, I added a little bit to it so cdrecord doesn't get messed on on weird filenames. I also made it so that in the end all the original files are back with no filename modifications. I also added comments. My CS teachers at school are always whining about comments and stuff. 

Gotta go for a cruise with the new cd I just burned!!! Laters....

```

#!/bin/bash

#

# create backups so that we leave originals untouched

#

mkdir ./bak

cp *.mp3 ./bak

#

# get rid of of stuff in the filename that could fubar cdrecord

#

for i in *.[Mm][Pp]3; do mv "$i" `echo $i | tr ' ' '_'`; done

for i in *.[Mm][Pp]3; do mv "$i" `echo $i | tr -d '-'`; done

for i in *.[Mm][Pp]3; do mv "$i" `echo $i | tr -d '_'`; done

for i in *.[Mm][Pp]3; do mv "$i" `echo $i | tr '[A-Z]' '[a-z]'`; done

for i in *.[Mm][Pp]3; do mv "$i" `echo $i | tr -d '('`; done

for i in *.[Mm][Pp]3; do mv "$i" `echo $i | tr -d ')'`; done

for i in *.[Mm][Pp]3; do mv "$i" `echo $i | tr -d '+'`; done

for i in *.[Mm][Pp]3; do mv "$i" `echo $i | tr -d "'"`; done

for i in *.[Mm][Pp]3; do mv "$i" `echo $i | tr -d '[0-9]'`; done

# oops, stripped the 3 from mp3....gotta add it back in

for i in *.[Mm][Pp]; do mv "$i" `basename $i .mp`.mp3; done

#

# decode mp3 to wav using lame

#

for i in *.mp3; do lame --decode $i `basename $i .mp3`.wav; done

#

# normalize is a tool for adjusting the volume of audio files 

# to a standard level.

#

normalize *.wav

#

# let us burn!!!

#

cdrecord dev=ATAPI:0,0,0 speed=16 -pad -audio -v -gracetime=2 *.wav

#

# get rid of temp files and restore originals

#

rm *.wav

rm *.mp3

mv ./bak/*.mp3 ./

rm -rf ./bak 

```

----------

## AGM

You could also check out bashburn, that's what I use. It's very cool.

----------

## MighMoS

Rather than moving all these files back and forth, wouldn't just using symlinks be easier?

----------

## freebit50

 *Quote:*   

> 
> 
> Rather than moving all these files back and forth, wouldn't just using symlinks be easier?
> 
> ```
> ...

 

----------

## agnitio

Why not use [:upper:] and [:lower:] insead of [A-Z] and [a-z] so that it works for non-english characters as well.

----------

## cottonmouth

 *freebit50 wrote:*   

> I also added comments. My CS teachers at school are always whining about comments and stuff. 

  Very nice. I don't know shell-scripting, but your comments make unintelligble bash-code much more understandable. Thanks!

Programming is about communication and exchange of ideas between people, the machine is just the medium. Code for people, not for machines. 

Or, as the Yukihiro Matsumoto said:

 *Quote:*   

> Code is an expression of the thoughts, attitudes, and ideas of the programmer. By reading code, you can not only figure out what particular task the programmers were trying to accomplish and understand how they did it, but you can also gain insight into how they were thinking. This is the reason that reading code makes programmers better. 

 

Matsumoto created Ruby, and no matter if you're interested in the language, his philosophy is enriching for all developers, no matter how skilled one is or which language one works in. Read more  here

----------

## brassj41

will the cd's play in a regular cdplayer?     I was under the impression that it had to be cdda and not wav

----------

## STEDevil

 *fireboy1919 wrote:*   

> ...keep the audio at CD quality - 48kHz

 

"DAT"-quality is 48kHz

CD-quality is always sampled at 44.1kHz

----------

## mholtz

How does this script handle mp3 files that are not 44.1kHz?  Are you sure you don't need sox to resample in those cases?

----------

## rhill

 *freebit50 wrote:*   

> 
> 
> ```
> 
> # get rid of of stuff in the filename that could fubar cdrecord
> ...

 

this'll kill any number ordering in the songs though.  i suppose it's okay for a directory of random mp3's, but for an album it'll end up burning them alphabetically, won't it?

----------

## Scratalacha

I saw this forum and found it helpfull in the creation of this script:

```

#!/bin/bash

export OPTIONS=""

export NORM=0

export num=0

export albumtitle="'VNV Nation - Matter and Form'"

for opt in "$@" ; do

   case ${opt} in

      --dummy|-d)

         echo "Running in dummy mode ..."

         OPTIONS="$OPTIONS -dummy"

         break;;

      --normalize|-n)

         echo "Normalizing on..."

         NORM=1

         break;;

      --help|-h)

         echo "To be continued..."

         exit 0;;

      *)

         echo "Unknown option '${opt}'"

         exit 1;;

   esac

done

echo "Converting files to WAV..."

for i in *.[MmOoMm][PpGg4][3GgAa]; do 

   let "num += 1"

   mplayer -vo null -ao pcm -aofile  "track_`printf "%02d" $num`.wav" "$i" 2> /dev/null | grep -A1 -e 'Title: [0-9A-Za-z]' > "track_`printf "%02d" $num`.inf" 

   if [ ! -s "track_`printf "%02d" $num`.inf" ]; then

      echo $i | sed -e 's/\[\+\\=\]*//' -e "s/\.[^\.]*$/\'/" -e "s/^/Tracktitle= \'/" >> "track_`printf "%02d" $num`.inf"      

   fi

   echo $num "-" $i      "Completed"      

done

for i in *.inf ; do sed -i -e "s/ Artist: /Performer= \'/" -e "s/ Title: /Tracktitle= \'/" -e "s/![\']$/\'/" -e "s/_/\ /" -e "s/'$//" -e "s/$/\'/" $i; done

if [ ! -z "$albumtitle" ]; then

   for i in *.inf; do echo "Albumtitle= "$albumtitle >> $i; done

fi

#create a time full calculator

#allow input of album title from CLI

if [ $NORM -eq 1 ]; then

   normalize *.wav

fi

cdrecord dev=ATAPI:/dev/hdc speed=4 driveropts=burnfree,varirec=0 -pad -audio -dao -v -text -useinfo $OPTIONS *.inf

rm track_*.wav &> /dev/null

rm track_*.inf &> /dev/null

```

This script allows one to burn mp3, ogg, and m4a files (and configurable to just about any other format) to an audio CD with cdtext.

It attempts to get the track information from the file, although if none is found, it falls back to the filename minus annoying characters.

Please let me know how this works, its a work in progress and as you can see i still have comments for code that i still need to add. Suggestions are welcome.

----------

## punter

 *freebit50 wrote:*   

> I love this little script!!! I was going to install k3b, but it was going to install a bunch of junk that I didn't want to wait for. This script works great, however, I added a little bit to it so cdrecord doesn't get messed on on weird filenames. I also made it so that in the end all the original files are back with no filename modifications. I also added comments. My CS teachers at school are always whining about comments and stuff. 
> 
> Gotta go for a cruise with the new cd I just burned!!! Laters....
> 
> ```
> ...

 

warning: this script will DELETE _all_ your .MP3 files (but not .mp3 files) !!!!

i was too lucky to notice in time....

----------

## ToeiRei

To burn the mp3-files without using temp-files:

```

#!/bin/bash

for I in *.mp3

do

mpg123 --cdr - "$I" | cdrecord -v -speed=2 dev=ATAPI:0,0,0 -audio -pad -nofix -

done

cdrecord -v -speed=2 dev=ATAPI:0,0,0 -fix

eject

```

----------

