1#! /bin/sh
2# @(#)cdda2mp3  1.5 10/02/14 Copyright 2010 J. Schilling
3#
4# Demo script for processing all audio tracks with a mp3 decoder
5# based on a news article by Tom Kludy
6#
7# usage: cdda2mp3 <name prefix for all mp3 files>
8#
9# list_audio_tracks is a (symbolic) link to cdda2wav
10# and used to generate a list of audio track numbers and start
11# sectors, which in turn are used in a loop to spawn cdda2wav
12# and the post processor on a track by track basis.
13###########################################################################
14# The contents of this file are subject to the terms of the
15# Common Development and Distribution License, Version 1.0 only
16# (the "License").  You may not use this file except in compliance
17# with the License.
18#
19# See the file CDDL.Schily.txt in this distribution for details.
20# A copy of the CDDL is also available via the Internet at
21# http://www.opensource.org/licenses/cddl1.txt
22#
23# When distributing Covered Code, include this CDDL HEADER in each
24# file and include the License file CDDL.Schily.txt from this distribution.
25###########################################################################
26
27# specify the sampling program and its options
28# do not specify the track option here!
29CDDA2WAV=cdda2wav
30CDDA2WAV_OPTS='-H -q'
31CDDA2WAV_XOPTS=''
32
33# for normal use, comment out the next line
34#DEBUG='-d1'
35
36# the post processor is fed through a pipe to avoid space waste
37# specify the post processing program and its options
38MP_CODER=lame
39MP_CODER_HELP=--help
40MP_OPTIONS=''
41MP_XOPTS=''
42
43found_fileprefix=FALSE
44
45$MP_CODER $MP_CODER_HELP > /dev/null 2> /dev/null
46if [ $? != 0 ] ; then
47	echo '"'$MP_CODER'"' "not found. Install first!"
48	exit 1
49fi
50
51usage() {
52	echo "Usage: cdda2mp3 [dev=<SCSI-address>] [cdda2wav options] [<name prefix for all mp3 files>] [ogg options]"
53}
54
55while [ $# -gt 0 ]; do
56	case "$1" in
57	-help)
58		usage
59		exit 0
60		;;
61
62	dev=*)
63		CDDA2WAV_DEV="$1"
64		shift
65		;;
66
67	-*)
68		if [ $found_fileprefix = TRUE ]; then
69			MP_XOPTS="$MP_XOPTS $1"
70		else
71			CDDA2WAV_XOPTS="$CDDA2WAV_XOPTS $1"
72		fi
73		shift
74		;;
75
76	*)
77		if [ $found_fileprefix = TRUE ]; then
78			echo "Too many file type args."
79			usage
80			exit 1
81		fi
82		FILEPREFIX=${1-audiotrack}
83		found_fileprefix=TRUE
84		#echo "arg:  $1"
85		#echo "args: $@"
86		shift
87		;;
88	esac
89done
90
91if [ -r /etc/default/cdda2mp3 ]; then
92	. /etc/default/cdda2mp3 2>/dev/null
93fi
94TRACKS=`$CDDA2WAV $CDDA2WAV_DEV -H -J -vtoc -N -g 2>&1 | grep '^T..:' | sed -e 's/T\(..\):.*/\1/g'`
95if [ -z "$TRACKS" ]; then
96	$CDDA2WAV $CDDA2WAV_DEV -H -J -vtoc -N -g
97	exit 1
98fi
99
100for TRACK in $TRACKS; do
101	NAME=$TRACK-$FILEPREFIX.mp3
102	$CDDA2WAV $CDDA2WAV_DEV $CDDA2WAV_OPTS -t$TRACK $DEBUG $CDDA2WAV_XOPTS -Owav - | \
103	$MP_CODER $MP_OPTIONS $MP_XOPTS - > $NAME
104
105	# check result code
106	RES=$?
107	if [ $RES = 0 ] ; then
108		echo File '"'$NAME'"' finished successfully.
109	else
110		echo File '"'$NAME'"' failed \(result $RES\). Aborted. >&2
111		break
112	fi
113done
114