#!/bin/bash # $Id: audiocd-write,v 1.4 2003/07/05 03:07:51 nturner Exp $ # # audio2cd - make an audio cd from a directory of mp3 or ogg files # # created 04-05-2001 by Tim Winter # hacked on by Nathaniel Turner # # Depends: mpg123, ogg123, sox, cdrecord # # Usage: # audio2cd [--keep-temps|--del-temps] [DIR] [TEMP_DIR] # set -e function main { # Set defaults BURN_AS_ROOT= KEEP_TEMPFILES=0 TEST_ONLY=0 MP3_DECODER=mpg123 OGG_DECODER=ogg123 TEMP_PREFIX="/var/tmp/audio2cd-`id -un`-" # Read configuration file, if it exists if [ -f "/etc/audio2cd.conf" ]; then source "/etc/audio2cd.conf" fi # Read command line parameters noMoreParams=0 while [ "$noMoreParams" = 0 ]; do case "$1" in --keep-temps|-k) shift KEEP_TEMPFILES=1 ;; --del-temps|-d) shift KEEP_TEMPFILES=0 ;; --test-only|-t) shift TEST_ONLY=1 ;; *) noMoreParams=1 ;; esac done if [ "$1" ]; then srcDir="$1" else srcDir=`pwd` fi tmpDir="$TEMP_PREFIX$(basename "$srcDir")" # blank line for readability echo # Verify that source directory exists if [ -d "$srcDir" ]; then if [ -L "$srcDir" ]; then srcDir="$srcDir/." fi echo "Audio files will be read from '$srcDir'." tracks=`find "$srcDir" -maxdepth 1 '(' -name "*.mp3" -o -name "*.ogg" ')' \ -printf '%f\n' | sort` if [ -z "$tracks" ]; then echo "Error: The selected directory contains no mp3 or ogg files." return 1 fi echo "The audio CD will contain the following tracks:" echo "$tracks" else echo "Error: '$srcDir' is not a directory." echo "USAGE: $(basename $0) [sourceDir]" return 1 fi # Output some info about the specified options if [ "$TEST_ONLY" = 1 ]; then echo "This is just a test; a CD will not be written." fi if [ "$KEEP_TEMPFILES" = 1 ]; then echo "Decoded wav files will be retained in $tmpDir." else echo "Decoded wav files will be deleted after successfully burning the CD." fi # Create temp directory if it doesn't exist if [ ! -d "$tmpDir" ]; then mkdir -p "$tmpDir" fi # Decode compressed audio in $srcDir to wav files in $tmpDir decodeFiles "$srcDir" "$tmpDir" || return 1 # Calculate the total size of the wav files to # make sure they will fit on the CD #SIZE=`du -bsS --exclude=*.mp3 "$tmpDir" | awk '{print $1;}'` SIZEMB=`du -hsS --exclude=*.mp3 "$tmpDir" | awk '{print $1;}'` echo "Total size of .wav files: $SIZEMB" echo "Writing CD:" cd "$tmpDir" || echo "unable to enter $tmpDir" if [ "$TEST_ONLY" = 1 ]; then echo "This is a just a test, so not actually burning the CD." else $BURN_AS_ROOT cdrecord -v -dao -eject -pad -audio *.wav fi if [ "$PIPESTATUS" = 0 ]; then echo "CD successfully written." if [ "$KEEP_TEMPFILES" = 1 ]; then echo "Leaving temp files in $tmpDir." else echo -n "Removing temporary files... " rm -f "$tmpDir"/*.wav && rmdir "$tmpDir" && echo OK fi else echo "Error: Something went wrong while trying to write the CD." return 1 fi } function decodeFiles { srcDir="$1" tmpDir="$2" # For each audio file in $srcDir, make an appropriately formatted wav file # in $tmpDir (44.1 kHz, stereo) by decoding and filtering through sox find "$srcDir" -maxdepth 1 -name "*.mp3" -o -name "*.ogg" \ | sort | while read f; do f=$(basename "$f") if [ ! -f "$tmpDir/$f.wav" ]; then case "$f" in *.mp3) echo -n "Decoding: $f (using $MP3_DECODER) ... " nice $MP3_DECODER -q -s -r 44100 "$srcDir/$f" \ | nice sox -t raw -r 44100 -c 2 -w -s - \ -r 44100 -c 2 -t wav -w -s "$tmpDir/$f.wav" || return 1 ;; *.ogg) echo -n "Decoding: $f (using $OGG_DECODER) ... " nice $OGG_DECODER -q -d au -f - "$srcDir/$f" 2>/dev/null\ | nice sox -t au - \ -r 44100 -c 2 -t wav -w -s "$tmpDir/$f.wav" || return 1 ;; esac if [ "${PIPESTATUS[1]}" = 0 ]; then echo "OK" else echo "Error." if [ -f "$tmpDir/$f.wav" ]; then rm "$tmpDir/$f.wav" && \ echo "Removed partly decoded tempfile." fi return 1 fi else echo "Using existing wav file for $f." fi done } main "$@"