Batch-convert and split .ape with .cue to .flac in various folders from terminal
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 videos on Youtube
willem.hill
Electronics tinkerer, local computer repair guy, and musician.
Updated on September 18, 2022Comments
-
willem.hill almost 2 years
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.jpg
s and.log
s in there too, which I don't care about. If atree
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.
-
αғsнιη about 8 yearsHi, would you please explain more about your sentence "... and copy over the tag info from the .cue ...". Does it means the converted .flac files name should be pick-up from .cue file? Then would you please share sample content of it. Else how multiple files should be create and what is max number of these files? Thanks
-
emk2203 about 8 years>I need to somehow skip those, since there's no cue data to go by. What do you mean by this? Without cue data, you could convert , but not split, since the info about where tracks start and end are in the cue.
-
willem.hill about 8 years@Afshin.Hamadi Yes, the filename would match the track title. The .cue file has info such as artist, album, etc. This info should be copied to the new flac files.
-
emk2203 about 8 yearsYou are contradicting yourself even more. "I need to convert but not split these, since there's no cue data to go by." - "Basically, I want to split all the .ape files into multiple .flac files, and copy over the tag info from the .cue...". So what is it? If it is the standard "I have .ape and .cue files, and I want them converted and split in-place to tagged .flac", the question could have been closed with two good answers 24 hours ago.
-
willem.hill about 8 years@emk2203 Yes, that is what I want. I'm not sure what you're point is though.
-
emk2203 about 8 years@willem.hill: The point was just that you should have taken out your third sentence completely, since it contradicts what you really want. Confused the hell out of me for sure.
-
-
Csabi Vidó about 8 yearsshnsplit's manpage lists ape as a supported format so you can skip converting to flac with ffmpeg and use
-o flac
with shnsplit instead. Don't expect to find a standard basename and ape as the codec/container other lossless codecs are used too, so it's better to not hard code and it would be better to include all steps in one run of the find command.metaflac --add-replay-gain
would also be a good idea. -
emk2203 about 8 years@LiveWireBT: Technically, you are correct. This is why I said "two solutions available" in one of my comment. However, you need to download the source for the plugins for ape and tta support, install g++ and yasm and compile and install them yourself. I went with the ffmpeg solution since it is easier.
-
David Foerster about 8 yearsFor a one-command version:
find -name '*.ape' -execdir sh -c 'flac="${1%.ape}.flac"; ffmpeg -i "$1" "$flac" && shnsplit -f CDImage.ape.cue "$flac" && cuetag CDImage.ape.cue "$flac"' _ {} \:
-
willem.hill about 8 years@emk2203 Sorry about the long response time. Anyway, the command you gave to split the files is converting them all to
.wav
files. I need them to still be inflac
-
emk2203 about 8 yearsThe
-o flac
switch got lost somehow in theshnsplit
line. I added it now. -
willem.hill about 8 years@emk2203 Ok, thanks. If I run
find . -name "*.wav" -delete
it should delete the created.wav
's, right? -
emk2203 about 8 yearsYes. I would always run once without the delete switch first to make sure that it runs as intended.
-
willem.hill about 8 years@emk2203 When I run the third command, I get
warning: number of files does not match number of tracks
. I have deleted all theCDImage.flac files
as well as all thesplit-track00.flac
, thinking these where screwing it up. How can I get this to work correctly? And thanks for your help so much! -
emk2203 about 8 yearsPlease use the slightly revised instructions. Tried them, they work.
-
emk2203 about 8 years@DavidFoerster: Thanks for the one-liner, my revised version would be
find -name '*.cue' -execdir sh -c 'flac="${1%.ape}.flac"; shnsplit -f "$1" -o flac -t "%n_%p-%t" "$flac" && cuetag "$1" *.flac' _ {} \;
with a fully equippedshnsplit
. This works for ape, tta, and flac if the flac variable is defined accordingly. Is it possible to make the one-liner do this automatically? (Input .flac should be renamed to .flc first) So, for .tta --> flac="${1%.tta}.flac", for .flac --> flac="${1%.flc}.flac and for .ape as it is. I'm stuck here.