How do I use afconvert to convert all the files in a directory from wav to caf?

后端 未结 7 2250
无人共我
无人共我 2021-01-31 00:24

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).

Her

7条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-31 01:01

    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 '
    

提交回复
热议问题