LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Blogs > Whaling the FOSS
User Name
Password

Notices

The experiences of an Australian student who uses Linux.

Cover all topics from shell scripts to monopolies and reviews to political comments.
Rate this Entry

Homebrew paranoia frontend ( CLI ) for audiobooks

Posted 02-19-2011 at 05:16 PM by William (Dthdealer)

I've had my troubles with GUI audio CD rippers lately, whether it be DBUS connection issues or lack of 'codecs' for cdda or wav ( sounds as weird as it is ). On top of that the rip-one-at-a-time strategy was too time consuming for the repetitive audio book ripping I am doing, which involved placing four disc rips under the same title.

So I wrote a shell script that does most of the work for me

Code:
#!/bin/bash

#
#	Automatically rips CDs in the fashion that I desire
#	Will probably need disc detection modification to work on other computers
#
#	Copyright William Hales 2011
#
#
#
#  This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
#
#	Author note:  I assume the GPL in its full form will be on your *nix
#	system somewhere in /usr/share/common-licenses/ .  If it isn't, follow
#	the link in the GPL statement above
#
#


######## User input error that is non fatal
function passive_error()
{
	echo "Error: $@"
	echo
	beep
	sleep 0.5
}


######## Doesn't stop pestering the user until they enter the neccesary information for each book
function gather_book_info_from_user()
{
	# Notify the user we need new book information
	beep

	while true
	do
		bookname=''

		echo -n 'Name of book: '
		read bookname

		if  ls | grep -qi "$bookame"
		then
			# No match
			:
		else
			passive_error "You have already ripped that book.  ( BTW this script can't resume a half done rip )"
			bookname=''
		fi

		if [ -n "$bookname" ]; then break; fi
	done

	# Ask the user how many disks are in this book
	while true
	do
		totaldisks='aardvark'
		echo -n 'Number of disks in book: '
		read totaldisks

		if [ $totaldisks -eq $totaldisks 2>/dev/null ]
		then
			# Number input
			if [ $totaldisks -gt 0 ]
			then
				break
			else
				passive_error 'Integer must be positive'
			fi
		else
			passive_error 'Positive integer input only'
		fi
	done
}



######## Halts the script until a CD is detected in /dev/sr0
# This is very hacky!  You will probably have to replace this function
#  with something that works on your computer
function wait_for_disk_to_be_inserted()
{
	while true
	do
		# Errors cannot be redirected to a variable, so I have
		#  to send them to a temporary file in a RAM filesystem

		dd if=/dev/sr0 2> /dev/shm/$$autoripperddcheck
		if cat /dev/shm/$$autoripperddcheck | grep -iq medium
		then
			# Medium not found, just wait
			sleep 2
		else
			# Something has been inserted!
			rm /dev/shm/$$autoripperddcheck
			break
		fi
	done
}



####################################
######## Script starts here ########
####################################


while true
do
	# Ask the user for the book name & total number of disks
	gather_book_info_from_user

	# Make and enter the correct directory
	mkdir "$bookname"
	cd "$bookname"

	discnum='0'

	while [ $discnum -lt $totaldisks  ]
	do

		discnum=$(( $discnum + 1 ))
		thisdisc="Disc $discnum"

		# Wait for CD drive to be active
		echo "Please insert $thisdisc"
		wait_for_disk_to_be_inserted

		mkdir "$thisdisc"
		cd "$thisdisc"

		# Rip the darned thing!
		cdparanoia -B

		cd ..
		eject
	done

	# Exit the bookname directory
	cd ..
done
The script asks you for the audio book name and then how many discs are in that book. From here on the program needs no intervention at all except for placing the next disc in the drive when the previous is ejected. The script will then silently and automatically start ripping the next, until all are done.

This is great for when you are playing fullscreen games or don't want to switch windows around to rip your audiobooks. The whole process is automatic and the rips are placed in the folder structure bookname/discnumber in the directory ( where the script was run from ).

Finally, once a whole book is finished, the script loops ( finally interrupting the user with a beep ) and asks for a new book name.

But one part of it is particularly hacky. The wait_for_disk_to_be_inserted() function abuses dd on /dev/sr0, piping errors to a temporary file which is then grepped for a keyword that determines whether or not a disc in in the drive. If anyone can suggest something better, *please* do. I'm sure this function will not work on all systems as-is, and therefore I would like to stress again that this script comes with NO WARRANTY WHATSOEVER as written in the GPL v3 and above.

Regards, William
Posted in Uncategorized
Views 838 Comments 2
« Prev     Main     Next »
Total Comments 2

Comments

  1. Old Comment
    check out abcde, it allows you to rip to multiple formats and name the files anyway you like. Pop in a CD and type abcde... if you really got fancy, you could write a front end to be triggered by udev or something that would auto rip anything new when inserted.
    Posted 02-19-2011 at 06:03 PM by lumak lumak is offline
  2. Old Comment
    Quote:
    Originally Posted by lumak View Comment
    check out abcde, it allows you to rip to multiple formats and name the files anyway you like. Pop in a CD and type abcde... if you really got fancy, you could write a front end to be triggered by udev or something that would auto rip anything new when inserted.
    Thanks lumak, I'll look into abcde
    Posted 02-19-2011 at 09:00 PM by William (Dthdealer) William (Dthdealer) is offline
 

  



All times are GMT -5. The time now is 12:35 PM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration