Tuesday, December 04, 2007

[update] Convert .ape to .flac

There is a new audio format around: the monkey's audio codec or file. To make the conversion you will need the shntools (RPM) and MAC (RPM). Notice that both RPMs are for Fedora 7, but they work. If found a Fedora 8 version, please let me know. This document is a customized version from other two documents: this and this.

- Set some variables:

CUE_FILE=<full path name to the CUE file>

#---
# assuming that the APE/FLAC file has the same name as the CUE file
APE_FILE="`basename \"${CUE_FILE}\" .cue`.ape"
WAV_FILE="`basename \"${CUE_FILE}\" .cue`.wav"
FLAC_FILE="`basename \"${CUE_FILE}\" .cue`.flac"
#---


- Convert APE file to WAV and to FLAC (if instead of an APE file it is a FLAC file you should skip this step):

#---
mac "${APE_FILE}" "${WAV_FILE}" -d
flac --best "${WAV_FILE}" "${FLAC_FILE}"
rm -f "${WAV_FILE}"
#---


- Break and convert to flac:

#---
cuebreakpoints "${CUE_FILE}" | shnsplit -o flac "${FLAC_FILE}"
#---


- Rename single tracks into music names, based on CUE file:

[UPDATE]
This script does NOT check character set validity. It means that it may not work for some particular case (or the general case in some languages). A simple example would be a CUE file in French in ISO-8859-1 (Windows default) and a Linux in Unicode (UTF-8). Oder problem also related with character set is the protection for non-valid character like ":" or "/", which may not be used within a file name. In any case the script will simply not rename the file (which will be left to the user to do). So, in the worst case, you will need to rename the failed files by hand. Useful for that will be to run the following command:

#---
grep -e "^[[:space:]]\+TITLE" "${CUE_FILE}"
#---
[UPDATE]


#---
COUNT=1;
## the first TITLE is for the album name, discard it
grep -e "^[[:space:]]\+TITLE" "${CUE_FILE}" \
| sed -e "s/.*\"\(.*\)\".*/\1/g" \
| while read TRACK_NAME;
do \
## just a little fine tune to have track names like
### "artist - 01 - track.flac" and "artist - 10 - track.flac"
if [ ${COUNT} -lt 10 ];
then \
NUM="0${COUNT}";
else \
NUM="${COUNT}";
fi;
ARTIST=`grep -e "^[[:space:]]\+PERFORMER" "${CUE_FILE}" \
| head -${COUNT} | tail -1 | \
sed -e "s/.*\"\(.*\)\".*/\1/g"`
## rename in the format: "<artist> - <track number> - <track name>.flac"
mv split-track${NUM}.flac "${ARTIST} - ${NUM} - ${TRACK_NAME}".flac;
COUNT=$(($COUNT+1));
done
#---