Ubuntu – Batch-convert and split .ape with .cue to .flac in various folders from terminal

14.04command lineflacformat conversion

I have a 44GB (yes, forty-four) folder of .ape CD files. Each .ape is a complete CD, and there is usually a .cue file along with each .ape. I need to convert but not split these, since there's no cue data to go by. There are also some .jpgs and .logs in there too, which I don't care about. If a tree command output helps visualize this, here you go:

├── Philips Mozart Collection - 180 CD
│   │   ├── box01-Symphonies
│   │   │   ├── 1B.jpg
│   │   │   ├── 1F.jpg
│   │   │   ├── cd01
│   │   │   │   ├── CDImage.ape
│   │   │   │   ├── CDImage.ape.cue
│   │   │   │   ├── Complete Mozart Edition, Vol. 01, Early Symphonies (Disc 1) - Neville Mariner, Academy of St. Martin in the Fields.log
│   │   │   │   ├── img421.jpg
│   │   │   │   └── img422.jpg
│   │   │   ├── cd02
│   │   │   │   └── CDImage.ape
│   │   │   ├── cd03
│   │   │   │   ├── CDImage.ape
│   │   │   │   ├── CDImage.ape.cue
│   │   │   │   ├── Complete Mozart Edition, Vol. 01, Early Symphonies (Disc 2) - Sir Neville Marinner - Acadamy of St Martin in the Fields.log
│   │   │   │   ├── img426.jpg
│   │   │   │   └── img427.jpg
│   │   │   ├── cd04

etc… all the way to 180 CDS.

Basically, I want to split all the .ape files into multiple .flac files, and copy over the tag info from the .cue, keeping the directory structure (preferably in a completely new and different directory). I prefer a single-line command to a script if possible. How would I go about this? Thanks to anyone who knows how to do this!

And before anyone flags this question as a duplicate to this one here:
Convert all .ape files to .flac in different subfolders, I would like to point out that that user didn't need to split the .ape into multiple .flacs.

Best Answer

I would do this in multiple steps, since you do this rarely. Transparency is more important than automation here. But a single line is also possible. I assume that if you use only one command for all the files each step, this is what you intended.

You need to have ffmpeg (or avconv), shntool and cuetools installed.

To convert all your .ape to .flac in place:

find . -name "*.ape" -exec sh -c 'exec ffmpeg -i "$1" "${1%.ape}.flac"' _ {} \;

If you install the patched MAC encoder and decoder for APE files from http://www.etree.org/shnutils/shntool/, this first step is unnecessary. But you would need to compile it with g++ and yasm yourself. Same goes for .tta files, which are also often used instead of .ape.

Split and name .flac files:

find . -name "*.cue" -exec sh -c 'exec shnsplit -f "$1" -o flac -t "%n_%p-%t" "${1%.cue}.flac"' _ {} \;

If shnsplit is also used for the conversion, replace the .flac in "${1%.cue}.flac" with the extension of the format you are converting from.

  • %n – track number
  • %p – performer
  • %t – track name

is taken from the .cue file for the .flac file names. To tag the resulting, split .flac files:

find . -name "*.cue" -execdir sh -c 'exec cuetag "$1" *.flac' _ {} \;

Remove the remaining CDImage.ape.cue, CDImage.flac, and CDImage.ape files at your leisure. The names are unique, so a simple find . -name "CDImage*" -delete is sufficient, except if Mozart made a composition starting with CDImage which I am not aware of.

For shnsplit to work, all necessary encoders/decoders need to be installed so it can read and write files. Modules for APE (and for the TTA format) would need to be compiled from source, the others by installing the package (i. e. FLAC).

This works for all standard cases where .cue and .tta/.flac/.ape files have the same name, except for the extension.


Side remark for others: If you deal with this conversion, your source files might be of Japanese origin, where APE and TTA were more popular than elsewhere. If the .cue files were initially SHIFT-JIS coded and now opened as UTF-8, the shnsplit step will break. You need to rename the .cue files with

find . -name "*.cue" -exec rename 's/\.cue$/.cux/' -- {} +

and convert the intermediate files to UTF-8 with

find . -name "*.cux" -exec sh -c 'exec iconv -f SHIFT-JIS -t UTF-8 -o "${1%.cux}.cue" -- "$1"' _ {} \;

before you attempt to do the steps above. Don't forget to get rid of the .cux files when you are done with:

find . -name "*.cux" -delete
Related Question