5

I'm writing a little script to analyze audio CDs. I'm specifically looking for a way to grab the track count from the CD, either from a shell-script or from Python. Is there an easy way to do this?

On an Ubuntu 12.04 derivative.

  • How did you make the text "on an Ubuntu 12.04 derivative"? – neverMind9 Nov 24 '18 at 3:08
6

cdparanoia

You can get a list of CD audio tracks using the command line tool, cdparanoia.

$ cdparanoia -sQ

Example

$ cdparanoia -sQ
cdparanoia III release 10.2 (September 11, 2008)


Table of contents (audio tracks only):
track        length               begin        copy pre ch
===========================================================
  1.    19497 [04:19.72]        0 [00:00.00]    OK   no  2
  2.    20253 [04:30.03]    19497 [04:19.72]    OK   no  2
  3.    29250 [06:30.00]    39750 [08:50.00]    OK   no  2
  4.    34263 [07:36.63]    69000 [15:20.00]    OK   no  2
  5.    28804 [06:24.04]   103263 [22:56.63]    OK   no  2
  6.    21582 [04:47.57]   132067 [29:20.67]    OK   no  2
  7.    33270 [07:23.45]   153649 [34:08.49]    OK   no  2
  8.    29919 [06:38.69]   186919 [41:32.19]    OK   no  2
  9.    29564 [06:34.14]   216838 [48:11.13]    OK   no  2
 10.    18993 [04:13.18]   246402 [54:45.27]    OK   no  2
 11.    35368 [07:51.43]   265395 [58:58.45]    OK   no  2
TOTAL  300763 [66:50.13]    (audio only)

You can count the number of lines that contain the string "OK" or that start with a number, for e.g. 1..

$ cdparanoia -sQ |& grep OK | wc -l
11

-or-

$ cdparanoia -sQ |& grep -P "^\s+\d+\." | wc -l
11

cdda2wav

You can also use this command line tool, cdda2wav.

$ cdda2wav -J -D /dev/cdrom

Example

$ cdda2wav -J -D /dev/cdrom
Type: ROM, Vendor 'Optiarc ' Model 'DVD RW AD-7930H ' Revision '1.D1' MMC+CDDA
569344 bytes buffer memory requested, 4 buffers, 55 sectors
#icedax version 1.1.11, real time sched., soundcard, libparanoia support
AUDIOtrack pre-emphasis  copy-permitted tracktype channels
      1-11           no             yes     audio    2
Table of Contents: total tracks:11, (total time 66:50.13)
  1.( 4:19.72),  2.( 4:30.03),  3.( 6:30.00),  4.( 7:36.63),  5.( 6:24.04),
  6.( 4:47.57),  7.( 7:23.45),  8.( 6:38.69),  9.( 6:34.14), 10.( 4:13.18),
 11.( 7:51.43)

Table of Contents: starting sectors
  1.(       0),  2.(   19497),  3.(   39750),  4.(   69000),  5.(  103263),
  6.(  132067),  7.(  153649),  8.(  186919),  9.(  216838), 10.(  246402),
 11.(  265395), lead-out(  300763)
CDINDEX discid: gteZIAUq3lgZlEmNg_HnbWL6cJc-
CDDB discid: 0x950faa0b
CD-Text: detected
CD-Extra: not detected
Album title: 'Hour of the Olympics' [from Mary Pope Osborne]
Track  1: 'Viking Ships at Sunrise Ch 3'
Track  2: 'Viking Ships at Sunrise Ch 4'
Track  3: 'Viking Ships at Sunrise Ch 5-6'
Track  4: 'Viking Ships at Sunrise Ch 7-8'
Track  5: 'Viking Ships at Sunrise Ch 9-10'
Track  6: 'Hour of the Olympics Ch 1'
Track  7: 'Hour of the Olympics Ch 2-3'
Track  8: 'Hour of the Olympics Ch 4-5'
Track  9: 'Hour of the Olympics Ch 6-8'
Track 10: 'Hour of the Olympics Ch 9'
Track 11: 'Hour of the Olympics Ch 10'
No media catalog number present.
scanning for ISRCs: 11 ...
index scan: 11...

Again you can enlist the help of grep to get the number of tracks:

# traditional way to parse output
$ cdda2wav -J -D /dev/cdrom |& grep index | sed 's#.*: \(.*\)...#\1#g'
11

# use grep's PCRE capability
$ cdda2wav -J -D /dev/cdrom |& grep -Po '(?<=index scan: )\d+' | tail -1
11

References

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.