Skip to content

Instantly share code, notes, and snippets.

@lukebakken
Forked from anonymous/gist:df51d12829bb1dac40e0
Last active November 29, 2016 00:16
Show Gist options
  • Save lukebakken/7a0b923aaabbad044b50cf3f064fe39c to your computer and use it in GitHub Desktop.
Save lukebakken/7a0b923aaabbad044b50cf3f064fe39c to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -o errexit
set -o nounset
# This script needs mp3splt, cdrtools, vorbis-tools
CREATE_OGG="false";
CREATE_FLAC="true";
CREATE_MKA="false";
WORKING_DIRECTORY=$(pwd)
function usage()
{
echo "Usage: $0 [OUTPUT_DIRECTORY] device";
echo "eg.: $0 my_audio_cd /dev/sr0";
exit;
}
function no_cdtext()
{
echo "CD doesn't contain cd-text information";
echo "Please specify output directory";
echo "$0 OUTPUT_DIRECTORY device";
exit;
}
function remove_quotes() {
local OUTPUT_STRING
# remove quotes
OUTPUT_STRING=$(echo "$1" | sed "s/^\([\"']\)\(.*\)\1\$/\2/g")
# remove selected special characters
echo "$OUTPUT_STRING" | sed "s/\(.*\)[\\]\(.*\)/\1\2/g"
}
# store command line parameters
if [[ $# == 2 ]]
then
declare -r output_directory="$1"
declare -r cdrom="$2"
elif [[ $# == 1 ]]
then
declare -r cdrom="$1"
else
usage
fi
# retrieve CD information
echo "Retrieve CD information"
CD_INFO=$(cdda2wav -J -g -D "$cdrom" 2>&1)
# test if CD-Text is available
if [[ "detected" == $(echo "${CD_INFO}" | awk '/^CD-Text.*/ {print $2}') ]]; then
echo "CD-Text detected"
declare -r cd_text_detected='true'
ALBUM_INFORMATION=$(echo "${CD_INFO}" | grep ^"Album title")
# remove characters from beginning of string
ALBUM_INFORMATION=$(echo "${ALBUM_INFORMATION#*Album title: }")
ALBUM_TITLE=$(echo "${ALBUM_INFORMATION% from *}")
ALBUM_TITLE=$(remove_quotes "${ALBUM_TITLE}")
ALBUM_ARTIST=$(echo "${ALBUM_INFORMATION#* from }")
ALBUM_ARTIST=$(remove_quotes "${ALBUM_ARTIST}")
else
declare -r cd_text_detected='false'
fi
# check if cd has cdtext
if [[ $cd_text_detected == 'true' ]]
then
if [[ -n $output_directory ]]
then
echo "CD-text found"
echo "Overriding given output directory name"
fi
mkdir -p "./${ALBUM_ARTIST}/${ALBUM_TITLE}"
cd "./${ALBUM_ARTIST}/${ALBUM_TITLE}"
output_directory="${ALBUM_ARTIST}/${ALBUM_TITLE}"
declare -r output_filename="${ALBUM_TITLE}"
else
if [[ "$#" != "2" ]]; then
no_cdtext
else
if [[ ! -d $output_directory ]]
then
mkdir "$output_directory"
fi
cd "$output_directory"
declare -r output_filename="$output_directory"
fi
fi
cdda2wav -cuefile -paranoia -t all "dev=$cdrom" "$output_filename.wav" &> "$output_filename.log"
if [[ "true" = ${CREATE_OGG} ]]; then
mkdir ogg
pushd ogg
oggenc -o "$output_filename.ogg" -q 6 "../$output_filename.wav"
oggsplt -c "../$output_filename.cue" -o "@N - @t" "$output_filename.ogg"
popd
fi
if [[ "true" = ${CREATE_FLAC} ]]; then
mkdir flac
pushd flac
cd flac # -5 -V -T "ARTIST=%a" -T "ALBUM=%g" -T "DATE=%y" -T "GENRE=%m" --tag-from-file=CUESHEET="%a - %g.cue" --cuesheet="%a - %g.cue" %s
flac --cuesheet="../$output_filename.cue" --tag-from-file=CUESHEET="../$output_filename.cue" --output-name="./$output_filename.flac" --best "../$output_filename.wav"
popd
fi
if [[ "true" = ${CREATE_MKA} ]]; then
mkdir mka
pushd mka
# -5 -V -T "ARTIST=%a" -T "ALBUM=%g" -T "DATE=%y" -T "GENRE=%m" --tag-from-file=CUESHEET="%a - %g.cue" --cuesheet="%a - %g.cue" %s
flac --output-name="./$output_filename.flac" --best "../$output_filename.wav"
mkvmerge -q -o "$output_filename.flac.mka" "$output_filename.flac" --attachment-mime-type text/plain --attachment-description "cdda2wav Log" --attach-file "../$output_filename.log" --attachment-mime-type text/plain --attachment-description "Original CUE Sheet" --attach-file "../$output_filename.cue" --title "${ALBUM_TITLE}" --chapters "../$output_filename.cue"
rm "./$output_filename.flac"
popd
fi
# rm "${WORKING_DIRECTORY}/$output_directory/$output_filename.{cddb,cdindex,cdtext,wav}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment