Skip to content

Instantly share code, notes, and snippets.

@gelin
Created February 20, 2020 15:38
Show Gist options
  • Save gelin/0846292621fd0a8a0e0f8633f7246296 to your computer and use it in GitHub Desktop.
Save gelin/0846292621fd0a8a0e0f8633f7246296 to your computer and use it in GitHub Desktop.
Script to split flac/ape CD rips by cue data and encode tracks to ogg vorbis files
#!/bin/sh
# Script to split flac/ape CD rips by cue data and encode tracks to ogg vorbis files.
#
# Put the script to a directory in your PATH.
#
# And then go to a directory containing .cue file and .flac/.ape file and run the command:
# cue-split.sh
#
# Or if the .cue is not in utf-8, decode it providing the -c flag with the original encoding:
# cue-split.sh -c cp1251
#
# Result:
# Original .flac/.ape and .cue files are moved to orig/ subfolder.
# Each track is extracted/encoded to current folder with the name like "trackNumber - trackTitle.ogg"
# Each track is tagged with data from .cue file.
#
# Dependencies:
# libc-bin (iconv) - to convert encoding of .cue file
# libav-tools (avconv) - to decode .ape file
# shntool (shnsplit) - to split .flac/.ape by .cue data
# vorbis-tools (oggenc) - to encode to .ogg
# cuetools (cuetag) - to apply tags from .cue to .ogg
while getopts ":c:" opt; do
case $opt in
c)
ICONV_FROM="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
set_first () {
eval "$1=\"\$2\""
}
if [ -f *.ape ]
then
set_first SND_FILE *.ape
DECODE_APE=true
elif [ -f *.flac ]
then
set_first SND_FILE *.flac
else
exit 1
fi
if [ -n "$ICONV_FROM" ]
then
for f in *.cue
do
echo "Converting $f from $ICONV_FROM to UTF-8"
iconv -o "${f%.cue}.utf8.cue" -f "$ICONV_FROM" -t UTF-8 "$f"
done
fi
if [ -f *.utf8.cue ]
then
set_first CUE_FILE *.utf8.cue
elif [ -f *.cue ]
then
set_first CUE_FILE *.cue
else
exit 1
fi
mkdir -p orig || exit 2
mv *.log orig
mv "$SND_FILE" orig
SND_FILE="orig/$SND_FILE"
mv *.cue orig
CUE_FILE="orig/$CUE_FILE"
if [ -n "$DECODE_APE" ]
then
avconv -i "$SND_FILE" "${SND_FILE%.ape}.wav" || exit 3
SND_FILE="${SND_FILE%.ape}.wav"
fi
shnsplit -f "$CUE_FILE" -t "%n - %t" -o "cust ext=ogg oggenc -q 5 -o %f -" "$SND_FILE" || exit 3
if [ -n "$DECODE_APE" ]
then
rm "$SND_FILE"
fi
rm -f 00*
cuetag "$CUE_FILE" *.ogg || exit 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment