|
|
Since MPD still doesn't support (external) cuesheets, I decided to split those full-album-length ape/flac/tta files into one song per file.
Here is a little script I've came up with just for this purpose.
It basically reads the cuesheet (if not specified, the one in the same dir with the big guy), splits the (decoded) big guy into little brothers and sisters, and encodes them to flac.
It also writes tags to the resulting flacs. ALBUM, ARTIST, TRACKNUMBER, and TITLE, that is.
- #!/bin/bash
- function help()
- {
- cat << HELP
- % nocue % < beta >
- ==> Splits big ape|flac|tta|wv|wav into pieces with tags in them
- ==> Needs a cuesheet, bchunk, and a little disk space
- ==> Output format can be: [flac]
- Usage: `basename "$0"` [-c cuesheet] file
- HELP
- }
- function fullpath()
- {
- local DIR NAME
- DIR=$(cd "$(dirname "$1")" && pwd)
- NAME=$(basename "$1")
- echo "$DIR"/"$NAME"
- }
- SIG='LoLiLoLiCoN'
- CUE=''
- BIG=''
- OPT_TEMP=$( getopt --longoptions cue:,help --options c:h -- "$@" )
- eval set -- "$OPT_TEMP"
- while : ; do case "$1" in
- --cue|-c) CUE=$(fullpath "$2") ; shift 2 ;;
- --help|-h) help ; exit 0 ;;
- --) shift ; break ;;
- *) help ; exit 1 ;;
- esac ; done
- BIG="$1"
- if [ ! -f "$BIG" ] ; then help ; exit 1 ; else BIG=$(fullpath "$BIG") ; fi
- DIR=$(dirname "$BIG") && cd "$DIR"
- if [ "$?" -gt "0" ] ; then echo "==> Can't enter working dir. Quit..." ; exit 1 ; fi
- if [ -f "$SIG"* ] ; then echo "==> WTF? $SIG stolen?!" ; exit 1 ; fi
- case "$BIG" in
- *.[Aa][Pp][Ee])
- mac "$BIG" "$SIG".wav -d ;;
- *.[Ff][Ll][Aa][Cc])
- flac -d -o "$SIG".wav "$BIG" ;;
- *.[Tt][Tt][Aa])
- ttaenc -d -o "$SIG".wav "$BIG" ;;
- *.[Ww][Vv])
- wvunpack "$BIG" -o "$SIG".wav ;;
- *.[Ww][Aa][Vv])
- mv "$BIG" "$SIG".wav ;;
- *) echo "==> Unsupported input. Quit..." ; exit 1 ;;
- esac
- if [ "$?" -gt "0" ] ; then echo "==> Decoder required. Grab it and try again. Quit..." ; exit 1 ; fi
- if [ -z "$CUE" ] ; then
- N_CUE=$(ls -1 *.[Cc][Uu][Ee] | wc -l)
- if [ "$N_CUE" == "1" ] ; then CUE=$(ls *.[Cc][Uu][Ee])
- else
- echo "==> There are more than 1 cuesheets in the same dir."
- ls *.[Cc][Uu][Ee]
- echo "==> Use -c option to specify one. Quit..." ; exit 1
- fi
- fi
- bchunk -w "$SIG".wav "$CUE" "$SIG"
- IFS='
- '
- TITLES=($(grep 'TITLE' "$CUE" | cut -d" -f2))
- ARTISTS=($(grep 'PERFORMER' "$CUE" | cut -d" -f2))
- ALBUM="${TITLES[0]}"
- N_TRACK="${#TITLES[@]}"
- unset IFS
- for ((N=1 ; N < N_TRACK ; N++)) ; do
- NN=$(printf '%.2d' $N)
- flac -T "TRACKNUMBER=$NN" -T "ARTIST=${ARTISTS[$N]}" \
- -T "ALBUM=${ALBUM}" -T "TITLE=${TITLES[$N]}" \
- -o "$NN. ${TITLES[$N]}".flac "$SIG"$NN.wav
- #GENRE? YEAR? DATE? no, keep it simple. You can do the tag stuff later anyway.
- done
- rm -f "$SIG"*.wav
复制代码 But there are several things I'd like to ask/point out:
1. cuesheets are not always encoded in utf8, which means it might get read incorrectly. iconv is great at charset conversion, but you must specify the input encoding yourself. Is there a way to automate input encoding detection, so all this charset conversion becomes painless? AFAIK, enca needs a language option and file doesn't always give you the right answer. If this automation can't be achieved, you'll have to check if the cuesheet is read correctly under your locale.
2. The script is based on my cuesheet format assumption. I don't know how a cuesheet can differ from others. But I used this template below. If I missed something, please let me know.
# The 1st and 4th line, as well as all the ISRC lines are omitted.
3. Output format can be easily enhanced. ogg, mp3, aac, or whatever you like. But I just like it simple.
4. Well, I do know about shntool and cue2tracks, but never got them working properly... Guess I'm just too lazy to try them out.
Please help me to improve this.  |
|