Bash Script to rip CD/DVD ISO image on OS X

Posted by: admin  :  Category: Operating Systems, OS X, Programming, Scripting

Apple’s OS X has an easy way to rip a CD/DVD image using Disk Utility program.
However, you’ll end up with a file in that is not in ISO format, thus utterly useless if you want to re-use the file for virtualization purposes or on another operating system unable to handle those .cdr files.

For a one-shot option, OS X provides everything to convert the .cdr to .iso files, which is outlined at http://imacify.com/2013/06/how-to-create-iso-disc-image-from-cddvd-in-mac-os-x/.

If you do however plan to rip a lot (and I mean, a lot!) of CDs/DVDs to ISO files on OS X, here’s a little bash script I came up with.

This script essentially does the following:

  1. It waits for a disc to be inserted
  2. It scans the disk label and writes an ISO file to your ~/Desktop/rips directory using the dd command
  3. go back to 1 😉

So essentially, it’s made to do mass ripping. I needed it for myself as I finally wanted to rip my gazillion of physical data CDs/DVDs onto my file server. And since I’m a lazy guy, the script shall do as much as possible by itself without bugging me too much 😉

So if you think, that could be of use to you as well, here’s a one-liner so you can call it up directly into your shell:

wget -qO- http://phunsites.net/scripts/ripiso.sh.gz | gunzip | bash

Calling the script like this, will assume non-interactive mode, i.e. the script will run endlessly and no questions will be asked.
Press CTRL-C to terminate the script.

You could also run it interactively, the script will then ask some Y/N questions. To do so, download the script and run ith with the -i (interactive) argument like this:

wget -qO- http://phunsites.net/scripts/ripiso.sh.gz | gunzip > ripiso.sh
./ripiso -i

Here’s also a direct download link: http://phunsites.net/scripts/ripiso.sh.gz, in case you want to inspect the script first.

The script tries to be somewhat informative, here’s an example output of the non-interactive mode:

$ wget -qO- http://phunsites.net/scripts/ripiso.sh.gz | gunzip | bash
Waiting for CD/DVD to be inserted ...
Waiting for CD/DVD to be inserted ...
Waiting for CD/DVD to be inserted ...
Waiting for CD/DVD to be inserted ...
Waiting for CD/DVD to be inserted ...
Found CD/DVD disk=/dev/disk1, volume=SOL_10_305_SPARC on /Volumes/SOL_10_305_SPARC from /dev/disk1s0

Unmounting disk from /Volumes/SOL_10_305_SPARC ...
Volume SOL_10_305_SPARC on disk1s0 unmounted
Ripping CD/DVD to /Users/Gianpaolo/Desktop/rips/SOL_10_305_SPARC.iso ...
0+0 records in
0+0 records out
0 bytes transferred in 0.043732 secs (0 bytes/sec)
14364+0 records in
14364+0 records out
7354368 bytes transferred in 5.015581 secs (1466304 bytes/sec)
33065+0 records in
33065+0 records out
[ ... some output omitted ... ]
1567823+0 records in
1567823+0 records out
802725376 bytes transferred in 420.424699 secs (1909320 bytes/sec)
Ejecting CD/DVD ...
Disk /dev/disk1 ejected
CD/DVD rip completed after 425 seconds.
Waiting for CD/DVD to be inserted ...
Waiting for CD/DVD to be inserted ...
Waiting for CD/DVD to be inserted ...
Waiting for CD/DVD to be inserted ...

And here’s also the source code of it for your convenience:

#!/bin/bash
#
# ripiso.sh
# a helper script to easily rip inserted DVD/CD to an ISO file
#
# Copyright (c) 2014 Gianpaolo Del Matto, http://phunsites.net. All rights reserved.
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
#    1) Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#
#    2) Redistributions in binary form must reproduce the above copyright notice,
#       this list of conditions and the following disclaimer in the documentation
#       and/or other materials provided with the distribution.
#
# history:
#
# r0.1 2014/08/03 Gianpaolo Del Matto, http://phunsites.net
#
#
# info:
#
# most current version is always available through:
#
# wget -qO- http://phunsites.net/scripts/ripiso.sh.gz | gunzip | bash
#


# local vars
#
force_non_interactive=1


# sanity checks: create the output directory if missing
#
[ ! -d "~/Desktop/rips/" ] && mkdir -p "~/Desktop/rips/"


# parse cli args
#
[ ! -z "$1" -a "$1" = "-i" ] && force_non_interactive=0


# rip_iso will try to rip an ISO from any currently
# inserted / mounted CD/DVD
#
function rip_iso {
	start=`date +%s`

	# get disk info from inserted disk, bail out if none found
	#
	mountinfo=`mount | grep cd9660`
	[ "$?" != "0" ] && { echo "error: no CD/DVD found"; return 1; }


	devid=`echo ${mountinfo} | awk '{ print $1 }'`
	mountpoint=`echo ${mountinfo} | awk '{ print $3 }'`
	label=`basename ${mountpoint}`
	blockdev=`echo ${devid} | sed 's/s[0-9]$//g'`

	printf "Found CD/DVD disk=%s, volume=%s on %s from %s\n\n" ${blockdev} ${label} ${mountpoint} ${devid}

	while [ : ]; do
		# skip this if we're running non-interactive
		# assume the user wants it be done :-)
		#
		[ "${force_non_interactive}" = "1" ] && break

		printf "Continue? [Y|n] "
		read _answer < /dev/tty
		echo ${_answer} | grep -E -i -e '^y$' > /dev/null 2>&1
		[ "$?" = "0" ] && break || return 1
	done

	printf "Unmounting disk from %s ...\n" ${mountpoint}
	diskutil unmount ${mountpoint}

	printf "Ripping CD/DVD to %s ...\n" ~/Desktop/rips/${label}.iso
	dd if=$blockdev of=~/Desktop/rips/${label}.iso &

	while [ : ]; do
		clear
		killall -SIGINFO dd > /dev/null 2>&1
		[ "$?" != "0" ] && break

		sleep 5
	done

	printf "Ejecting CD/DVD ...\n"
	diskutil eject ${blockdev}

	end=`date +%s`
	time_elapsed=`expr ${end} - ${start}`
	printf "CD/DVD rip completed after %s seconds.\n" ${time_elapsed}

	return 0
}



# main
# ##########

while [ : ]; do
	# detect if mounted disk is seen
	#
	while [ : ]; do
		clear
		printf "Waiting for CD/DVD to be inserted ...\n"
		mount | grep cd9660 >/dev/null 2>&1
		[ "$?" = "0" ] && break
		sleep 1
	done


	# good, run the image ripper
	#
	rip_iso


	# skip this if we're running non-interactive
	# assume the user wants it be done :-)
	#
	if [ "${force_non_interactive}" = "0" ]; then
		# if we're running interactive, ask if
		# user wants to rip another disc
		#
		printf "Rip another disc? [Y|n] "
		read _answer < /dev/tty
		echo ${_answer} | grep -E -i -e '^y$' > /dev/null 2>&1
		[ "$?" != "0" ] && break
	fi
done

# END #

Comments are closed.