14

I have a directory with about 50 wav files that I need to convert to caf, because AudioServicesCreateSystemSoundID() returns an error for some of them (but not all).

Here's an example of the command I've used successfully for a single file:

afconvert -f caff -d LEI16@44100 -c 1 whistle.wav whistle.caf

How do I do this quickly - not one-by-one for each file?

jmlane
  • 2,109
  • 1
  • 16
  • 24
Elliot
  • 6,086
  • 11
  • 45
  • 57

7 Answers7

36

Similar approach for bash: for i in *.wav; do afconvert -f caff -d LEI16@44100 -c 1 $i ${i%.wav}.caf; done

jmlane
  • 2,109
  • 1
  • 16
  • 24
Randall
  • 361
  • 1
  • 2
  • 2
9

On Windows, use the %~ni syntax.

for %i in (*.wav) do afconvert -f caff -d LEI16@44100 -c 1 %i %~ni.caf
Danyal Aytekin
  • 4,106
  • 3
  • 36
  • 44
lavinio
  • 23,931
  • 5
  • 55
  • 71
5
for file in *.wav; do afconvert -f caff -d LEI16@44100 -c 1 "$file"; done

Hit the Return key directly after done.

Simple :)

Greenonline
  • 1,330
  • 8
  • 23
  • 31
AABSTRKT
  • 49
  • 1
  • 2
5

found this:

##
## Shell script to batch convert all files in a directory to caf sound format for iPhone
## Place this shell script a directory with sound files and run it: 'sh converttocaf.sh'
## Any comments to 'support@ezone.com'
##
 
for f in *; do
    if  [ "$f" != "converttocaf.sh" ]
    then
        /usr/bin/afconvert -f caff -d LEI16 "$f"
        echo "$f converted"
    fi
done

Customize the afconvert options then save it as a text file called 'converttocaf.sh', place it in the directory of files you want to convert and run it from Terminal.

It works with files with spaces in their names.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ward
  • 3,318
  • 3
  • 30
  • 50
  • 1
    combined it with a bit of syntax from Randall's answer and my own conversion settings and it worked great! Thank you! – horseshoe7 Jul 25 '11 at 20:19
5

For the people who are using OSX and are a bit afraid of Terminal scripts I created a little application with Automator, this application converts the files you select.

Download here

Jankeesvw
  • 711
  • 8
  • 18
  • I just updated the link, it's now available at: https://dl.dropboxusercontent.com/u/135074/convert-to-caff.zip – Jankeesvw Jun 09 '16 at 13:44
1

If you are dealing with filenames that contain spaces on Linux, try the following code:

SAVEIFS=$IFS 
IFS=$(echo -en "\n\b"); for i in *.wav; do afconvert -f caff -d LEI16@44100 -c 1 $i ${i%.wav}.caf; done
Greenonline
  • 1,330
  • 8
  • 23
  • 31
0

Python script on OSX. Default data format of the .caf is ima4. Default directory is '.'

Make a file called wav2caf.py, put it in your path, make it executable, fill it with:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import argparse
import glob

def main():
    # handle command line args
    parser = argparse.ArgumentParser(description='A program that converts .wav to .caf files.', formatter_class=argparse.RawTextHelpFormatter)
    parser.add_help = True
    parser.add_argument('-f', '--folder', dest='folder', type=str, default='.', help='folder of files to convert')
    parser.add_argument('-d', '--data', dest='data', type=str, default='ima4', help='data format of .caf')
    args = parser.parse_args()

    # process files in folder
    os.chdir(args.folder)
    for filename in glob.glob("*.wav"):
        name, ext = os.path.splitext(filename)
        command = 'afconvert -f caff -d ' + args.data + ' ' + filename + ' ' + name + '.caf'
        os.system(command)

if __name__ == "__main__":
    main()

Converts all .wav to .caf in current directory:

wav2caf.py

Converts all .wav to .caf in specified directory:

wav2caf.py -f Desktop/wavs/

Converts all .wav to .caf with data type 'aac ':

wav2caf.py -d 'aac '
delrocco
  • 495
  • 1
  • 4
  • 23