Announcement

Collapse
No announcement yet.

ape to flac, alac to flac, wav to flac overnight with a one-liner

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    ape to flac, alac to flac, wav to flac overnight with a one-liner

    [N.B.: I don't know where to put this. Maybe it can go in a wiki. Maybe it or something better should be in the slimserver faq. If you know how to get it into the wiki and you feel like explaining it to me, I'd appreciate it and I might even do it.]

    If you feel that free is good, Propietary is bad, obsolete is bad, and/or MS windows-centric is bad, you can take your tracks to flac.

    Would you like to switch all your music tracks from ape to flac, wav to flac, or alac (apple lossless) to flac by typing a single line, and letting the machine do the rest of the work? Would you like to do this without using any more hard drive space than that which will be taken up by the tracks when they are finished? Would you like to do this without consuming precious cartilage with repetitive mouse motions? Would you like to do this without having to explain to your loved ones and your superiors why you are spending so much time in front of your computer?

    If you are running linux or OS X you can do this from the shell ("Terminal" in the Mac).

    Caveat: you will need to re-tag. However, with the directory structure inherited from iTunes this can in principle be automated. Mediarage is a program that can do such a thing. But yes, you will need to re-tag. In this message I only talk about how to migrate to flac.

    I give the long explanation first, then summarize. You can skip down to the summary and work from there as well. I have tailored this description towards the Apple people because the linux people know most if not all of this already and also because I've barely used linux since 2002. If it doesn't work in linux then rtfm, tweak and share.

    You will need the following programs: mac, flac, and alac. The first two are available, among other places, within the xACT distribution; if you install xACT, you will find them in the directory

    /Applications/xACT.app/Contents/Resources/Binaries/bin

    For the alac binary, you can find it under "Apple Lossless" at

    Audio, Lossless, WavPack, Monkey's Audio, FLAC


    You will also need a couple of shell scripts like the ones I wrote and appended at the end of this message.

    I use the "terminal", because that lets me do the conversion as a batch job on an entire directory and all its subdirectories using a single one-line command. By contrast, using xACT or some other GUI usually involves doing a lot of dragging and dropping, one directory at a time. With the terminal, I can type the one line, leave the machine alone for a while (overnight if need be) and when it's done, all has been converted.

    Another advantage of this method is that I don't need to temporarily use much hard drive space to hold my "intermediate" files. For example, you might notice this if you convert from within iTunes: first you convert all your .m4a to .wav (say), and then you delete your .m4a's; there is a point at which you have both versions of each track on the hard drive, and that requires a large buffer. With the shell command method, files are converted and deleted one at a time, so you only need as much hard drive space as will be taken up by the final format.

    The conversions all have a similar procedure. You cd to the top of the directory hierarchy where the files reside (e.g. ~/Music/ ), and then execute a command that looks like this:

    find . -name \*.XXX -execdir CONVERSION {} \;

    where "XXX" is the extension of the files that are to be changed, and "CONVERSION" is the particular program that does the conversion. Usually I've had to write a small shell script to implement "CONVERSION", using the conversion utilities that come with the xACT program.

    For example, when I'm changing monkeyaudio files to flac, I execute:

    find . -name \*.ape -execdir monkey2flac {} \;

    where "monkey2flac" is a shell script that you will find towards the end of this message.

    In order for all this to work, you need to be able to execute your own programs from within the shell. I do this by creating a subdirectory in my home directory called "bin", where I put all my executables; there are other ways to do this. Then I take each of the required conversion programs that came with xACT and I either copy them to ~/bin or I do a soft link. In ~/bin I also place my shell scripts. I make sure all the files in ~/bin are executable by typing at the shell the command:

    chmod 755 ~/bin/*

    You should put ~/bin in your executable path, which can be done by inserting this line in the file ~/.bashrc :

    export PATH=$PATH:~/bin

    Also make sure that your terminal is booting up properly. One way to do this is to go into the preferences in Terminal, select the "Execute this command" option and use this:

    /bin/bash -i


    To summarize:

    1. Create a directory for your executables, e.g. ~/bin
    2. Obtain alac, flac, and mac and either put copies of them in ~/bin, or softlink them there.
    3. Put ~/bin in your executable path.
    4. Put the shell scripts monkey2flac, alac2flac in your ~/bin directory.
    5. To perform the conversion go to the top of the directory hierarchy where the files to be converted are located and type:

    a) *.ape to *.flac:

    find . -name \*.ape -execdir monkey2flac {} \;

    b) *.wav to *.flac (does not require shell script):

    find . -name \*.wav -execdir flac --replay-gain -V --delete-input-file {} \;

    c) *.flac to *.wav (does not require shell script):

    find . -name \*.flac -execdir flac -d --delete-input-file {} \;

    d) *.m4a to *.flac:

    find . -name \*.m4a -execdir alac2flac {} \;

    The scripts follow below.

    Here is script "monkey2flac", which should be placed in ~/bin (don't include the "===" lines!):

    ===========================
    #!/bin/sh

    # infile should be of the form basename.ape

    infile=$1

    # outfile is built by stripping the .ape extension first

    midfile=`basename "$1" .ape`

    # If there is a problem with the name then exit

    [ "$midfile" = "$infile" ] && exit 1

    # Else set midfile name to basename.wav

    midfile=$midfile.wav

    # Now use mac to transcode from ape to wav, then remove the ape file

    mac "$infile" "$midfile" -d
    rm "$infile"

    # Now we go from wav to flac and we use flac's arguments to delete the
    # intermediate wav file, set replay gain and verify that the
    # transcoding was successful. One can also set the compression higher
    # or lower here, but setting it at the highest level raises the
    # cpu time by a factor of five or so

    flac --replay-gain -V --delete-input-file "$midfile"
    ===========================





    Here is the script "alac2flac" :

    ===========================
    #!/bin/sh

    # infile should be of the form basename.ape

    infile=$1

    # outfile is built by stripping the .ape extension first

    midfile=`basename "$1" .m4a`

    # If there is a problem with the name then exit

    [ "$midfile" = "$infile" ] && exit 1

    # Else set midfile name to basename.wav

    midfile=$midfile.wav

    # Now use alac to transcode from m4a to wav, then remove the m4a file

    alac -f "$midfile" "$infile"
    rm "$infile"

    # Now we go from wav to flac and we use flac's arguments to delete the
    # intermediate wav file, set replay gain and verify that the
    # transcoding was successful. One can also set the compression higher
    # or lower here, but setting it at the highest level raises the
    # cpu time by a factor of five or so

    flac --replay-gain -V --delete-input-file "$midfile"

    ===========================

    Cheers,

    Ariel
Working...
X