Using The Linux Command Line To Record An MP3 File To An Audio CD

© copyright 17.Jun.2010 by Paul Bradley posted under Linux.


Twice a week, I have a long commute of around 200 miles, and to make the journey bearable I listen to several Linux podcasts. However, the CD player in my car doesn’t support MP3 files, so I needed to write a Bash script which would burn the first 80 minutes of a podcast on to a CD.

To achieve this you will need the following Linux command line utilities, most distributions will have these programs in their repositories. First you will need cdrecord to actually burn the podcast to a CD-RW. The mpg123 program is used to convert the original mp3 into a wav file. Finally we use the sox utility, to extract the first 80 minutes of audio, so that it will fit onto a standard CD-RW.

Finding the SCSI bus ID of your CD-RW drive

To use the cdrecord command to actually burn a file to the CD-RW, you will need to find out the SCSI bus ID of your CD-RW drive. By issuing the -scanbus switch, you will get a list like the one shown below. In this example, my CD-RW drives ID is 0,1,0 - which I use later.

cdrecord -scanbus

scsibus0:
	0,0,0	  0) *
	0,1,0	  1) ’HL-DT-ST’ ’DVD-RW GWA-4082N’ ’CC15’ Removable CD-ROM
	0,2,0	  2) *

Converting the MP3 and trimming the audio to 80 minutes

The following command line, will convert the file input.mp3 to a WAV file called output.wav

mpg123 --rate 44100 --stereo --buffer 3072 --resync -w output.wav input.mp3

We then use the sox command, to trim or extract the first 80 minutes of audio by specifying the start location of zero and an endpoint of 80:00. The resulting file will be called cd.wav. If the first ten minutes of the podcast is just idle chatter, then you can trim from 10:00 to 90:00 to grab 80 minutes starting from 10 minutes in.

sox output.wav cd.wav trim 0 80:00

Blanking the CD-RW and burning the audio file to disk

Before we burn the cd.wav file to the CD-RW, we first need to erase the contents of the disk to end up with a blank disk. Here we need to pass in the device ID of the drive, which we determined by using the -scanbus command earlier.

cdrecord -silent blank=fast dev=0,1,0

Now that we have a blank disk, we can now burn the file cd.wav to the CD. In this command we specify the write speed of the drive (32) and tell the command to eject the CD when finished.

cdrecord dev=0,1,0 -silent -eject speed=32 -pad -audio cd.wav

§


Please consider linking to this page, by sharing it with others using social sites like del.icio.us, Stumble Upon or Twitter or by emailing it to your friends.


Other Popular Linux Articles