или так#!/bin/bash
# Copyright (C) 2007-2008 Vladimir V. Kamarzin <vvk@altlinux.org>
#
# Split one big audiofile (cdimage) to separate tracks
#
# This file 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#. shell-error
renice 19 -p $$ > /dev/null 2>&1 ||:
while read cuefile
do
# get cuesheet filename
cue="${cuefile##*/}"
# object (filename without extension)
obj=${cue%.cue}
# relative path to object's directory
objdir="${cuefile%/*}"
pushd "$objdir"
# First, find all files that need decoding before process and decode it
if [ -f "$obj".mp3 ]; then
lame --decode "$obj".mp3 "$obj".wav || fatal "cannot decode $obj.mp3, exiting"
elif [ -f "$obj".ogg ]; then
oggdec "$obj".ogg || fatal "cannot decode $obj.ogg, exiting"
fi
# Next, split one-big-file. Use flac for loseless and ogg for lossy
if [ -f "$obj".ape ]; then
cuebreakpoints "$cue" | shnsplit -o 'flac flac --best -o %f -' -t %n "$obj".ape \
|| fatal "failed to split $obj.ape"
elif [ -f "$obj".flac ]; then
cuebreakpoints "$cue" | shnsplit -o 'flac flac --best -o %f -' -t %n "$obj".flac \
|| fatal "failed to split $obj.flac"
elif [ -f "$obj".wav ]; then
cuebreakpoints "$cue" | shnsplit -o 'cust ext=ogg oggenc -q 7 -o %f -' -t %n "$obj".wav \
|| fatal "failed to split $obj.wav"
else
fatal "$obj file not found, exiting"
fi
# Recode tags. Assume that russian tags has windows-1251 encoding
case "$LANG" in
ru_RU.KOI8-R) recode_to=koi8-r
;;
ru_RU.CP1251) recode_not_needed=1
;;
*) recode_to=utf8
esac
[ "$recode_not_needed" = 1 ] && ln -s "$cue" tmp.cue || recode -f 1251.."$recode_to" < "$cue" > tmp.cue \
|| fatal "cannot recode cue: $cue"
# Embed tags into tracks, rename tracks
for (( i=1 ; i <= $(cueprint -d '%N' tmp.cue) ; ++i ))
do
NN=$(printf '%02d' $i)
[ -s "$NN.flac" ] && {
# Prepare track tags, filter out empty tags, embed the rest
cueprint -n $i -t \
'ARRANGER=%A\nCOMPOSER=%C\nGENRE=%G\nMESSAGE=%M\nTRACKNUMBER=%n\nARTIST=%p\nTITLE=%t\nALBUM=%T\n' \
tmp.cue | egrep -v '=$' | metaflac --import-tags-from=- $NN.flac
# rename NN.flac to "NN - TrackTitle.flac"
mv $NN.flac "$NN - $(cueprint -n $i -t %t tmp.cue | sed -e "s,/,,g").flac"
}
[ -s "$NN.ogg" ] && {
cueprint -n $i -t \
'ARRANGER=%A\nCOMPOSER=%C\nGENRE=%G\nCOMMENT=%M\nTRACKNUMBER=%n\nARTIST=%p\nTITLE=%t\nALBUM=%T\n' \
tmp.cue | egrep -v '=$' > tags.tmp
vorbiscomment -a -c tags.tmp "$NN.ogg"
mv $NN.ogg "$NN - $(cueprint -n $i -t %t tmp.cue | sed -e "s,/,,g").ogg"
}
done
rm -f tmp.cue "$cue" "$obj".{flac,ape,mp3,ogg,wav} tags.tmp
popd
done < <(find . -type f -iname "*.cue")