script to disc-at-once extract audio books with cdparanoia


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to disc-at-once extract audio books with cdparanoia
# 1  
Old 09-28-2011
script to disc-at-once extract audio books with cdparanoia

Presently I am doing this in two steps. In the following example, I want to extract an 18 track disc with a filename of /rip/fandf11.wav
Code:
cdparanoia -Q
cdparanoia 1-18 "/rip/fandf11.wav" && eject cdrom

I'm trying to learn some basic scripting but I ran into a problem trying to get the number of tracks from the stdout of cdparanoia:
Code:
cdparanoia III release 10.2 (September 11, 2008)

 

Table of contents (audio tracks only):
track        length               begin        copy pre ch
===========================================================
  1.    20769 [04:36.69]        0 [00:00.00]    no   no  2
  2.    11601 [02:34.51]    20769 [04:36.69]    no   no  2
  3.    23132 [05:08.32]    32370 [07:11.45]    no   no  2
  4.    22598 [05:01.23]    55502 [12:20.02]    no   no  2
  5.    19510 [04:20.10]    78100 [17:21.25]    no   no  2
  6.    18957 [04:12.57]    97610 [21:41.35]    no   no  2
  7.    12103 [02:41.28]   116567 [25:54.17]    no   no  2
  8.    25735 [05:43.10]   128670 [28:35.45]    no   no  2
  9.    19409 [04:18.59]   154405 [34:18.55]    no   no  2
 10.    12771 [02:50.21]   173814 [38:37.39]    no   no  2
 11.    16617 [03:41.42]   186585 [41:27.60]    no   no  2
 12.    22036 [04:53.61]   203202 [45:09.27]    no   no  2
 13.    18105 [04:01.30]   225238 [50:03.13]    no   no  2
 14.    11389 [02:31.64]   243343 [54:04.43]    no   no  2
 15.    16964 [03:46.14]   254732 [56:36.32]    no   no  2
 16.    13345 [02:57.70]   271696 [60:22.46]    no   no  2
 17.    22906 [05:05.31]   285041 [63:20.41]    no   no  2
 18.    24715 [05:29.40]   307947 [68:25.72]    no   no  2
TOTAL  332662 [73:55.37]    (audio only)

Now my first thought was it would be nice to get the script to recognize that "18." was the last track in the listing from cdparanoia, but I have no idea how to iterate a search for a number prior to ". ", that is dot-space or dot-space-space where it will successively replace the value of the track number from 1 to 18 (in this example) in a variable, say $END.

Since I didn't know how to do that and it seemed a little complicated I thought how about taking the line number of "TOTAL" and subtracting the number of prefix lines and the "TOTAL" line itself , 8, giving me my 18.
I tried to execute
Code:
cdparanoia -Q | grep -n -o "TOTAL"

and it did not send the output to grep. Why??

So what I'm really looking to do at this point is figure out some way I can make that work. Then my next step is to get the output (in this case 26:TOTAL) and taking the value before the : and subtracting 8 and putting it into $END.

I did see an example that took the cdparanoia output and then used &> to put it in a file...but I don't want to make a temp file if I don't have to.

Once I get that done, I have the basics of the script then I think I would probably like to get a bit more complicated like tacking a +1 on the end of the filename in the /rip directory so I don't have to keep track.
# 2  
Old 09-28-2011
For some odd reason, cdparanoia seems to write to stderr and not stdout and that is why the list didn't go to grep. A simple redirect and pipe to awk and you'll have your answer without having to do any counting, and without depending on the it always writing the same number of header/trailer lines.

Code:
#!/usr/bin/env ksh
cdparanoia -Q 2>&1| awk ' $1+0 > 0 { n=$1+0; next; } END { print n}' |read END

and if you must use bash
Code:
#!/usr/bin/env bash
END=$(cdparanoia -Q 2>&1| awk ' $1+0 > 0 { n=$1+0; next; } END { print n}' )

# 3  
Old 09-28-2011
Thanks...

Perhaps I should have mentioned I'm using Ubuntu 10.10 in case that is relevant, and I would prefer to use /bin/bash since that is what most of the examples are in.

Anyway, I take it that "simple redirect" is the 2>&1 to the pipe. I didn't want to use the awk statement because I have no clue what that means and so I would be copying and pasting rather than learning anything. Would you explain what that statement does?

Quote:
Originally Posted by agama
For some odd reason, cdparanoia seems to write to stderr and not stdout and that is why the list didn't go to grep. A simple redirect and pipe to awk and you'll have your answer without having to do any counting, and without depending on the it always writing the same number of header/trailer lines.

Code:
#!/usr/bin/env ksh
cdparanoia -Q 2>&1| awk ' $1+0 > 0 { n=$1+0; next; } END { print n}' |read END

and if you must use bash
Code:
#!/usr/bin/env bash
END=$(cdparanoia -Q 2>&1| awk ' $1+0 > 0 { n=$1+0; next; } END { print n}' )

# 4  
Old 09-28-2011
Using bash is perfectly acceptable. Kshell has a few features that make it more attractive to me, but that's a personal preference.

The 2>&1 causes output to stderr (file descriptor 2) to be written onto stdout (file descriptor 1) which is what the shell maps to the process on the other side of the pipe.

Some comments on the awk:
Code:
awk ' 
$1+0 > 0 {   # when the first field converted to a number is greater than 0
   n = $1+0; # save the first field as a number in variable n
   next;      # continue processing with next line of input (not required for this case, but good form)
} 

END {     # execute this code after the last line of input has been processed
   print n;  # print the value of n to standard output
}'

In short, awk reads the input file (or from the pipe a.k.a. stdin) and for each line of input executes the programme. In this case the programme is a very simple check to see if the first column (or field if you like that phrase) contains a digit, and if it does the code in curly braces is executed (saving that value in n).

It uses a bit of awk trickiness to do this. The field is added to 0 knowing that awk will convert any text string to 0 and for each record that does not start with a number (e.g. 1.) the result will be 0 + 0 and the condition will be false; the code will not be executed and n will not be changed.

Here's a link to a decent tutorial on awk -- worth the read as awk is one of the (IMHO) most useful tools available to a UNIX programmer.

Awk - A Tutorial and Introduction - by Bruce Barnett

Hope this helps.

Last edited by agama; 09-28-2011 at 09:28 PM.. Reason: clarification
# 5  
Old 09-28-2011
Quote:
Originally Posted by agama
Using bash is perfectly acceptable. Kshell has a few features that make it more attractive to me, but that's a personal preference.

The 2>&1 causes output to stderr (file descriptor 2) to be written onto stdout (file descriptor 1) which is what the shell maps to the process on the other side of the pipe.

Some comments on the awk:
Code:
awk ' 
$1+0 > 0 {   # when the first field converted to a number is greater than 0
   n = $1+0; # save the first field as a number in variable n
   next;      # continue processing with next line of input (not required for this case, but good form)
} 

END {     # execute this code after the last line of input has been processed
   print n;  # print the value of n to standard output
}'

In short, awk reads the input file (or from the pipe a.k.a. stdin) and for each line of input executes the programme. In this case the programme is a very simple check to see if the first column (or field if you like that phrase) contains a digit, and if it does the code in curly braces is executed (saving that value in n).

It uses a bit of awk trickiness to do this. The field is added to 0 knowing that awk will convert any text string to 0 and for each record that does not start with a number (e.g. 1.) the result will be 0 + 0 and the condition will be false; the code will not be executed and n will not be changed.

Here's a link to a decent tutorial on awk -- worth the read as awk is one of the (IMHO) most useful tools available to a UNIX programmer.

You are only allowed to post URLs once you have at least 5 posts.

Hope this helps.
Ok, I think I understand what you're doing with awk, it is just so impenetrable at this point that it doesn't make much sense. I am best at learning by doing so an awk guide makes less sense to me than to tackle it via grep... That said, let me see if I understand your comment about awk, you said first column or field, but in reality, it is just the first character on the line that has to be a number, correct?

Also, I was able to get the right number, but I think your commented code has something wrong with it... I had to make the following modifications:

Code:
#END=$(cdparanoia -Q 2>&1| awk ' $1+0 > 0 { n=$1+0; next; } END { print n}' )

END=$(cdparanoia -Q 2>&1| awk ' $1+0 > 0 {              # when the first field converted to a number is greater than 0
   n = $1+0;                                            # save the first field as a number in variable n
   next;                                                # continue processing with next line of input (not required for this case, but good form)
     } 

END {                                                   # execute this code after the last line of input has been processed
   print n}')                                           # print the value of n to standard output


echo $END

# 6  
Old 09-28-2011
Quote:
you said first column or field, but in reality, it is just the first character on the line that has to be a number, correct?
If the first column contains 2abc, then $1+0 would be 2, and 23abc would result in 23. So, its the first consecutive characters that are digits.

I'm not sure I see the changes, unless you mean wrapping it in $() and adding the pipe. Regardless; glad you got it working.
# 7  
Old 09-28-2011
So the $1+0 actually adds the initial number on the line? If it was 139ab5, $1+0 would be 139? It just goes through and changes n each time it encounters a number, stops at each . and then when it gets to the end, TOTAL does nothing? So watching n as it went through, it would be "0" for the first 7 lines, then it would proceed through each integer, 1-18, and remain at 18 for the last line?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. What is on Your Mind?

Neo's Recommended Audio Books

Just finished this Audible audio book. It's simply great. Highly recommended: This book was so well done that I did not want to miss a single word; so ended up listening to half of the book twice when I got distracted. This is a great audio book. ... (0 Replies)
Discussion started by: Neo
0 Replies

2. Shell Programming and Scripting

The Start Of A Simple Audio Scope Shell Script...

This is a DEMO shell script to generate a simple graticule and plot inside it... Apologies for any typos... it is another building block along with my other two shell uploads recently to start a semi_serious project of an Terminal_AudioScope... The fist upload I posted recently was to show... (83 Replies)
Discussion started by: wisecracker
83 Replies

3. Shell Programming and Scripting

Need a script for automation the convert a lot number audio files to another format

I have a lot number audio files in the MP3 proprietary format, I want to convert them to 'opus' the free and higher quality format, with keep metadata also. My selection command-line programs are SoX (Sound eXchange) for convert MP3 files to 'AIFF' format in order to keep quality and metadata*... (1 Reply)
Discussion started by: temp-usr
1 Replies

4. OS X (Apple)

Python script to do simple audio capture...

This site is the first to get this snippet. It will capture an audio recording of any time length within the limits of OSX's QuickTime Player's capablility... A shell script derivative of this will be used as a further capture for CygWin's AudioScope.sh. Thoroughly read ALL the comments in... (0 Replies)
Discussion started by: wisecracker
0 Replies

5. Slackware

Problems with audio recording in Audacity 2.0.5. Slackware64 14.1; Intel HD Audio.

I'm trying to record audio using Audacity 2.0.5 installed from SlackBuilds. My system is 64-bit Slackware 14.1 and a sound card is Intel HD Audio. I didn't change my sound system to OSS. (Default sound system in Slackware 14.1 is ALSA, isn't it?) First, I set Internal Microphone slider in KMix... (2 Replies)
Discussion started by: qzxcvbnm
2 Replies

6. Shell Programming and Scripting

Error with Audio Conversion Bash Script

Good evening, I'm currently working on a BASH script to convert audio between file formats and I've come across a snag. At the beginning of the script, I'm having the system check to see if any files with the .m4a extension exist in the directory, and if so, it runs the script. If there are no... (1 Reply)
Discussion started by: KBurkholder
1 Replies

7. Shell Programming and Scripting

Script to list files not present in audio.txt file

I am having following folder structure. /root/audios/pop /root/audios/jazz /root/audios/rock Inside those pop, jazz, rock folders there are following files, p1.ul, p2.ul, p3.ul, j1.ul, j2.ul, j3.ul, r1.ul, r2.ul, r3.ul And I have a file named as "audio.txt" in the path /root/audios,... (11 Replies)
Discussion started by: gopikrish81
11 Replies

8. Shell Programming and Scripting

Books for Shell Script and UNIX

Hi i am new to UNIX completely. i dont know any thing about UNIX. can some one please help me with some books on the unix archetecture and all the internal working of UNIX OS (i want to know every thing about unix from basics) and shell programming. can any one please if available provide... (1 Reply)
Discussion started by: amiman
1 Replies

9. Solaris

Ultra 10 - Copying Files From Disc After Booting Up With Recovery Disc?

Hello, I'm still learning unix and I have what is probably a simple question but I can't seem to find the question to. I have an Ultra 10 Sparc Server running solaris 8 and the drive may have crashed (I hope not). Currently, it appears some files in the /etc folder are missing. I have a backup... (1 Reply)
Discussion started by: ideffects
1 Replies
Login or Register to Ask a Question